Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Term.php
Go to the documentation of this file.
1 <?php
10 namespace Magento\Search\Block;
11 
16 use Magento\Search\Model\ResourceModel\Query\CollectionFactory;
17 
22 class Term extends Template
23 {
27  protected $_terms;
28 
32  protected $_minPopularity;
33 
37  protected $_maxPopularity;
38 
44  protected $_urlFactory;
45 
52 
59  public function __construct(
60  Context $context,
61  CollectionFactory $queryCollectionFactory,
62  UrlFactory $urlFactory,
63  array $data = []
64  ) {
65  $this->_queryCollectionFactory = $queryCollectionFactory;
66  $this->_urlFactory = $urlFactory;
67  parent::__construct($context, $data);
68  }
69 
76  protected function _loadTerms()
77  {
78  if (empty($this->_terms)) {
79  $this->_terms = [];
80  $terms = $this->_queryCollectionFactory->create()->setPopularQueryFilter(
81  $this->_storeManager->getStore()->getId()
82  )->setPageSize(
83  100
84  )->load()->getItems();
85 
86  if (count($terms) == 0) {
87  return $this;
88  }
89 
90  $this->_maxPopularity = reset($terms)->getPopularity();
91  $this->_minPopularity = end($terms)->getPopularity();
92  $range = $this->_maxPopularity - $this->_minPopularity;
93  $range = $range == 0 ? 1 : $range;
94  foreach ($terms as $term) {
95  if (!$term->getPopularity()) {
96  continue;
97  }
98  $term->setRatio(($term->getPopularity() - $this->_minPopularity) / $range);
99  $temp[$term->getQueryText()] = $term;
100  $termKeys[] = $term->getQueryText();
101  }
102  natcasesort($termKeys);
103 
104  foreach ($termKeys as $termKey) {
105  $this->_terms[$termKey] = $temp[$termKey];
106  }
107  }
108  return $this;
109  }
110 
115  public function getTerms()
116  {
117  $this->_loadTerms();
118  return $this->_terms;
119  }
120 
125  public function getSearchUrl($obj)
126  {
128  $url = $this->_urlFactory->create();
129  /*
130  * url encoding will be done in Url.php http_build_query
131  * so no need to explicitly called urlencode for the text
132  */
133  $url->setQueryParam('q', $obj->getQueryText());
134  return $url->getUrl('catalogsearch/result');
135  }
136 
140  public function getMaxPopularity()
141  {
142  return $this->_maxPopularity;
143  }
144 
148  public function getMinPopularity()
149  {
150  return $this->_minPopularity;
151  }
152 }
__construct(Context $context, CollectionFactory $queryCollectionFactory, UrlFactory $urlFactory, array $data=[])
Definition: Term.php:59