Term.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Search term block
  8. */
  9. namespace Magento\Search\Block;
  10. use Magento\Framework\UrlFactory;
  11. use Magento\Framework\UrlInterface;
  12. use Magento\Framework\View\Element\Template;
  13. use Magento\Framework\View\Element\Template\Context;
  14. use Magento\Search\Model\ResourceModel\Query\CollectionFactory;
  15. /**
  16. * @api
  17. * @since 100.0.2
  18. */
  19. class Term extends Template
  20. {
  21. /**
  22. * @var array
  23. */
  24. protected $_terms;
  25. /**
  26. * @var int
  27. */
  28. protected $_minPopularity;
  29. /**
  30. * @var int
  31. */
  32. protected $_maxPopularity;
  33. /**
  34. * Url factory
  35. *
  36. * @var UrlFactory
  37. */
  38. protected $_urlFactory;
  39. /**
  40. * Query collection factory
  41. *
  42. * @var CollectionFactory
  43. */
  44. protected $_queryCollectionFactory;
  45. /**
  46. * @param Context $context
  47. * @param CollectionFactory $queryCollectionFactory
  48. * @param UrlFactory $urlFactory
  49. * @param array $data
  50. */
  51. public function __construct(
  52. Context $context,
  53. CollectionFactory $queryCollectionFactory,
  54. UrlFactory $urlFactory,
  55. array $data = []
  56. ) {
  57. $this->_queryCollectionFactory = $queryCollectionFactory;
  58. $this->_urlFactory = $urlFactory;
  59. parent::__construct($context, $data);
  60. }
  61. /**
  62. * Load terms and try to sort it by names
  63. *
  64. * @return $this
  65. * @throws \Magento\Framework\Exception\NoSuchEntityException
  66. */
  67. protected function _loadTerms()
  68. {
  69. if (empty($this->_terms)) {
  70. $this->_terms = [];
  71. $terms = $this->_queryCollectionFactory->create()->setPopularQueryFilter(
  72. $this->_storeManager->getStore()->getId()
  73. )->setPageSize(
  74. 100
  75. )->load()->getItems();
  76. if (count($terms) == 0) {
  77. return $this;
  78. }
  79. $this->_maxPopularity = reset($terms)->getPopularity();
  80. $this->_minPopularity = end($terms)->getPopularity();
  81. $range = $this->_maxPopularity - $this->_minPopularity;
  82. $range = $range == 0 ? 1 : $range;
  83. foreach ($terms as $term) {
  84. if (!$term->getPopularity()) {
  85. continue;
  86. }
  87. $term->setRatio(($term->getPopularity() - $this->_minPopularity) / $range);
  88. $temp[$term->getQueryText()] = $term;
  89. $termKeys[] = $term->getQueryText();
  90. }
  91. natcasesort($termKeys);
  92. foreach ($termKeys as $termKey) {
  93. $this->_terms[$termKey] = $temp[$termKey];
  94. }
  95. }
  96. return $this;
  97. }
  98. /**
  99. * @return array
  100. * @throws \Magento\Framework\Exception\NoSuchEntityException
  101. */
  102. public function getTerms()
  103. {
  104. $this->_loadTerms();
  105. return $this->_terms;
  106. }
  107. /**
  108. * @param \Magento\Framework\DataObject $obj
  109. * @return string
  110. */
  111. public function getSearchUrl($obj)
  112. {
  113. /** @var $url UrlInterface */
  114. $url = $this->_urlFactory->create();
  115. /*
  116. * url encoding will be done in Url.php http_build_query
  117. * so no need to explicitly called urlencode for the text
  118. */
  119. $url->setQueryParam('q', $obj->getQueryText());
  120. return $url->getUrl('catalogsearch/result');
  121. }
  122. /**
  123. * @return int
  124. */
  125. public function getMaxPopularity()
  126. {
  127. return $this->_maxPopularity;
  128. }
  129. /**
  130. * @return int
  131. */
  132. public function getMinPopularity()
  133. {
  134. return $this->_minPopularity;
  135. }
  136. }