Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SynonymReader.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
21  private $fullTextSelect;
22 
28  protected $storeManager;
29 
36  public function __construct(
37  \Magento\Framework\Model\ResourceModel\Db\Context $context,
39  \Magento\Framework\DB\Helper\Mysql\Fulltext $fulltext,
40  $connectionName = null
41  ) {
42  parent::__construct($context, $connectionName);
43  $this->fullTextSelect = $fulltext;
44  $this->storeManager = $storeManager;
45  }
46 
54  public function loadByPhrase(\Magento\Search\Model\SynonymReader $object, $phrase)
55  {
56  $rows = $this->queryByPhrase(strtolower($phrase));
57  $synsPerScope = $this->getSynRowsPerScope($rows);
58 
59  if (!empty($synsPerScope[\Magento\Store\Model\ScopeInterface::SCOPE_STORES])) {
60  $object->setData($synsPerScope[\Magento\Store\Model\ScopeInterface::SCOPE_STORES]);
61  } elseif (!empty($synsPerScope[\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES])) {
62  $object->setData($synsPerScope[\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES]);
63  } else {
64  $object->setData($synsPerScope[\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT]);
65  }
66  $this->_afterLoad($object);
67  return $this;
68  }
69 
75  protected function _construct()
76  {
77  $this->_init('search_synonyms', 'group_id');
78  }
79 
86  private function queryByPhrase($phrase)
87  {
88  $matchQuery = $this->fullTextSelect->getMatchQuery(
89  ['synonyms' => 'synonyms'],
90  $phrase,
92  );
93  $query = $this->getConnection()->select()->from(
94  $this->getMainTable()
95  )->where($matchQuery);
96 
97  return $this->getConnection()->fetchAll($query);
98  }
99 
106  private function getSynRowsPerScope($rows)
107  {
108  $synRowsForStoreView = [];
109  $synRowsForWebsite = [];
110  $synRowsForDefault = [];
111 
112  // The synonyms configured for current store view gets highest priority. Second highest is current website
113  // scope. If there were no store view and website specific synonyms then at last 'default' (All store views)
114  // will be considered.
115  foreach ($rows as $row) {
116  if ($this->isSynRowForStoreView($row)) {
117  // Check for current store view
118  $synRowsForStoreView[] = $row;
119  } elseif (empty($synRowsForStoreView) && $this->isSynRowForWebsite($row)) {
120  // Check for current website
121  $synRowsForWebsite[] = $row;
122  } elseif (empty($synRowsForStoreView)
123  && empty($synRowsForWebsite)
124  && $this->isSynRowForDefaultScope($row)) {
125  // Check for all store views (i.e. global/default config)
126  $synRowsForDefault[] = $row;
127  }
128  }
129  $synsPerScope[\Magento\Store\Model\ScopeInterface::SCOPE_STORES] = $synRowsForStoreView;
130  $synsPerScope[\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES] = $synRowsForWebsite;
131  $synsPerScope[\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT] = $synRowsForDefault;
132  return $synsPerScope;
133  }
134 
141  private function isSynRowForStoreView($row)
142  {
143  $storeViewId = $this->storeManager->getStore()->getId();
144  return ($row['store_id'] === $storeViewId);
145  }
146 
153  private function isSynRowForWebsite($row)
154  {
155  $websiteId = $this->storeManager->getStore()->getWebsiteId();
156  return (($row['website_id'] === $websiteId) && ($row['store_id'] == 0));
157  }
158 
165  private function isSynRowForDefaultScope($row)
166  {
167  return (($row['website_id'] == 0) && ($row['store_id'] == 0));
168  }
169 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
loadByPhrase(\Magento\Search\Model\SynonymReader $object, $phrase)
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\DB\Helper\Mysql\Fulltext $fulltext, $connectionName=null)
_afterLoad(\Magento\Framework\Model\AbstractModel $object)
Definition: AbstractDb.php:641