Index.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\CatalogSearch\Controller\Result;
  8. use Magento\Framework\App\Action\HttpGetActionInterface;
  9. use Magento\Catalog\Model\Layer\Resolver;
  10. use Magento\Catalog\Model\Session;
  11. use Magento\Framework\App\Action\Context;
  12. use Magento\Framework\App\Action\HttpPostActionInterface;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. use Magento\Search\Model\QueryFactory;
  15. use Magento\Search\Model\PopularSearchTerms;
  16. /**
  17. * Search result.
  18. */
  19. class Index extends \Magento\Framework\App\Action\Action implements HttpGetActionInterface, HttpPostActionInterface
  20. {
  21. /**
  22. * No results default handle.
  23. */
  24. const DEFAULT_NO_RESULT_HANDLE = 'catalogsearch_result_index_noresults';
  25. /**
  26. * Catalog session
  27. *
  28. * @var Session
  29. */
  30. protected $_catalogSession;
  31. /**
  32. * @var StoreManagerInterface
  33. */
  34. protected $_storeManager;
  35. /**
  36. * @var QueryFactory
  37. */
  38. private $_queryFactory;
  39. /**
  40. * Catalog Layer Resolver
  41. *
  42. * @var Resolver
  43. */
  44. private $layerResolver;
  45. /**
  46. * @param Context $context
  47. * @param Session $catalogSession
  48. * @param StoreManagerInterface $storeManager
  49. * @param QueryFactory $queryFactory
  50. * @param Resolver $layerResolver
  51. */
  52. public function __construct(
  53. Context $context,
  54. Session $catalogSession,
  55. StoreManagerInterface $storeManager,
  56. QueryFactory $queryFactory,
  57. Resolver $layerResolver
  58. ) {
  59. parent::__construct($context);
  60. $this->_storeManager = $storeManager;
  61. $this->_catalogSession = $catalogSession;
  62. $this->_queryFactory = $queryFactory;
  63. $this->layerResolver = $layerResolver;
  64. }
  65. /**
  66. * Display search result
  67. *
  68. * @return void
  69. *
  70. * @throws \Magento\Framework\Exception\LocalizedException
  71. */
  72. public function execute()
  73. {
  74. $this->layerResolver->create(Resolver::CATALOG_LAYER_SEARCH);
  75. /* @var $query \Magento\Search\Model\Query */
  76. $query = $this->_queryFactory->get();
  77. $storeId = $this->_storeManager->getStore()->getId();
  78. $query->setStoreId($storeId);
  79. $queryText = $query->getQueryText();
  80. if ($queryText != '') {
  81. $catalogSearchHelper = $this->_objectManager->get(\Magento\CatalogSearch\Helper\Data::class);
  82. $getAdditionalRequestParameters = $this->getRequest()->getParams();
  83. unset($getAdditionalRequestParameters[QueryFactory::QUERY_VAR_NAME]);
  84. $handles = null;
  85. if ($query->getNumResults() == 0) {
  86. $this->_view->getPage()->initLayout();
  87. $handles = $this->_view->getLayout()->getUpdate()->getHandles();
  88. $handles[] = static::DEFAULT_NO_RESULT_HANDLE;
  89. }
  90. if (empty($getAdditionalRequestParameters) &&
  91. $this->_objectManager->get(PopularSearchTerms::class)->isCacheable($queryText, $storeId)
  92. ) {
  93. $this->getCacheableResult($catalogSearchHelper, $query, $handles);
  94. } else {
  95. $this->getNotCacheableResult($catalogSearchHelper, $query, $handles);
  96. }
  97. } else {
  98. $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
  99. }
  100. }
  101. /**
  102. * Return cacheable result
  103. *
  104. * @param \Magento\CatalogSearch\Helper\Data $catalogSearchHelper
  105. * @param \Magento\Search\Model\Query $query
  106. * @param array $handles
  107. * @return void
  108. */
  109. private function getCacheableResult($catalogSearchHelper, $query, $handles)
  110. {
  111. if (!$catalogSearchHelper->isMinQueryLength()) {
  112. $redirect = $query->getRedirect();
  113. if ($redirect && $this->_url->getCurrentUrl() !== $redirect) {
  114. $this->getResponse()->setRedirect($redirect);
  115. return;
  116. }
  117. }
  118. $catalogSearchHelper->checkNotes();
  119. $this->_view->loadLayout($handles);
  120. $this->_view->renderLayout();
  121. }
  122. /**
  123. * Return not cacheable result
  124. *
  125. * @param \Magento\CatalogSearch\Helper\Data $catalogSearchHelper
  126. * @param \Magento\Search\Model\Query $query
  127. * @param array $handles
  128. * @return void
  129. *
  130. * @throws \Magento\Framework\Exception\LocalizedException
  131. */
  132. private function getNotCacheableResult($catalogSearchHelper, $query, $handles)
  133. {
  134. if ($catalogSearchHelper->isMinQueryLength()) {
  135. $query->setId(0)->setIsActive(1)->setIsProcessed(1);
  136. } else {
  137. $query->saveIncrementalPopularity();
  138. $redirect = $query->getRedirect();
  139. if ($redirect && $this->_url->getCurrentUrl() !== $redirect) {
  140. $this->getResponse()->setRedirect($redirect);
  141. return;
  142. }
  143. }
  144. $catalogSearchHelper->checkNotes();
  145. $this->_view->loadLayout($handles);
  146. $this->getResponse()->setNoCacheHeaders();
  147. $this->_view->renderLayout();
  148. }
  149. }