Data.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. namespace Mageplaza\Search\Helper;
  22. use Magento\Catalog\Model\CategoryFactory;
  23. use Magento\Catalog\Model\Config;
  24. use Magento\Catalog\Model\Product\Visibility;
  25. use Magento\Catalog\Model\ResourceModel\Product\Collection;
  26. use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
  27. use Magento\Customer\Model\Session;
  28. use Magento\Framework\App\Helper\Context;
  29. use Magento\Framework\Escaper;
  30. use Magento\Framework\Locale\FormatInterface;
  31. use Magento\Framework\ObjectManagerInterface;
  32. use Magento\Framework\Pricing\Helper\Data as PricingHelper;
  33. use Magento\Framework\Registry;
  34. use Magento\Store\Model\StoreManagerInterface;
  35. use Mageplaza\Core\Helper\AbstractData;
  36. use Mageplaza\Search\Model\Product\Url;
  37. /**
  38. * Search helper
  39. */
  40. class Data extends AbstractData
  41. {
  42. const CONFIG_MODULE_PATH = 'mpsearch';
  43. /**
  44. * @var \Magento\Catalog\Model\Product\Visibility
  45. */
  46. protected $productVisibility;
  47. /**
  48. * @var \Magento\Catalog\Model\Config
  49. */
  50. protected $catalogConfig;
  51. /**
  52. * @var \Magento\Framework\Pricing\Helper\Data
  53. */
  54. protected $_priceHelper;
  55. /**
  56. * @var \Magento\Framework\Escaper
  57. */
  58. protected $_escaper;
  59. /**
  60. * Customer session
  61. *
  62. * @var \Magento\Customer\Model\Session
  63. */
  64. protected $_customerSession;
  65. /**
  66. * @var \Magento\Customer\Model\ResourceModel\Group\CollectionFactory
  67. */
  68. protected $_customerGroupFactory;
  69. /**
  70. * @var \Magento\Framework\Locale\FormatInterface
  71. */
  72. protected $localeFormat;
  73. /**
  74. * @var \Magento\Catalog\Model\CategoryFactory
  75. */
  76. protected $categoryFactory;
  77. /**
  78. * Data constructor.
  79. * @param \Magento\Framework\App\Helper\Context $context
  80. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  81. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  82. * @param \Magento\Customer\Model\ResourceModel\Group\CollectionFactory $customerGroupCollectionFactory
  83. * @param \Magento\Framework\Escaper $escaper
  84. * @param \Magento\Framework\Pricing\Helper\Data $priceHelper
  85. * @param \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility
  86. * @param \Magento\Catalog\Model\Config $catalogConfig
  87. * @param \Magento\Customer\Model\Session $customerSession
  88. * @param \Magento\Framework\Locale\FormatInterface $localeFormat
  89. * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
  90. */
  91. public function __construct(
  92. Context $context,
  93. StoreManagerInterface $storeManager,
  94. ObjectManagerInterface $objectManager,
  95. CollectionFactory $customerGroupCollectionFactory,
  96. Escaper $escaper,
  97. PricingHelper $priceHelper,
  98. Visibility $catalogProductVisibility,
  99. Config $catalogConfig,
  100. Session $customerSession,
  101. FormatInterface $localeFormat,
  102. CategoryFactory $categoryFactory
  103. )
  104. {
  105. $this->_customerGroupFactory = $customerGroupCollectionFactory;
  106. $this->_escaper = $escaper;
  107. $this->_priceHelper = $priceHelper;
  108. $this->productVisibility = $catalogProductVisibility;
  109. $this->catalogConfig = $catalogConfig;
  110. $this->_customerSession = $customerSession;
  111. $this->localeFormat = $localeFormat;
  112. $this->categoryFactory = $categoryFactory;
  113. parent::__construct($context, $objectManager, $storeManager);
  114. }
  115. /**
  116. * @param null $storeId
  117. * @return bool
  118. */
  119. public function isEnabled($storeId = null)
  120. {
  121. return $this->getConfigGeneral('enabled', $storeId) && $this->isModuleOutputEnabled();
  122. }
  123. /**
  124. * @param string $code
  125. * @param null $storeId
  126. * @return mixed
  127. */
  128. public function getConfigGeneral($code = '', $storeId = null)
  129. {
  130. $code = ($code !== '') ? '/' . $code : '';
  131. return $this->getConfigValue(static::CONFIG_MODULE_PATH . '/general' . $code, $storeId);
  132. }
  133. /**
  134. * @return \Mageplaza\Search\Helper\Media
  135. */
  136. public function getMediaHelper()
  137. {
  138. return $this->objectManager->get(Media::class);
  139. }
  140. /**
  141. * @param null $store
  142. * @return string
  143. */
  144. public function getSearchBy($store = null)
  145. {
  146. $searchBy = $this->getConfigGeneral('search_by', $store);
  147. return self::jsonEncode(explode(',', $searchBy));
  148. }
  149. /**
  150. * @param null $store
  151. * @return string
  152. */
  153. public function getDisplay($store = null)
  154. {
  155. $searchBy = $this->getConfigGeneral('display', $store);
  156. return self::jsonEncode(explode(',', $searchBy));
  157. }
  158. /**
  159. * Create json file to contain product data
  160. */
  161. public function createJsonFile()
  162. {
  163. $errors = [];
  164. $customerGroups = $this->_customerGroupFactory->create();
  165. foreach ($this->storeManager->getStores() as $store) {
  166. foreach ($customerGroups as $group) {
  167. try {
  168. $this->createJsonFileForStore($store, $group->getId());
  169. } catch (\Exception $e) {
  170. $errors[] = __('Cannot generate data for store %1 and customer group %2, %3', $store->getCode(), $group->getCode(), $e->getMessage());
  171. }
  172. }
  173. }
  174. return $errors;
  175. }
  176. /**
  177. * @param $store
  178. * @param $group
  179. * @return $this
  180. */
  181. public function createJsonFileForStore($store, $group)
  182. {
  183. if(!$this->isEnabled($store->getId())){
  184. return $this;
  185. }
  186. $productList = [];
  187. /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
  188. $collection = $this->objectManager->create(Collection::class);
  189. $collection->addAttributeToSelect($this->catalogConfig->getProductAttributes())
  190. ->setStore($store)
  191. ->addPriceData($group)
  192. ->addMinimalPrice()
  193. ->addFinalPrice()
  194. ->addTaxPercents()
  195. ->addStoreFilter()
  196. ->addUrlRewrite()
  197. ->setVisibility($this->productVisibility->getVisibleInSearchIds());
  198. /** @var \Magento\Catalog\Model\Product $product */
  199. foreach ($collection as $product) {
  200. $productList[] = [
  201. 'value' => $product->getName(),
  202. 'c' => $product->getCategoryIds(), //categoryIds
  203. 'd' => $this->getProductDescription($product, $store), //short description
  204. 'p' => $this->_priceHelper->currencyByStore($product->getFinalPrice(), $store, false, false), //price
  205. 'i' => $this->getMediaHelper()->getProductImage($product),//image
  206. 'u' => $this->getProductUrl($product) //product url
  207. ];
  208. }
  209. $this->getMediaHelper()->createJsFile(
  210. $this->getJsFilePath($group, $store),
  211. 'var mageplazaSearchProducts = ' . self::jsonEncode($productList)
  212. );
  213. return $this;
  214. }
  215. /**
  216. * @param \Magento\Catalog\Model\Product $product
  217. * @return bool|string
  218. */
  219. protected function getProductUrl($product)
  220. {
  221. $productUrl = $product->getProductUrl();
  222. $requestPath = $product->getRequestPath();
  223. if (!$requestPath) {
  224. $pos = strpos($productUrl, 'catalog/product/view');
  225. if ($pos !== false) {
  226. $productUrl = substr($productUrl, $pos + 20);
  227. }
  228. } else {
  229. $productUrl = $requestPath;
  230. }
  231. return $productUrl;
  232. }
  233. /**
  234. * @param $product
  235. * @param $store
  236. * @return array|bool|string
  237. */
  238. protected function getProductDescription($product, $store)
  239. {
  240. $attributeHtml = strip_tags($product->getShortDescription());
  241. $attributeHtml = $this->_escaper->escapeHtml($attributeHtml);
  242. if ($limitDesLetter = (int)$this->getConfigGeneral('max_letter_numbers', $store->getId())) {
  243. $attributeHtml = substr($attributeHtml, 0, $limitDesLetter);
  244. }
  245. return $attributeHtml;
  246. }
  247. /**
  248. * @param int $customerGroupId
  249. * @param \Magento\Store\Model\Store $store
  250. * @return string
  251. */
  252. public function getJsFilePath($customerGroupId, $store)
  253. {
  254. return Media::TEMPLATE_MEDIA_PATH . '/' . $store->getCode() . '_' . $customerGroupId . '.js';
  255. }
  256. /**
  257. * @return string
  258. */
  259. public function getJsFileUrl()
  260. {
  261. $customerGroupId = $this->_customerSession->getCustomerGroupId();
  262. /** @var \Magento\Store\Model\Store $store */
  263. $store = $this->storeManager->getStore();
  264. $mediaDirectory = $this->getMediaHelper()->getMediaDirectory();
  265. $filePath = $this->getJsFilePath($customerGroupId, $store);
  266. if (!$mediaDirectory->isFile($filePath)) {
  267. $this->createJsonFileForStore($store, $customerGroupId);
  268. }
  269. return $this->getMediaHelper()->getMediaUrl($filePath);
  270. }
  271. /**
  272. * @return array
  273. */
  274. public function getCategoryTree()
  275. {
  276. $categoriesOptions = [0 => __('All Categories')];
  277. $maxLevel = max(0, (int)$this->getConfigGeneral('category/max_depth')) ?: 2;
  278. $parent = $this->storeManager->getStore()->getRootCategoryId();
  279. $categories = $this->categoryFactory->create()
  280. ->getCategories($parent, 1, false, true);
  281. foreach ($categories as $category) {
  282. $this->getCategoryOptions($category, $categoriesOptions, $maxLevel);
  283. }
  284. return $categoriesOptions;
  285. }
  286. /**
  287. * @param $category
  288. * @param $options
  289. * @param $level
  290. * @param string $htmlPrefix
  291. * @return $this
  292. */
  293. protected function getCategoryOptions($category, &$options, $level, $htmlPrefix = '')
  294. {
  295. if ($level <= 0) {
  296. return $this;
  297. }
  298. $level--;
  299. $options[$category->getId()] = $htmlPrefix . $category->getName();
  300. $htmlPrefix .= '- ';
  301. foreach ($this->getChildCategories($category) as $childCategory) {
  302. $this->getCategoryOptions($childCategory, $options, $level, $htmlPrefix);
  303. }
  304. return $this;
  305. }
  306. /**
  307. * @param \Magento\Catalog\Model\Category $category
  308. * @return array
  309. */
  310. public function getChildCategories($category)
  311. {
  312. if ($category->getUseFlatResource()) {
  313. return $category->getChildrenNodes();
  314. }
  315. return $category->getChildrenCategories();
  316. }
  317. /**
  318. * @return string
  319. */
  320. public function getPriceFormat()
  321. {
  322. return self::jsonEncode($this->localeFormat->getPriceFormat());
  323. }
  324. }