Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
17 {
21  protected $_storeManager;
22 
27 
32  protected $_addStoreDataFlag = false;
33 
44  public function __construct(
45  \Magento\Framework\Data\Collection\EntityFactory $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\Review\Model\ResourceModel\Rating\Option\CollectionFactory $ratingCollectionF,
51  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
52  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
53  ) {
54  $this->_storeManager = $storeManager;
55  $this->_ratingCollectionF = $ratingCollectionF;
56  parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
57  }
58 
62  protected $_isStoreJoined = false;
63 
69  protected function _construct()
70  {
71  $this->_init(\Magento\Review\Model\Rating::class, \Magento\Review\Model\ResourceModel\Rating::class);
72  }
73 
80  public function addEntityFilter($entity)
81  {
82  $connection = $this->getConnection();
83 
84  $this->getSelect()->join(
85  $this->getTable('rating_entity'),
86  'main_table.entity_id=' . $this->getTable('rating_entity') . '.entity_id',
87  ['entity_code']
88  );
89 
90  if (is_numeric($entity)) {
91  $this->addFilter(
92  'entity',
93  $connection->quoteInto($this->getTable('rating_entity') . '.entity_id=?', $entity),
94  'string'
95  );
96  } elseif (is_string($entity)) {
97  $this->addFilter(
98  'entity',
99  $connection->quoteInto($this->getTable('rating_entity') . '.entity_code=?', $entity),
100  'string'
101  );
102  }
103  return $this;
104  }
105 
112  public function setPositionOrder($dir = 'ASC')
113  {
114  $this->setOrder('main_table.position', $dir);
115  return $this;
116  }
117 
124  public function setStoreFilter($storeId)
125  {
126  if ($this->_storeManager->isSingleStoreMode()) {
127  return $this;
128  }
129  $connection = $this->getConnection();
130  if (!is_array($storeId)) {
131  $storeId = [$storeId === null ? -1 : $storeId];
132  }
133  if (empty($storeId)) {
134  return $this;
135  }
136  if (!$this->_isStoreJoined) {
137  $this->getSelect()->distinct(
138  true
139  )->join(
140  ['store' => $this->getTable('rating_store')],
141  'main_table.rating_id = store.rating_id',
142  []
143  );
144  $this->_isStoreJoined = true;
145  }
146  $inCondition = $connection->prepareSqlCondition('store.store_id', ['in' => $storeId]);
147  $this->getSelect()->where($inCondition);
148  $this->setPositionOrder();
149  return $this;
150  }
151 
157  public function addOptionToItems()
158  {
159  $arrRatingId = $this->getColumnValues('rating_id');
160 
161  if (!empty($arrRatingId)) {
163  $collection = $this->_ratingCollectionF->create()->addRatingFilter(
164  $arrRatingId
165  )->setPositionOrder()->load();
166 
167  foreach ($this as $rating) {
168  $rating->setOptions($collection->getItemsByColumnValue('rating_id', $rating->getId()));
169  }
170  }
171 
172  return $this;
173  }
174 
182  public function addEntitySummaryToItem($entityPkValue, $storeId)
183  {
184  $arrRatingId = $this->getColumnValues('rating_id');
185  if (count($arrRatingId) == 0) {
186  return $this;
187  }
188 
189  $connection = $this->getConnection();
190 
191  $inCond = $connection->prepareSqlCondition('rating_option_vote.rating_id', ['in' => $arrRatingId]);
192  $sumCond = new \Zend_Db_Expr("SUM(rating_option_vote.{$connection->quoteIdentifier('percent')})");
193  $countCond = new \Zend_Db_Expr('COUNT(*)');
194  $select = $connection->select()->from(
195  ['rating_option_vote' => $this->getTable('rating_option_vote')],
196  ['rating_id' => 'rating_option_vote.rating_id', 'sum' => $sumCond, 'count' => $countCond]
197  )->join(
198  ['review_store' => $this->getTable('review_store')],
199  'rating_option_vote.review_id=review_store.review_id AND review_store.store_id = :store_id',
200  []
201  );
202  if (!$this->_storeManager->isSingleStoreMode()) {
203  $select->join(
204  ['rst' => $this->getTable('rating_store')],
205  'rst.rating_id = rating_option_vote.rating_id AND rst.store_id = :rst_store_id',
206  []
207  );
208  }
209  $select->join(
210  ['review' => $this->getTable('review')],
211  'review_store.review_id=review.review_id AND review.status_id=1',
212  []
213  )->where(
214  $inCond
215  )->where(
216  'rating_option_vote.entity_pk_value=:pk_value'
217  )->group(
218  'rating_option_vote.rating_id'
219  );
220  $bind = [':store_id' => (int)$storeId, ':pk_value' => $entityPkValue];
221  if (!$this->_storeManager->isSingleStoreMode()) {
222  $bind[':rst_store_id'] = (int)$storeId;
223  }
224 
225  $data = $this->getConnection()->fetchAll($select, $bind);
226 
227  foreach ($data as $item) {
228  $rating = $this->getItemById($item['rating_id']);
229  if ($rating && $item['count'] > 0) {
230  $rating->setSummary($item['sum'] / $item['count']);
231  }
232  }
233  return $this;
234  }
235 
243  {
244  $connection = $this->getConnection();
245  $ratingCodeCond = $connection->getIfNullSql('title.value', 'main_table.rating_code');
246  $this->getSelect()->joinLeft(
247  ['title' => $this->getTable('rating_title')],
248  $connection->quoteInto('main_table.rating_id=title.rating_id AND title.store_id = ?', (int)$storeId),
249  ['rating_code' => $ratingCodeCond]
250  );
251  return $this;
252  }
253 
259  public function addStoreData()
260  {
261  if (!$this->_storeManager->isSingleStoreMode()) {
262  if (!$this->_isCollectionLoaded) {
263  $this->_addStoreDataFlag = true;
264  } elseif (!$this->_addStoreDataFlag) {
265  $this->_addStoreData();
266  }
267  }
268 
269  return $this;
270  }
271 
279  public function load($printQuery = false, $logQuery = false)
280  {
281  if ($this->isLoaded()) {
282  return $this;
283  }
284  $this->_eventManager->dispatch('rating_rating_collection_load_before', ['collection' => $this]);
285  parent::load($printQuery, $logQuery);
286  if ($this->_addStoreDataFlag) {
287  $this->_addStoreData();
288  }
289  return $this;
290  }
291 
297  protected function _addStoreData()
298  {
299  $ratingIds = [];
300  foreach ($this as $item) {
301  $ratingIds[] = $item->getId();
302  $item->setStores([]);
303  }
304  if (!$ratingIds) {
305  return $this;
306  }
307  $connection = $this->getConnection();
308 
309  $inCondition = $connection->prepareSqlCondition('rating_id', ['in' => $ratingIds]);
310 
311  $this->_select = $connection->select()->from($this->getTable('rating_store'))->where($inCondition);
312 
313  $data = $connection->fetchAll($this->_select);
314  if (is_array($data) && count($data) > 0) {
315  foreach ($data as $row) {
316  $item = $this->getItemById($row['rating_id']);
317  $item->setStores(array_merge($item->getStores(), [$row['store_id']]));
318  }
319  }
320  return $this;
321  }
322 
329  public function setActiveFilter($isActive = true)
330  {
331  $this->getSelect()->where('main_table.is_active=?', $isActive);
332  return $this;
333  }
334 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Review\Model\ResourceModel\Rating\Option\CollectionFactory $ratingCollectionF, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null)
Definition: Collection.php:44
$storeManager
$resource
Definition: bulk.php:12
$rating
Definition: item.phtml:18
$logger
addFilter($field, $value, $type='and')
Definition: Collection.php:118
$entity
Definition: element.phtml:22
load($printQuery=false, $logQuery=false)
Definition: Collection.php:279
setOrder($field, $direction=self::SORT_ORDER_DESC)
Definition: AbstractDb.php:274
$connection
Definition: bulk.php:13