/
Data.php
264 lines (241 loc) · 6.05 KB
/
Data.php
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Search\Helper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Escaper;
use Magento\Framework\Stdlib\StringUtils;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Search\Model\Query as SearchQuery;
use Magento\Search\Model\QueryFactory;
/**
* Search helper
*/
class Data extends AbstractHelper
{
/**
* Note messages
*
* @var array
*/
protected $_messages = [];
/**
* Magento string lib
*
* @var String
*/
protected $string;
/**
* Core store config
*
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var Escaper
*/
protected $escaper;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* Construct
*
* @param Context $context
* @param StringUtils $string
* @param Escaper $escaper
* @param StoreManagerInterface $storeManager
*/
public function __construct(
Context $context,
StringUtils $string,
Escaper $escaper,
StoreManagerInterface $storeManager
) {
$this->string = $string;
$this->scopeConfig = $context->getScopeConfig();
$this->escaper = $escaper;
$this->storeManager = $storeManager;
parent::__construct($context);
}
/**
* Is a minimum query length
*
* @return bool
*/
public function isMinQueryLength()
{
$minQueryLength = $this->getMinQueryLength();
$thisQueryLength = $this->string->strlen($this->getQueryText());
return !$thisQueryLength || $minQueryLength !== '' && $thisQueryLength < $minQueryLength;
}
/**
* Retrieve HTML escaped search query
*
* @return string
*/
public function getEscapedQueryText()
{
return $this->escaper->escapeHtml(
$this->getPreparedQueryText($this->getQueryText(), $this->getMaxQueryLength())
);
}
/**
* Retrieve result page url and set "secure" param to avoid confirm
* message when we submit form from secure page to unsecure
*
* @param string $query
* @return string
*/
public function getResultUrl($query = null)
{
return $this->_getUrl(
'catalogsearch/result',
['_query' => [QueryFactory::QUERY_VAR_NAME => $query], '_secure' => $this->_request->isSecure()]
);
}
/**
* Retrieve suggest url
*
* @return string
*/
public function getSuggestUrl()
{
return $this->_getUrl(
'search/ajax/suggest',
['_secure' => $this->storeManager->getStore()->isCurrentlySecure()]
);
}
/**
* Retrieve search term url
*
* @return string
*/
public function getSearchTermUrl()
{
return $this->_getUrl('search/term/popular');
}
/**
* Retrieve minimum query length
*
* @param mixed $store
* @return int|string
*/
public function getMinQueryLength($store = null)
{
return $this->scopeConfig->getValue(
SearchQuery::XML_PATH_MIN_QUERY_LENGTH,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
);
}
/**
* Retrieve maximum query length
*
* @param mixed $store
* @return int|string
*/
public function getMaxQueryLength($store = null)
{
return $this->scopeConfig->getValue(
SearchQuery::XML_PATH_MAX_QUERY_LENGTH,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
);
}
/**
* Add Note message
*
* @param string $message
* @return $this
*/
public function addNoteMessage($message)
{
$this->_messages[] = $message;
return $this;
}
/**
* Set Note messages
*
* @param array $messages
* @return $this
*/
public function setNoteMessages(array $messages)
{
$this->_messages = $messages;
return $this;
}
/**
* Retrieve Current Note messages
*
* @return array
*/
public function getNoteMessages()
{
return $this->_messages;
}
/**
* Check query of a warnings
*
* @param mixed $store
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function checkNotes($store = null)
{
if ($this->isQueryTooLong($this->getQueryText(), $this->getMaxQueryLength())) {
$this->addNoteMessage(
__(
'Your search query can\'t be longer than %1, so we shortened your query.',
$this->getMaxQueryLength()
)
);
}
return $this;
}
/**
* @return string
*/
public function getQueryParamName()
{
return QueryFactory::QUERY_VAR_NAME;
}
/**
* @param string $queryText
* @param int|string $maxQueryLength
* @return bool
*/
private function isQueryTooLong($queryText, $maxQueryLength)
{
return ($maxQueryLength !== '' && $this->string->strlen($queryText) > $maxQueryLength);
}
/**
* Retrieve search query text
*
* @return string
*/
private function getQueryText()
{
$queryText = $this->_request->getParam($this->getQueryParamName());
return($queryText === null || is_array($queryText))
? ''
: $this->string->cleanString(trim($queryText));
}
/**
* @param string $queryText
* @param int|string $maxQueryLength
* @return string
*/
private function getPreparedQueryText($queryText, $maxQueryLength)
{
if ($this->isQueryTooLong($queryText, $maxQueryLength)) {
$queryText = $this->string->substr($queryText, 0, $maxQueryLength);
}
return $queryText;
}
}