Data.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Helper;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\App\Helper\AbstractHelper;
  9. use Magento\Framework\App\Helper\Context;
  10. use Magento\Framework\Escaper;
  11. use Magento\Framework\Stdlib\StringUtils;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. use Magento\Search\Model\Query as SearchQuery;
  14. use Magento\Search\Model\QueryFactory;
  15. /**
  16. * Search helper
  17. * @api
  18. * @since 100.0.2
  19. */
  20. class Data extends AbstractHelper
  21. {
  22. /**
  23. * Note messages
  24. *
  25. * @var array
  26. */
  27. protected $_messages = [];
  28. /**
  29. * Magento string lib
  30. *
  31. * @var String
  32. */
  33. protected $string;
  34. /**
  35. * Core store config
  36. *
  37. * @var ScopeConfigInterface
  38. * @since 100.1.0
  39. */
  40. protected $scopeConfig;
  41. /**
  42. * @var Escaper
  43. * @since 100.1.0
  44. */
  45. protected $escaper;
  46. /**
  47. * @var \Magento\Store\Model\StoreManagerInterface
  48. * @since 100.1.0
  49. */
  50. protected $storeManager;
  51. /**
  52. * Construct
  53. *
  54. * @param Context $context
  55. * @param StringUtils $string
  56. * @param Escaper $escaper
  57. * @param StoreManagerInterface $storeManager
  58. */
  59. public function __construct(
  60. Context $context,
  61. StringUtils $string,
  62. Escaper $escaper,
  63. StoreManagerInterface $storeManager
  64. ) {
  65. $this->string = $string;
  66. $this->scopeConfig = $context->getScopeConfig();
  67. $this->escaper = $escaper;
  68. $this->storeManager = $storeManager;
  69. parent::__construct($context);
  70. }
  71. /**
  72. * Is a minimum query length
  73. *
  74. * @return bool
  75. */
  76. public function isMinQueryLength()
  77. {
  78. $minQueryLength = $this->getMinQueryLength();
  79. $thisQueryLength = $this->string->strlen($this->getQueryText());
  80. return !$thisQueryLength || $minQueryLength !== '' && $thisQueryLength < $minQueryLength;
  81. }
  82. /**
  83. * Retrieve HTML escaped search query
  84. *
  85. * @return string
  86. */
  87. public function getEscapedQueryText()
  88. {
  89. return $this->escaper->escapeHtml(
  90. $this->getPreparedQueryText($this->getQueryText(), $this->getMaxQueryLength())
  91. );
  92. }
  93. /**
  94. * Retrieve result page url and set "secure" param to avoid confirm
  95. * message when we submit form from secure page to unsecure
  96. *
  97. * @param string $query
  98. * @return string
  99. */
  100. public function getResultUrl($query = null)
  101. {
  102. return $this->_getUrl(
  103. 'catalogsearch/result',
  104. ['_query' => [QueryFactory::QUERY_VAR_NAME => $query], '_secure' => $this->_request->isSecure()]
  105. );
  106. }
  107. /**
  108. * Retrieve suggest url
  109. *
  110. * @return string
  111. */
  112. public function getSuggestUrl()
  113. {
  114. return $this->_getUrl(
  115. 'search/ajax/suggest',
  116. ['_secure' => $this->_getRequest()->isSecure()]
  117. );
  118. }
  119. /**
  120. * Retrieve search term url
  121. *
  122. * @return string
  123. */
  124. public function getSearchTermUrl()
  125. {
  126. return $this->_getUrl('search/term/popular');
  127. }
  128. /**
  129. * Retrieve minimum query length
  130. *
  131. * @param mixed $store
  132. * @return int|string
  133. */
  134. public function getMinQueryLength($store = null)
  135. {
  136. return $this->scopeConfig->getValue(
  137. SearchQuery::XML_PATH_MIN_QUERY_LENGTH,
  138. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  139. $store
  140. );
  141. }
  142. /**
  143. * Retrieve maximum query length
  144. *
  145. * @param mixed $store
  146. * @return int|string
  147. */
  148. public function getMaxQueryLength($store = null)
  149. {
  150. return $this->scopeConfig->getValue(
  151. SearchQuery::XML_PATH_MAX_QUERY_LENGTH,
  152. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  153. $store
  154. );
  155. }
  156. /**
  157. * Add Note message
  158. *
  159. * @param string $message
  160. * @return $this
  161. */
  162. public function addNoteMessage($message)
  163. {
  164. $this->_messages[] = $message;
  165. return $this;
  166. }
  167. /**
  168. * Set Note messages
  169. *
  170. * @param array $messages
  171. * @return $this
  172. */
  173. public function setNoteMessages(array $messages)
  174. {
  175. $this->_messages = $messages;
  176. return $this;
  177. }
  178. /**
  179. * Retrieve Current Note messages
  180. *
  181. * @return array
  182. */
  183. public function getNoteMessages()
  184. {
  185. return $this->_messages;
  186. }
  187. /**
  188. * Check query of a warnings
  189. *
  190. * @param mixed $store
  191. * @return $this
  192. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  193. */
  194. public function checkNotes($store = null)
  195. {
  196. if ($this->isQueryTooLong($this->getQueryText(), $this->getMaxQueryLength())) {
  197. $this->addNoteMessage(
  198. __(
  199. 'Your search query can\'t be longer than %1, so we shortened your query.',
  200. $this->getMaxQueryLength()
  201. )
  202. );
  203. }
  204. return $this;
  205. }
  206. /**
  207. * @return string
  208. */
  209. public function getQueryParamName()
  210. {
  211. return QueryFactory::QUERY_VAR_NAME;
  212. }
  213. /**
  214. * @param string $queryText
  215. * @param int|string $maxQueryLength
  216. * @return bool
  217. */
  218. private function isQueryTooLong($queryText, $maxQueryLength)
  219. {
  220. return ($maxQueryLength !== '' && $this->string->strlen($queryText) > $maxQueryLength);
  221. }
  222. /**
  223. * Retrieve search query text
  224. *
  225. * @return string
  226. */
  227. private function getQueryText()
  228. {
  229. $queryText = $this->_request->getParam($this->getQueryParamName());
  230. return($queryText === null || is_array($queryText))
  231. ? ''
  232. : $this->string->cleanString(trim($queryText));
  233. }
  234. /**
  235. * @param string $queryText
  236. * @param int|string $maxQueryLength
  237. * @return string
  238. */
  239. private function getPreparedQueryText($queryText, $maxQueryLength)
  240. {
  241. if ($this->isQueryTooLong($queryText, $maxQueryLength)) {
  242. $queryText = $this->string->substr($queryText, 0, $maxQueryLength);
  243. }
  244. return $queryText;
  245. }
  246. }