vijayrami

data.php

Jun 11th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.38 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Mageplaza
  4.  *
  5.  * NOTICE OF LICENSE
  6.  *
  7.  * This source file is subject to the Mageplaza.com license that is
  8.  * available through the world-wide-web at this URL:
  9.  * https://www.mageplaza.com/LICENSE.txt
  10.  *
  11.  * DISCLAIMER
  12.  *
  13.  * Do not edit or add to this file if you wish to upgrade this extension to newer
  14.  * version in the future.
  15.  *
  16.  * @category    Mageplaza
  17.  * @package     Mageplaza_Search
  18.  * @copyright   Copyright (c) 2017 Mageplaza (http://www.mageplaza.com/)
  19.  * @license     https://www.mageplaza.com/LICENSE.txt
  20.  */
  21.  
  22. namespace Mageplaza\Search\Helper;
  23.  
  24. use Magento\Catalog\Model\CategoryFactory;
  25. use Magento\Catalog\Model\Config;
  26. use Magento\Catalog\Model\Product\Visibility;
  27. use Magento\Catalog\Model\ResourceModel\Product\Collection;
  28. use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
  29. use Magento\Customer\Model\Session;
  30. use Magento\Framework\App\Helper\Context;
  31. use Magento\Framework\Escaper;
  32. use Magento\Framework\Locale\FormatInterface;
  33. use Magento\Framework\ObjectManagerInterface;
  34. use Magento\Framework\Pricing\Helper\Data as PricingHelper;
  35. use Magento\Framework\Registry;
  36. use Magento\Store\Model\StoreManagerInterface;
  37. use Mageplaza\Core\Helper\AbstractData;
  38. use Mageplaza\Search\Model\Product\Url;
  39.  
  40. /**
  41.  * Search helper
  42.  */
  43. class Data extends AbstractData
  44. {
  45.     const CONFIG_MODULE_PATH = 'mpsearch';
  46.  
  47.     /**
  48.      * @var \Magento\Catalog\Model\Product\Visibility
  49.      */
  50.     protected $productVisibility;
  51.  
  52.     /**
  53.      * @var \Magento\Catalog\Model\Config
  54.      */
  55.     protected $catalogConfig;
  56.  
  57.     /**
  58.      * @var \Magento\Framework\Pricing\Helper\Data
  59.      */
  60.     protected $_priceHelper;
  61.  
  62.     /**
  63.      * @var \Magento\Framework\Escaper
  64.      */
  65.     protected $_escaper;
  66.  
  67.     /**
  68.      * Customer session
  69.      *
  70.      * @var \Magento\Customer\Model\Session
  71.      */
  72.     protected $_customerSession;
  73.  
  74.     /**
  75.      * @var \Magento\Customer\Model\ResourceModel\Group\CollectionFactory
  76.      */
  77.     protected $_customerGroupFactory;
  78.  
  79.     /**
  80.      * @var \Magento\Framework\Locale\FormatInterface
  81.      */
  82.     protected $localeFormat;
  83.  
  84.     /**
  85.      * @var \Magento\Catalog\Model\CategoryFactory
  86.      */
  87.     protected $categoryFactory;
  88.  
  89.     /**
  90.      * Data constructor.
  91.      * @param \Magento\Framework\App\Helper\Context $context
  92.      * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  93.      * @param \Magento\Framework\ObjectManagerInterface $objectManager
  94.      * @param \Magento\Customer\Model\ResourceModel\Group\CollectionFactory $customerGroupCollectionFactory
  95.      * @param \Magento\Framework\Escaper $escaper
  96.      * @param \Magento\Framework\Pricing\Helper\Data $priceHelper
  97.      * @param \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility
  98.      * @param \Magento\Catalog\Model\Config $catalogConfig
  99.      * @param \Magento\Customer\Model\Session $customerSession
  100.      * @param \Magento\Framework\Locale\FormatInterface $localeFormat
  101.      * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
  102.      */
  103.     public function __construct(
  104.         Context $context,
  105.         StoreManagerInterface $storeManager,
  106.         ObjectManagerInterface $objectManager,
  107.         CollectionFactory $customerGroupCollectionFactory,
  108.         Escaper $escaper,
  109.         PricingHelper $priceHelper,
  110.         Visibility $catalogProductVisibility,
  111.         Config $catalogConfig,
  112.         Session $customerSession,
  113.         FormatInterface $localeFormat,
  114.         CategoryFactory $categoryFactory
  115.     )
  116.     {
  117.         $this->_customerGroupFactory = $customerGroupCollectionFactory;
  118.         $this->_escaper              = $escaper;
  119.         $this->_priceHelper          = $priceHelper;
  120.         $this->productVisibility     = $catalogProductVisibility;
  121.         $this->catalogConfig         = $catalogConfig;
  122.         $this->_customerSession      = $customerSession;
  123.         $this->localeFormat          = $localeFormat;
  124.         $this->categoryFactory       = $categoryFactory;
  125.  
  126.         parent::__construct($context, $objectManager, $storeManager);
  127.     }
  128.  
  129.     /**
  130.      * @param null $storeId
  131.      * @return bool
  132.      */
  133.     public function isEnabled($storeId = null)
  134.     {
  135.         return $this->getConfigGeneral('enabled', $storeId) && $this->isModuleOutputEnabled();
  136.     }
  137.  
  138.     /**
  139.      * @param string $code
  140.      * @param null $storeId
  141.      * @return mixed
  142.      */
  143.     public function getConfigGeneral($code = '', $storeId = null)
  144.     {
  145.         $code = ($code !== '') ? '/' . $code : '';
  146.  
  147.         return $this->getConfigValue(static::CONFIG_MODULE_PATH . '/general' . $code, $storeId);
  148.     }
  149.  
  150.     /**
  151.      * @return \Mageplaza\Search\Helper\Media
  152.      */
  153.     public function getMediaHelper()
  154.     {
  155.         return $this->objectManager->get(Media::class);
  156.     }
  157.  
  158.     /**
  159.      * @param null $store
  160.      * @return string
  161.      */
  162.     public function getSearchBy($store = null)
  163.     {
  164.         $searchBy = $this->getConfigGeneral('search_by', $store);
  165.  
  166.         return self::jsonEncode(explode(',', $searchBy));
  167.     }
  168.  
  169.     /**
  170.      * @param null $store
  171.      * @return string
  172.      */
  173.     public function getDisplay($store = null)
  174.     {
  175.         $searchBy = $this->getConfigGeneral('display', $store);
  176.  
  177.         return self::jsonEncode(explode(',', $searchBy));
  178.     }
  179.  
  180.     /**
  181.      * Create json file to contain product data
  182.      */
  183.     public function createJsonFile()
  184.     {
  185.         $errors         = [];
  186.         $customerGroups = $this->_customerGroupFactory->create();
  187.         foreach ($this->storeManager->getStores() as $store) {
  188.             foreach ($customerGroups as $group) {
  189.                 try {
  190.                     $this->createJsonFileForStore($store, $group->getId());
  191.                 } catch (\Exception $e) {
  192.                     $errors[] = __('Cannot generate data for store %1 and customer group %2, %3', $store->getCode(), $group->getCode(), $e->getMessage());
  193.                 }
  194.             }
  195.         }
  196.  
  197.         return $errors;
  198.     }
  199.  
  200.     /**
  201.      * @param $store
  202.      * @param $group
  203.      * @return $this
  204.      */
  205.     public function createJsonFileForStore($store, $group)
  206.     {
  207.         if(!$this->isEnabled($store->getId())){
  208.             return $this;
  209.         }
  210.  
  211.         $productList = [];
  212.  
  213.         /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
  214.         $collection = $this->objectManager->create(Collection::class);
  215.         $collection->addAttributeToSelect($this->catalogConfig->getProductAttributes())
  216.             ->setStore($store)
  217.             ->addPriceData($group)
  218.             ->addMinimalPrice()
  219.             ->addFinalPrice()
  220.             ->addTaxPercents()
  221.             ->addStoreFilter()
  222.             ->addUrlRewrite()
  223.             ->setVisibility($this->productVisibility->getVisibleInSearchIds());
  224.  
  225.         /** @var \Magento\Catalog\Model\Product $product */
  226.         foreach ($collection as $product) {
  227.             $productList[] = [
  228.                 'value' => $product->getName(),
  229.                 'c'     => $product->getCategoryIds(), //categoryIds
  230.                 'd'     => $this->getProductDescription($product, $store), //short description
  231.                 'p'     => $this->_priceHelper->currencyByStore($product->getFinalPrice(), $store, false, false), //price
  232.                 'i'     => $this->getMediaHelper()->getProductImage($product),//image
  233.                 'u'     => $this->getProductUrl($product) //product url
  234.             ];
  235.         }
  236.  
  237.         $this->getMediaHelper()->createJsFile(
  238.             $this->getJsFilePath($group, $store),
  239.             'var mageplazaSearchProducts = ' . self::jsonEncode($productList)
  240.         );
  241.  
  242.         return $this;
  243.     }
  244.  
  245.     /**
  246.      * @param \Magento\Catalog\Model\Product $product
  247.      * @return bool|string
  248.      */
  249.     protected function getProductUrl($product)
  250.     {
  251.         $productUrl  = $product->getProductUrl();
  252.         $requestPath = $product->getRequestPath();
  253.         if (!$requestPath) {
  254.             $pos = strpos($productUrl, 'catalog/product/view');
  255.             if ($pos !== false) {
  256.                 $productUrl = substr($productUrl, $pos + 20);
  257.             }
  258.         } else {
  259.             $productUrl = $requestPath;
  260.         }
  261.  
  262.         return $productUrl;
  263.     }
  264.  
  265.     /**
  266.      * @param $product
  267.      * @param $store
  268.      * @return array|bool|string
  269.      */
  270.     protected function getProductDescription($product, $store)
  271.     {
  272.         $attributeHtml = strip_tags($product->getShortDescription());
  273.         $attributeHtml = $this->_escaper->escapeHtml($attributeHtml);
  274.         if ($limitDesLetter = (int)$this->getConfigGeneral('max_letter_numbers', $store->getId())) {
  275.             $attributeHtml = substr($attributeHtml, 0, $limitDesLetter);
  276.         }
  277.  
  278.         return $attributeHtml;
  279.     }
  280.  
  281.     /**
  282.      * @param int $customerGroupId
  283.      * @param \Magento\Store\Model\Store $store
  284.      * @return string
  285.      */
  286.     public function getJsFilePath($customerGroupId, $store)
  287.     {
  288.         return Media::TEMPLATE_MEDIA_PATH . '/' . $store->getCode() . '_' . $customerGroupId . '.js';
  289.     }
  290.  
  291.     /**
  292.      * @return string
  293.      */
  294.     public function getJsFileUrl()
  295.     {
  296.         $customerGroupId = $this->_customerSession->getCustomerGroupId();
  297.  
  298.         /** @var \Magento\Store\Model\Store $store */
  299.         $store = $this->storeManager->getStore();
  300.  
  301.         $mediaDirectory = $this->getMediaHelper()->getMediaDirectory();
  302.         $filePath       = $this->getJsFilePath($customerGroupId, $store);
  303.         if (!$mediaDirectory->isFile($filePath)) {
  304.             $this->createJsonFileForStore($store, $customerGroupId);
  305.         }
  306.  
  307.         return $this->getMediaHelper()->getMediaUrl($filePath);
  308.     }
  309.  
  310.     /**
  311.      * @return array
  312.      */
  313.     public function getCategoryTree()
  314.     {
  315.         $categoriesOptions = [0 => __('All Categories')];
  316.  
  317.         $maxLevel   = max(0, (int)$this->getConfigGeneral('category/max_depth')) ?: 2;
  318.         $parent     = $this->storeManager->getStore()->getRootCategoryId();
  319.         $categories = $this->categoryFactory->create()
  320.             ->getCategories($parent, 1, false, true);
  321.         foreach ($categories as $category) {
  322.             $this->getCategoryOptions($category, $categoriesOptions, $maxLevel);
  323.         }
  324.  
  325.         return $categoriesOptions;
  326.     }
  327.  
  328.     /**
  329.      * @param $category
  330.      * @param $options
  331.      * @param $level
  332.      * @param string $htmlPrefix
  333.      * @return $this
  334.      */
  335.     protected function getCategoryOptions($category, &$options, $level, $htmlPrefix = '')
  336.     {
  337.         if ($level <= 0) {
  338.             return $this;
  339.         }
  340.         $level--;
  341.  
  342.         $options[$category->getId()] = $htmlPrefix . $category->getName();
  343.  
  344.         $htmlPrefix .= '- ';
  345.         if ($this->getChildCategories($category)) {
  346.         foreach ($this->getChildCategories($category) as $childCategory) {
  347.             $this->getCategoryOptions($childCategory, $options, $level, $htmlPrefix);
  348.         }
  349.         }
  350.  
  351.         return $this;
  352.     }
  353.  
  354.     /**
  355.      * @param \Magento\Catalog\Model\Category $category
  356.      * @return array
  357.      */
  358.     public function getChildCategories($category)
  359.     {
  360.         if ($category->getUseFlatResource()) {
  361.             return $category->getChildrenNodes();
  362.         }
  363.  
  364.         return $category->getChildrenCategories();
  365.     }
  366.  
  367.     /**
  368.      * @return string
  369.      */
  370.     public function getPriceFormat()
  371.     {
  372.         return self::jsonEncode($this->localeFormat->getPriceFormat());
  373.     }
  374. }
Add Comment
Please, Sign In to add comment