Index.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_LayeredNavigation
  18. * @copyright Copyright (c) 2017 Mageplaza (http://www.mageplaza.com/)
  19. * @license https://www.mageplaza.com/LICENSE.txt
  20. */
  21. namespace Mageplaza\LayeredNavigation\Controller\Search\Result;
  22. use Magento\Catalog\Model\Layer\Resolver;
  23. use Magento\Catalog\Model\Session;
  24. use Magento\Framework\App\Action\Context;
  25. use Magento\Search\Model\QueryFactory;
  26. use Magento\Store\Model\StoreManagerInterface;
  27. /**
  28. * Class Index
  29. * @package Mageplaza\LayeredNavigation\Controller\Search\Result
  30. */
  31. class Index extends \Magento\Framework\App\Action\Action
  32. {
  33. /**
  34. * Catalog session
  35. *
  36. * @var Session
  37. */
  38. protected $_catalogSession;
  39. /**
  40. * @var StoreManagerInterface
  41. */
  42. protected $_storeManager;
  43. /**
  44. * @type \Magento\Framework\Json\Helper\Data
  45. */
  46. protected $_jsonHelper;
  47. /**
  48. * @type \Mageplaza\LayeredNavigation\Helper\Data
  49. */
  50. protected $_moduleHelper;
  51. /**
  52. * @type \Magento\CatalogSearch\Helper\Data
  53. */
  54. protected $_helper;
  55. /**
  56. * @var QueryFactory
  57. */
  58. private $_queryFactory;
  59. /**
  60. * Catalog Layer Resolver
  61. *
  62. * @var Resolver
  63. */
  64. private $layerResolver;
  65. /**
  66. * @param \Magento\Framework\App\Action\Context $context
  67. * @param \Magento\Catalog\Model\Session $catalogSession
  68. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  69. * @param \Magento\Search\Model\QueryFactory $queryFactory
  70. * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
  71. * @param \Magento\CatalogSearch\Helper\Data $helper
  72. * @param \Magento\Framework\Json\Helper\Data $jsonHelper
  73. * @param \Mageplaza\LayeredNavigation\Helper\Data $moduleHelper
  74. */
  75. public function __construct(
  76. Context $context,
  77. Session $catalogSession,
  78. StoreManagerInterface $storeManager,
  79. QueryFactory $queryFactory,
  80. Resolver $layerResolver,
  81. \Magento\CatalogSearch\Helper\Data $helper,
  82. \Magento\Framework\Json\Helper\Data $jsonHelper,
  83. \Mageplaza\LayeredNavigation\Helper\Data $moduleHelper
  84. )
  85. {
  86. parent::__construct($context);
  87. $this->_storeManager = $storeManager;
  88. $this->_catalogSession = $catalogSession;
  89. $this->_queryFactory = $queryFactory;
  90. $this->layerResolver = $layerResolver;
  91. $this->_jsonHelper = $jsonHelper;
  92. $this->_moduleHelper = $moduleHelper;
  93. $this->_helper = $helper;
  94. }
  95. /**
  96. * Display search result
  97. *
  98. * @return void
  99. */
  100. public function execute()
  101. {
  102. $this->layerResolver->create(Resolver::CATALOG_LAYER_SEARCH);
  103. /* @var $query \Magento\Search\Model\Query */
  104. $query = $this->_queryFactory->get();
  105. $query->setStoreId($this->_storeManager->getStore()->getId());
  106. if ($query->getQueryText() != '') {
  107. if ($this->_helper->isMinQueryLength()) {
  108. $query->setId(0)->setIsActive(1)->setIsProcessed(1);
  109. } else {
  110. $query->saveIncrementalPopularity();
  111. if ($query->getRedirect()) {
  112. $this->getResponse()->setRedirect($query->getRedirect());
  113. return;
  114. }
  115. }
  116. $this->_helper->checkNotes();
  117. if ($this->_moduleHelper->isEnabled() && $this->getRequest()->isAjax()) {
  118. $navigation = $this->_view->getLayout()->getBlock('catalogsearch.leftnav');
  119. $products = $this->_view->getLayout()->getBlock('search.result');
  120. $result = ['products' => $products->toHtml(), 'navigation' => $navigation->toHtml()];
  121. $this->getResponse()->representJson($this->_jsonHelper->jsonEncode($result));
  122. } else {
  123. $this->_view->loadLayout();
  124. $this->_view->renderLayout();
  125. }
  126. } else {
  127. $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
  128. }
  129. }
  130. }