Query.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Model;
  7. use Magento\Search\Model\ResourceModel\Query\Collection as QueryCollection;
  8. use Magento\Search\Model\ResourceModel\Query\CollectionFactory as QueryCollectionFactory;
  9. use Magento\Search\Model\SearchCollectionInterface as Collection;
  10. use Magento\Search\Model\SearchCollectionFactory as CollectionFactory;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Framework\Data\Collection\AbstractDb as DbCollection;
  13. use Magento\Framework\Model\AbstractModel;
  14. use Magento\Framework\Model\ResourceModel\AbstractResource;
  15. use Magento\Framework\Registry;
  16. use Magento\Store\Model\StoreManagerInterface;
  17. /**
  18. * Search query model
  19. *
  20. * @method \Magento\Search\Model\Query setQueryText(string $value)
  21. * @method int getNumResults()
  22. * @method \Magento\Search\Model\Query setNumResults(int $value)
  23. * @method int getPopularity()
  24. * @method \Magento\Search\Model\Query setPopularity(int $value)
  25. * @method string getRedirect()
  26. * @method \Magento\Search\Model\Query setRedirect(string $value)
  27. * @method int getDisplayInTerms()
  28. * @method \Magento\Search\Model\Query setDisplayInTerms(int $value)
  29. * @method \Magento\Search\Model\Query setQueryNameExceeded(bool $value)
  30. * @method int getIsActive()
  31. * @method \Magento\Search\Model\Query setIsActive(int $value)
  32. * @method int getIsProcessed()
  33. * @method \Magento\Search\Model\Query setIsProcessed(int $value)
  34. * @method string getUpdatedAt()
  35. * @method \Magento\Search\Model\Query setUpdatedAt(string $value)
  36. * @method \Magento\Search\Model\Query setIsQueryTextExceeded(bool $value)
  37. * @method \Magento\Search\Model\Query setIsQueryTextShort(bool $value)
  38. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  39. * @api
  40. * @since 100.0.2
  41. */
  42. class Query extends AbstractModel implements QueryInterface
  43. {
  44. /**
  45. * Event prefix
  46. *
  47. * @var string
  48. */
  49. protected $_eventPrefix = 'search_query';
  50. /**
  51. * Event object key name
  52. *
  53. * @var string
  54. */
  55. protected $_eventObject = 'search_query';
  56. const CACHE_TAG = 'SEARCH_QUERY';
  57. const XML_PATH_MIN_QUERY_LENGTH = 'catalog/search/min_query_length';
  58. const XML_PATH_MAX_QUERY_LENGTH = 'catalog/search/max_query_length';
  59. /**
  60. * Core store config
  61. *
  62. * @var ScopeConfigInterface
  63. */
  64. protected $_scopeConfig;
  65. /**
  66. * Store manager
  67. *
  68. * @var StoreManagerInterface
  69. */
  70. protected $_storeManager;
  71. /**
  72. * Search collection factory
  73. *
  74. * @var CollectionFactory
  75. */
  76. protected $_searchCollectionFactory;
  77. /**
  78. * Query collection factory
  79. *
  80. * @var QueryCollectionFactory
  81. */
  82. protected $_queryCollectionFactory;
  83. /**
  84. * Construct
  85. *
  86. * @param \Magento\Framework\Model\Context $context
  87. * @param Registry $registry
  88. * @param QueryCollectionFactory $queryCollectionFactory
  89. * @param CollectionFactory $searchCollectionFactory
  90. * @param StoreManagerInterface $storeManager
  91. * @param ScopeConfigInterface $scopeConfig
  92. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  93. * @param DbCollection $resourceCollection
  94. * @param array $data
  95. */
  96. public function __construct(
  97. \Magento\Framework\Model\Context $context,
  98. Registry $registry,
  99. QueryCollectionFactory $queryCollectionFactory,
  100. CollectionFactory $searchCollectionFactory,
  101. StoreManagerInterface $storeManager,
  102. ScopeConfigInterface $scopeConfig,
  103. AbstractResource $resource = null,
  104. DbCollection $resourceCollection = null,
  105. array $data = []
  106. ) {
  107. $this->_queryCollectionFactory = $queryCollectionFactory;
  108. $this->_searchCollectionFactory = $searchCollectionFactory;
  109. $this->_storeManager = $storeManager;
  110. $this->_scopeConfig = $scopeConfig;
  111. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  112. }
  113. /**
  114. * Init resource model
  115. *
  116. * @return void
  117. */
  118. protected function _construct()
  119. {
  120. $this->_init(\Magento\Search\Model\ResourceModel\Query::class);
  121. }
  122. /**
  123. * Retrieve search collection
  124. *
  125. * @return Collection
  126. */
  127. public function getSearchCollection()
  128. {
  129. return $this->_searchCollectionFactory->create();
  130. }
  131. /**
  132. * Retrieve collection of suggest queries
  133. *
  134. * @return QueryCollection
  135. * @throws \Magento\Framework\Exception\NoSuchEntityException
  136. */
  137. public function getSuggestCollection()
  138. {
  139. $collection = $this->getData('suggest_collection');
  140. if ($collection === null) {
  141. $collection = $this->_queryCollectionFactory->create()->setStoreId(
  142. $this->getStoreId()
  143. )->setQueryFilter(
  144. $this->getQueryText()
  145. );
  146. $this->setData('suggest_collection', $collection);
  147. }
  148. return $collection;
  149. }
  150. /**
  151. * Load Query object by query string
  152. *
  153. * @param string $text
  154. * @return $this
  155. * @throws \Magento\Framework\Exception\LocalizedException
  156. * @deprecated 100.1.0 "synonym for" feature has been removed
  157. */
  158. public function loadByQuery($text)
  159. {
  160. $this->loadByQueryText($text);
  161. return $this;
  162. }
  163. /**
  164. * Load Query object only by query text
  165. *
  166. * @param string $text
  167. * @return $this
  168. * @throws \Magento\Framework\Exception\LocalizedException
  169. */
  170. public function loadByQueryText($text)
  171. {
  172. $this->_getResource()->loadByQueryText($this, $text);
  173. $this->_afterLoad();
  174. $this->setOrigData();
  175. return $this;
  176. }
  177. /**
  178. * Set Store Id
  179. *
  180. * @param int $storeId
  181. * @return void
  182. */
  183. public function setStoreId($storeId)
  184. {
  185. $this->setData('store_id', $storeId);
  186. }
  187. /**
  188. * Retrieve store Id
  189. *
  190. * @return int
  191. * @throws \Magento\Framework\Exception\NoSuchEntityException
  192. */
  193. public function getStoreId()
  194. {
  195. if (!($storeId = $this->getData('store_id'))) {
  196. $storeId = $this->_storeManager->getStore()->getId();
  197. }
  198. return $storeId;
  199. }
  200. /**
  201. * Prepare save query for result
  202. *
  203. * @return $this
  204. * @throws \Exception
  205. */
  206. public function prepare()
  207. {
  208. if (!$this->getId()) {
  209. $this->setIsActive(0);
  210. $this->setIsProcessed(0);
  211. $this->save();
  212. $this->setIsActive(1);
  213. }
  214. return $this;
  215. }
  216. /**
  217. * Save query with incremental popularity
  218. *
  219. * @return $this
  220. *
  221. * @throws \Magento\Framework\Exception\LocalizedException
  222. */
  223. public function saveIncrementalPopularity()
  224. {
  225. $this->getResource()->saveIncrementalPopularity($this);
  226. return $this;
  227. }
  228. /**
  229. * Save query with number of results
  230. *
  231. * @param int $numResults
  232. * @return $this
  233. *
  234. * @throws \Magento\Framework\Exception\LocalizedException
  235. */
  236. public function saveNumResults($numResults)
  237. {
  238. $this->setNumResults($numResults);
  239. $this->getResource()->saveNumResults($this);
  240. return $this;
  241. }
  242. /**
  243. * Retrieve minimum query length
  244. *
  245. * @return int
  246. * @throws \Magento\Framework\Exception\NoSuchEntityException
  247. */
  248. public function getMinQueryLength()
  249. {
  250. return $this->_scopeConfig->getValue(
  251. self::XML_PATH_MIN_QUERY_LENGTH,
  252. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  253. $this->getStoreId()
  254. );
  255. }
  256. /**
  257. * Retrieve maximum query length
  258. *
  259. * @return int
  260. * @throws \Magento\Framework\Exception\NoSuchEntityException
  261. */
  262. public function getMaxQueryLength()
  263. {
  264. return $this->_scopeConfig->getValue(
  265. self::XML_PATH_MAX_QUERY_LENGTH,
  266. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  267. $this->getStoreId()
  268. );
  269. }
  270. /**
  271. * @inheritdoc
  272. */
  273. public function getQueryText()
  274. {
  275. return $this->getDataByKey('query_text');
  276. }
  277. /**
  278. * Check if query maximum length exceeded.
  279. *
  280. * @return bool
  281. * @codeCoverageIgnore
  282. */
  283. public function isQueryTextExceeded()
  284. {
  285. return $this->getData('is_query_text_exceeded');
  286. }
  287. /**
  288. * Check if minimum query length reached.
  289. *
  290. * @return bool
  291. * @codeCoverageIgnore
  292. * @since 100.1.0
  293. */
  294. public function isQueryTextShort()
  295. {
  296. return $this->getData('is_query_text_short');
  297. }
  298. }