Collection.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Model\ResourceModel\Rating\Option\Vote;
  7. /**
  8. * Rating votes collection
  9. *
  10. * @api
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @since 100.0.2
  13. */
  14. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  15. {
  16. /**
  17. * Store list manager
  18. *
  19. * @var \Magento\Store\Model\StoreManagerInterface
  20. */
  21. protected $_storeManager;
  22. /**
  23. * @var \Magento\Review\Model\ResourceModel\Rating\Option\CollectionFactory
  24. */
  25. protected $_ratingCollectionF;
  26. /**
  27. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  28. * @param \Psr\Log\LoggerInterface $logger
  29. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  30. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  31. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  32. * @param \Magento\Review\Model\ResourceModel\Rating\Option\CollectionFactory $ratingCollectionF
  33. * @param mixed $connection
  34. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  35. */
  36. public function __construct(
  37. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  38. \Psr\Log\LoggerInterface $logger,
  39. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  40. \Magento\Framework\Event\ManagerInterface $eventManager,
  41. \Magento\Store\Model\StoreManagerInterface $storeManager,
  42. \Magento\Review\Model\ResourceModel\Rating\Option\CollectionFactory $ratingCollectionF,
  43. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  44. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  45. ) {
  46. $this->_storeManager = $storeManager;
  47. $this->_ratingCollectionF = $ratingCollectionF;
  48. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  49. }
  50. /**
  51. * Define model
  52. *
  53. * @return void
  54. */
  55. protected function _construct()
  56. {
  57. $this->_init(
  58. \Magento\Review\Model\Rating\Option\Vote::class,
  59. \Magento\Review\Model\ResourceModel\Rating\Option\Vote::class
  60. );
  61. }
  62. /**
  63. * Set review filter
  64. *
  65. * @param int $reviewId
  66. * @return $this
  67. */
  68. public function setReviewFilter($reviewId)
  69. {
  70. $this->getSelect()->where("main_table.review_id = ?", $reviewId);
  71. return $this;
  72. }
  73. /**
  74. * Set EntityPk filter
  75. *
  76. * @param int $entityId
  77. * @return $this
  78. */
  79. public function setEntityPkFilter($entityId)
  80. {
  81. $this->getSelect()->where("entity_pk_value = ?", $entityId);
  82. return $this;
  83. }
  84. /**
  85. * Set store filter
  86. *
  87. * @param int $storeId
  88. * @return $this
  89. */
  90. public function setStoreFilter($storeId)
  91. {
  92. if ($this->_storeManager->isSingleStoreMode()) {
  93. return $this;
  94. }
  95. $this->getSelect()->join(
  96. ['rstore' => $this->getTable('review_store')],
  97. $this->getConnection()->quoteInto(
  98. 'main_table.review_id=rstore.review_id AND rstore.store_id=?',
  99. (int)$storeId
  100. ),
  101. []
  102. );
  103. return $this;
  104. }
  105. /**
  106. * Add rating info to select
  107. *
  108. * @param int $storeId
  109. * @return $this
  110. */
  111. public function addRatingInfo($storeId = null)
  112. {
  113. $connection = $this->getConnection();
  114. $ratingCodeCond = $connection->getIfNullSql('title.value', 'rating.rating_code');
  115. $this->getSelect()->join(
  116. ['rating' => $this->getTable('rating')],
  117. 'rating.rating_id = main_table.rating_id',
  118. ['rating_code']
  119. )->joinLeft(
  120. ['title' => $this->getTable('rating_title')],
  121. $connection->quoteInto(
  122. 'main_table.rating_id=title.rating_id AND title.store_id = ?',
  123. (int)$this->_storeManager->getStore()->getId()
  124. ),
  125. ['rating_code' => $ratingCodeCond]
  126. );
  127. if (!$this->_storeManager->isSingleStoreMode()) {
  128. if ($storeId == null) {
  129. $storeId = $this->_storeManager->getStore()->getId();
  130. }
  131. if (is_array($storeId)) {
  132. $condition = $connection->prepareSqlCondition('store.store_id', ['in' => $storeId]);
  133. } else {
  134. $condition = $connection->quoteInto('store.store_id = ?', $storeId);
  135. }
  136. $this->getSelect()->join(
  137. ['store' => $this->getTable('rating_store')],
  138. 'main_table.rating_id = store.rating_id AND ' . $condition
  139. );
  140. }
  141. $connection->fetchAll($this->getSelect());
  142. return $this;
  143. }
  144. /**
  145. * Add option info to select
  146. *
  147. * @return $this
  148. */
  149. public function addOptionInfo()
  150. {
  151. $this->getSelect()->join(
  152. ['rating_option' => $this->getTable('rating_option')],
  153. 'main_table.option_id = rating_option.option_id'
  154. );
  155. return $this;
  156. }
  157. /**
  158. * Add rating options
  159. *
  160. * @return $this
  161. */
  162. public function addRatingOptions()
  163. {
  164. if (!$this->getSize()) {
  165. return $this;
  166. }
  167. foreach ($this->getItems() as $item) {
  168. /** @var \Magento\Review\Model\ResourceModel\Rating\Option\Collection $options */
  169. $options = $this->_ratingCollectionF->create();
  170. $options->addRatingFilter($item->getRatingId())->load();
  171. if ($item->getRatingId()) {
  172. $item->setRatingOptions($options);
  173. } else {
  174. return $this;
  175. }
  176. }
  177. return $this;
  178. }
  179. }