Collection.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Model\ResourceModel\Query;
  7. use Magento\Store\Model\Store;
  8. /**
  9. * Search query collection
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  15. {
  16. /**
  17. * Store for filter
  18. *
  19. * @var int
  20. */
  21. protected $_storeId;
  22. /**
  23. * Store manager
  24. *
  25. * @var \Magento\Store\Model\StoreManagerInterface
  26. */
  27. protected $_storeManager;
  28. /**
  29. * Search resource helper
  30. *
  31. * @var \Magento\Framework\DB\Helper
  32. */
  33. protected $_resourceHelper;
  34. /**
  35. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  36. * @param \Psr\Log\LoggerInterface $logger
  37. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  38. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  39. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  40. * @param \Magento\Framework\DB\Helper $resourceHelper
  41. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  42. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  43. */
  44. public function __construct(
  45. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  46. \Psr\Log\LoggerInterface $logger,
  47. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  48. \Magento\Framework\Event\ManagerInterface $eventManager,
  49. \Magento\Store\Model\StoreManagerInterface $storeManager,
  50. \Magento\Framework\DB\Helper $resourceHelper,
  51. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  52. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  53. ) {
  54. $this->_storeManager = $storeManager;
  55. $this->_resourceHelper = $resourceHelper;
  56. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  57. }
  58. /**
  59. * Init model for collection
  60. *
  61. * @return void
  62. */
  63. protected function _construct()
  64. {
  65. $this->_init(\Magento\Search\Model\Query::class, \Magento\Search\Model\ResourceModel\Query::class);
  66. }
  67. /**
  68. * Set Store ID for filter
  69. *
  70. * @param Store|int $store
  71. * @return $this
  72. */
  73. public function setStoreId($store)
  74. {
  75. if ($store instanceof Store) {
  76. $store = $store->getId();
  77. }
  78. $this->_storeId = $store;
  79. return $this;
  80. }
  81. /**
  82. * Retrieve Store ID Filter
  83. *
  84. * @return int|null
  85. */
  86. public function getStoreId()
  87. {
  88. return $this->_storeId;
  89. }
  90. /**
  91. * Set search query text to filter
  92. *
  93. * @param string $query
  94. * @return $this
  95. */
  96. public function setQueryFilter($query)
  97. {
  98. $this->getSelect()->reset(
  99. \Magento\Framework\DB\Select::FROM
  100. )->distinct(
  101. true
  102. )->from(
  103. ['main_table' => $this->getTable('search_query')]
  104. )->where(
  105. 'num_results > 0 AND display_in_terms = 1 AND query_text LIKE ?',
  106. $this->_resourceHelper->addLikeEscape($query, ['position' => 'start'])
  107. )->order(
  108. 'popularity ' . \Magento\Framework\DB\Select::SQL_DESC
  109. );
  110. if ($this->getStoreId()) {
  111. $this->getSelect()->where('store_id = ?', (int)$this->getStoreId());
  112. }
  113. return $this;
  114. }
  115. /**
  116. * Set Popular Search Query Filter
  117. *
  118. * @param int|array $storeIds
  119. * @return $this
  120. */
  121. public function setPopularQueryFilter($storeIds = null)
  122. {
  123. $this->getSelect()->reset(
  124. \Magento\Framework\DB\Select::FROM
  125. )->reset(
  126. \Magento\Framework\DB\Select::COLUMNS
  127. )->distinct(
  128. true
  129. )->from(
  130. ['main_table' => $this->getTable('search_query')]
  131. );
  132. if ($storeIds) {
  133. $this->addStoreFilter($storeIds);
  134. $this->getSelect()->where('num_results > 0');
  135. } elseif (null === $storeIds) {
  136. $this->addStoreFilter($this->_storeManager->getStore()->getId());
  137. $this->getSelect()->where('num_results > 0');
  138. }
  139. $this->getSelect()->order(['popularity desc']);
  140. return $this;
  141. }
  142. /**
  143. * Set Recent Queries Order
  144. *
  145. * @return $this
  146. */
  147. public function setRecentQueryFilter()
  148. {
  149. $this->setOrder('updated_at', 'desc');
  150. return $this;
  151. }
  152. /**
  153. * Filter collection by specified store ids
  154. *
  155. * @param array|int $storeIds
  156. * @return $this
  157. */
  158. public function addStoreFilter($storeIds)
  159. {
  160. if (!is_array($storeIds)) {
  161. $storeIds = [$storeIds];
  162. }
  163. $this->getSelect()->where('main_table.store_id IN (?)', $storeIds);
  164. return $this;
  165. }
  166. }