Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/cartforge.co/app/code/Webkul/PrivateShop/Helper/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/cartforge.co/app/code/Webkul/PrivateShop/Helper/Groups.php
<?php
/**
 * Webkul Software
 *
 * @category  Webkul
 * @package   Webkul_PrivateShop
 * @author    Webkul Software Private Limited
 * @copyright Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\PrivateShop\Helper;

use Webkul\PrivateShop\Api\Data\PrivateGroupInterfaceFactory;
use Webkul\PrivateShop\Api\PrivateProductRepositoryInterface;
use Webkul\PrivateShop\Model\ResourceModel\PrivateProduct\CollectionFactory;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;

/**
 * Class Groups
 * Webkul\PrivateShop\Helper
 */
class Groups extends \Magento\Backend\Helper\Data
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var \Magento\Backend\Model\Session
     */
    protected $_session;

    /**
     * Selected products for mass-update
     *
     * @var \Webkul\PrivateShop\Model\ResourceModel\PrivateProduct\Collection
     */
    protected $_privateProducts;

    /**
     * Selected products for mass-update
     *
     * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
     */
    protected $_products;

    /**
     * Selected customers for mass-update
     *
     * @var \Magento\Customer\Model\ResourceModel\Customer\Collection
     */
    protected $_customers;

    /**
     * array of product ids
     *
     * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
     */
    protected $productIds;

    /**
     * @var PrivateGroupInterfaceFactory
     */
    protected $privateGroupFactory;

    /**
     * @var productsFactory
     */
    protected $_productsFactory;

    /**
     * @var \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory
     */
    protected $customersFactory;

    /**
     * @var CustomerRepositoryInterface
     */
    protected $customerRepository;

    /**
     * @var CustomerInterfaceFactory
     */
    protected $customerDataFactory;

    /**
     * @var CollectionFactory
     */
    protected $privateProductFactory;

    /**
     * @var PrivateProductRepositoryInterface
     */
    protected $privateProductRepository;

    /**
     * @var \Magento\Framework\Serialize\Serializer\Json
     */
    protected $jsonSerializer;

    /**
     * @var \Magento\Framework\App\Request\Http
     */
    protected $request;

    /**
     * @var \Webkul\PrivateShop\Logger\PrivateGroupLogger
     */
    protected $logger;

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Magento\Framework\App\Route\Config $routeConfig
     * @param \Magento\Framework\Locale\ResolverInterface $locale
     * @param \Magento\Backend\Model\UrlInterface $backendUrl
     * @param \Magento\Backend\Model\Auth $auth
     * @param \Magento\Backend\App\Area\FrontNameResolver $frontNameResolver
     * @param \Magento\Framework\Math\Random $mathRandom
     * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productsFactory
     * @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customersFactory
     * @param CustomerRepositoryInterface $customerRepository
     * @param CustomerInterfaceFactory $customerDataFactory
     * @param PrivateGroupInterfaceFactory $privateGroupFactory
     * @param PrivateProductRepositoryInterface $privateProductRepository
     * @param CollectionFactory $privateProductFactory
     * @param \Magento\Backend\Model\Session $session
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\App\Request\Http $request
     * @param \Magento\Framework\Serialize\Serializer\Json $jsonSerializer
     * @param \Webkul\PrivateShop\Logger\PrivateGroupLogger $logger
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\App\Route\Config $routeConfig,
        \Magento\Framework\Locale\ResolverInterface $locale,
        \Magento\Backend\Model\UrlInterface $backendUrl,
        \Magento\Backend\Model\Auth $auth,
        \Magento\Backend\App\Area\FrontNameResolver $frontNameResolver,
        \Magento\Framework\Math\Random $mathRandom,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productsFactory,
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customersFactory,
        CustomerRepositoryInterface $customerRepository,
        CustomerInterfaceFactory $customerDataFactory,
        PrivateGroupInterfaceFactory $privateGroupFactory,
        PrivateProductRepositoryInterface $privateProductRepository,
        CollectionFactory $privateProductFactory,
        \Magento\Backend\Model\SessionFactory $session,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\Serialize\Serializer\Json $jsonSerializer,
        \Webkul\PrivateShop\Logger\PrivateGroupLogger $logger,
    ) {
        $this->privateGroupFactory = $privateGroupFactory;
        $this->_session = $session;
        $this->_productsFactory = $productsFactory;
        $this->customersFactory = $customersFactory;
        $this->customerRepository = $customerRepository;
        $this->customerDataFactory = $customerDataFactory;
        $this->privateProductFactory = $privateProductFactory;
        $this->privateProductRepository = $privateProductRepository;
        $this->_storeManager = $storeManager;
        $this->jsonSerializer = $jsonSerializer;
        $this->request = $request;
        $this->logger = $logger;
        parent::__construct($context, $routeConfig, $locale, $backendUrl, $auth, $frontNameResolver, $mathRandom);
    }

    /**
     * Return selected store id from request
     *
     * @return integer
     */
    public function getSelectedStoreId()
    {
        return (int)$this->_getRequest()->getParam('store', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
    }

    /**
     * Return private product collection with selected product filter
     *
     * @return \Webkul\PrivateShop\Model\ResourceModel\PrivateProduct\Collection
     */
    public function getPrivateProducts()
    {
        if ($this->_privateProducts === null) {
            $groupIds = $this->getGroupIds();

            if (!is_array($groupIds)) {
                $groupIds = [0];
            }

            $this->_privateProducts = $this->privateProductFactory->create()
                ->addFieldToFilter(
                    'group_id',
                    ['in' => $groupIds]
                );
        }

        return $this->_privateProducts;
    }

    /**
     * Return product collection with selected product filter
     *
     * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
     */
    public function getProducts()
    {
        if ($this->_products === null) {
            $productsIds = $this->getProductIds();

            if (!is_array($productsIds)) {
                $productsIds = [0];
            }

            $this->_products = $this->_productsFactory->create()
                ->addAttributeToFilter(
                    'product_private_group',
                    [['neq' => '']],
                    'left'
                );
        }

        return $this->_products;
    }

    /**
     * Return customer collection
     *
     * @return \Magento\Customer\Model\ResourceModel\Customer\Collection
     */
    public function getCustomers()
    {
        if ($this->_customers === null) {
            $this->_customers = $this->customersFactory->create()
                ->addAttributeToFilter(
                    'customer_private_group',
                    [['neq' => '']],
                    'left'
                );
        }
        return $this->_customers;
    }

    /**
     * Set array of selected groups
     *
     * @param array $groupIds
     *
     * @return void
     */
    public function setGroupIds($groupIds)
    {
        $this->_session->create()->setGroupIds($groupIds);
    }

    /**
     * Return array of selected groups ids from post or session
     *
     * @return array|null
     */
    public function getGroupIds()
    {
        return $this->_session->create()->getGroupIds();
    }

    /**
     * Return array of selected product ids from post or session
     *
     * @return array|null
     */
    public function getProductIds()
    {
        $privateProducts =  $this->getPrivateProducts();
        if (!$this->productIds) {
            $productIds = [];
            foreach ($privateProducts as $model) {
                $productIds[] = $model->getProductIds();
            }
            $this->productIds = $productIds;
        }
        return $this->productIds;
    }

    /**
     * Updating customer groups
     *
     * @return void
     */
    public function updateCustomerGroups()
    {
        try {
            foreach ($this->getCustomers() as $customer) {
                $customerGroups = $this->jsonSerializer->unserialize(
                    $customer->getData('customer_private_group')
                );
                $differenceValue = array_diff($customerGroups, $this->getGroupIds());
                $this->request->setParam('private_shop', ['data' => $differenceValue]);
                if (empty($differenceValue)) {
                    $this->request->setParam('private_shop', '');
                }
                $this->saveModel($this->customerRepository, $customer->getDataModel());
            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            throw new CouldNotSaveException(
                __('Something went wrong while updating the customer private groups.'),
                $e
            );
        } catch (\Exception $e) {
            throw new CouldNotSaveException(
                __('Something went wrong while updating the customer private groups.'),
                $e
            );
        }
    }

    /**
     * Remove private Product and update Magento products attribute value
     *
     * @throws CouldNotSaveException
     * @return void
     */
    public function updatePrivateProducts()
    {
        $updateAtrribute = [
            'is_private_product' => 0
        ];
        try {
            $updatePrivateAttributeIds = [];
            foreach ($this->getProducts() as $product) {
                $productGroups = $this->jsonSerializer->unserialize(
                    $product->getData('product_private_group')
                );
                $differenceValue = array_diff($productGroups, $this->getGroupIds());
                $updatedGroups = $this->jsonSerializer->serialize(
                    $differenceValue
                );
                if (empty($differenceValue)) {
                    $updatePrivateAttributeIds[] = $product->getId();
                    $updatedGroups = '';
                }
                
                \Magento\Framework\App\ObjectManager::getInstance()
                    ->get(\Magento\Catalog\Model\Product\Action::class)
                    ->updateAttributes(
                        [$product->getId()],
                        ['product_private_group' => $updatedGroups],
                        $this->getSelectedStoreId()
                    );
            }

            if (!empty($updatePrivateAttributeIds)) {
                \Magento\Framework\App\ObjectManager::getInstance()
                    ->get(\Magento\Catalog\Model\Product\Action::class)
                    ->updateAttributes(
                        $updatePrivateAttributeIds,
                        $updateAtrribute,
                        $this->getSelectedStoreId()
                    );
            }
            $this->deletePrivateProducts();
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            throw new CouldNotSaveException(
                __('Something went wrong while updating the product(s) private attributes.'),
                $e
            );
        } catch (\Exception $e) {
            throw new CouldNotSaveException(
                __('Something went wrong while updating the product(s) private attributes.'),
                $e
            );
        }
    }

    /**
     * Delete private products from Module
     *
     * @throws CouldNotSaveException
     */
    private function deletePrivateProducts()
    {
        try {
            foreach ($this->getPrivateProducts() as $model) {
                $this->privateProductRepository->deleteById($model->getId());
            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            throw new CouldNotSaveException(
                __('Something went wrong while deleting private products.'),
                $e
            );
        } catch (\Exception $e) {
            throw new CouldNotSaveException(
                __('Something went wrong while deleting private products.'),
                $e
            );
        }
    }

    /**
     * Save Model using Object.
     *
     * @param object $model
     * @param object $object
     */
    public function saveModel($model, $object)
    {
        $model->save($object);
    }

    /**
     * [logDataInLogger  is used to log the data in the marktplace.log file]
     *
     * @param  string $data
     * @return
     */
    public function logDataInLogger($data)
    {
        $this->logger->info($data);
    }
}

Spamworldpro Mini