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