Collection.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Model\ResourceModel\Review;
  7. /**
  8. * Review collection resource model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  14. {
  15. /**
  16. * Review table
  17. *
  18. * @var string
  19. */
  20. protected $_reviewTable = null;
  21. /**
  22. * Review detail table
  23. *
  24. * @var string
  25. */
  26. protected $_reviewDetailTable = null;
  27. /**
  28. * Review status table
  29. *
  30. * @var string
  31. */
  32. protected $_reviewStatusTable = null;
  33. /**
  34. * Review entity table
  35. *
  36. * @var string
  37. */
  38. protected $_reviewEntityTable = null;
  39. /**
  40. * Review store table
  41. *
  42. * @var string
  43. */
  44. protected $_reviewStoreTable = null;
  45. /**
  46. * Add store data flag
  47. * @var bool
  48. */
  49. protected $_addStoreDataFlag = false;
  50. /**
  51. * Review data
  52. *
  53. * @var \Magento\Review\Helper\Data
  54. */
  55. protected $_reviewData = null;
  56. /**
  57. * Rating option model
  58. *
  59. * @var \Magento\Review\Model\Rating\Option\VoteFactory
  60. */
  61. protected $_voteFactory;
  62. /**
  63. * Core model store manager interface
  64. *
  65. * @var \Magento\Store\Model\StoreManagerInterface
  66. */
  67. protected $_storeManager;
  68. /**
  69. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  70. * @param \Psr\Log\LoggerInterface $logger
  71. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  72. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  73. * @param \Magento\Review\Helper\Data $reviewData
  74. * @param \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory
  75. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  76. * @param mixed $connection
  77. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  78. */
  79. public function __construct(
  80. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  81. \Psr\Log\LoggerInterface $logger,
  82. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  83. \Magento\Framework\Event\ManagerInterface $eventManager,
  84. \Magento\Review\Helper\Data $reviewData,
  85. \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory,
  86. \Magento\Store\Model\StoreManagerInterface $storeManager,
  87. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  88. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  89. ) {
  90. $this->_reviewData = $reviewData;
  91. $this->_voteFactory = $voteFactory;
  92. $this->_storeManager = $storeManager;
  93. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  94. }
  95. /**
  96. * Define module
  97. *
  98. * @return void
  99. */
  100. protected function _construct()
  101. {
  102. $this->_init(\Magento\Review\Model\Review::class, \Magento\Review\Model\ResourceModel\Review::class);
  103. }
  104. /**
  105. * Initialize select
  106. *
  107. * @return $this
  108. */
  109. protected function _initSelect()
  110. {
  111. parent::_initSelect();
  112. $this->getSelect()->join(
  113. ['detail' => $this->getReviewDetailTable()],
  114. 'main_table.review_id = detail.review_id',
  115. ['detail_id', 'title', 'detail', 'nickname', 'customer_id']
  116. );
  117. return $this;
  118. }
  119. /**
  120. * Add customer filter
  121. *
  122. * @param int|string $customerId
  123. * @return $this
  124. */
  125. public function addCustomerFilter($customerId)
  126. {
  127. $this->addFilter('customer', $this->getConnection()->quoteInto('detail.customer_id=?', $customerId), 'string');
  128. return $this;
  129. }
  130. /**
  131. * Add store filter
  132. *
  133. * @param int|int[] $storeId
  134. * @return $this
  135. */
  136. public function addStoreFilter($storeId)
  137. {
  138. $inCond = $this->getConnection()->prepareSqlCondition('store.store_id', ['in' => $storeId]);
  139. $this->getSelect()->join(
  140. ['store' => $this->getReviewStoreTable()],
  141. 'main_table.review_id=store.review_id',
  142. []
  143. );
  144. $this->getSelect()->where($inCond);
  145. return $this;
  146. }
  147. /**
  148. * Add stores data
  149. *
  150. * @return $this
  151. */
  152. public function addStoreData()
  153. {
  154. $this->_addStoreDataFlag = true;
  155. return $this;
  156. }
  157. /**
  158. * Add entity filter
  159. *
  160. * @param int|string $entity
  161. * @param int $pkValue
  162. * @return $this
  163. */
  164. public function addEntityFilter($entity, $pkValue)
  165. {
  166. $reviewEntityTable = $this->getReviewEntityTable();
  167. if (is_numeric($entity)) {
  168. $this->addFilter('entity', $this->getConnection()->quoteInto('main_table.entity_id=?', $entity), 'string');
  169. } elseif (is_string($entity)) {
  170. $this->_select->join(
  171. $reviewEntityTable,
  172. 'main_table.entity_id=' . $reviewEntityTable . '.entity_id',
  173. ['entity_code']
  174. );
  175. $this->addFilter(
  176. 'entity',
  177. $this->getConnection()->quoteInto($reviewEntityTable . '.entity_code=?', $entity),
  178. 'string'
  179. );
  180. }
  181. $this->addFilter(
  182. 'entity_pk_value',
  183. $this->getConnection()->quoteInto('main_table.entity_pk_value=?', $pkValue),
  184. 'string'
  185. );
  186. return $this;
  187. }
  188. /**
  189. * Add status filter
  190. *
  191. * @param int|string $status
  192. * @return $this
  193. */
  194. public function addStatusFilter($status)
  195. {
  196. if (is_string($status)) {
  197. $statuses = array_flip($this->_reviewData->getReviewStatuses());
  198. $status = isset($statuses[$status]) ? $statuses[$status] : 0;
  199. }
  200. if (is_numeric($status)) {
  201. $this->addFilter('status', $this->getConnection()->quoteInto('main_table.status_id=?', $status), 'string');
  202. }
  203. return $this;
  204. }
  205. /**
  206. * Set date order
  207. *
  208. * @param string $dir
  209. * @return $this
  210. */
  211. public function setDateOrder($dir = 'DESC')
  212. {
  213. $this->setOrder('main_table.created_at', $dir);
  214. return $this;
  215. }
  216. /**
  217. * Add rate votes
  218. *
  219. * @return $this
  220. */
  221. public function addRateVotes()
  222. {
  223. foreach ($this->getItems() as $item) {
  224. $votesCollection = $this->_voteFactory->create()->getResourceCollection()->setReviewFilter(
  225. $item->getId()
  226. )->setStoreFilter(
  227. $this->_storeManager->getStore()->getId()
  228. )->addRatingInfo(
  229. $this->_storeManager->getStore()->getId()
  230. )->load();
  231. $item->setRatingVotes($votesCollection);
  232. }
  233. return $this;
  234. }
  235. /**
  236. * Add reviews total count
  237. *
  238. * @return $this
  239. */
  240. public function addReviewsTotalCount()
  241. {
  242. $this->_select->joinLeft(
  243. ['r' => $this->getReviewTable()],
  244. 'main_table.entity_pk_value = r.entity_pk_value',
  245. ['total_reviews' => new \Zend_Db_Expr('COUNT(r.review_id)')]
  246. )->group(
  247. 'main_table.review_id'
  248. );
  249. return $this;
  250. }
  251. /**
  252. * Load data
  253. *
  254. * @param boolean $printQuery
  255. * @param boolean $logQuery
  256. * @return $this
  257. */
  258. public function load($printQuery = false, $logQuery = false)
  259. {
  260. if ($this->isLoaded()) {
  261. return $this;
  262. }
  263. $this->_eventManager->dispatch('review_review_collection_load_before', ['collection' => $this]);
  264. parent::load($printQuery, $logQuery);
  265. if ($this->_addStoreDataFlag) {
  266. $this->_addStoreData();
  267. }
  268. return $this;
  269. }
  270. /**
  271. * Add store data
  272. *
  273. * @return void
  274. */
  275. protected function _addStoreData()
  276. {
  277. $connection = $this->getConnection();
  278. $reviewsIds = $this->getColumnValues('review_id');
  279. $storesToReviews = [];
  280. if (count($reviewsIds) > 0) {
  281. $inCond = $connection->prepareSqlCondition('review_id', ['in' => $reviewsIds]);
  282. $select = $connection->select()->from($this->getReviewStoreTable())->where($inCond);
  283. $result = $connection->fetchAll($select);
  284. foreach ($result as $row) {
  285. if (!isset($storesToReviews[$row['review_id']])) {
  286. $storesToReviews[$row['review_id']] = [];
  287. }
  288. $storesToReviews[$row['review_id']][] = $row['store_id'];
  289. }
  290. }
  291. foreach ($this as $item) {
  292. if (isset($storesToReviews[$item->getId()])) {
  293. $item->setStores($storesToReviews[$item->getId()]);
  294. } else {
  295. $item->setStores([]);
  296. }
  297. }
  298. }
  299. /**
  300. * Get review table
  301. *
  302. * @return string
  303. */
  304. protected function getReviewTable()
  305. {
  306. if ($this->_reviewTable === null) {
  307. $this->_reviewTable = $this->getTable('review');
  308. }
  309. return $this->_reviewTable;
  310. }
  311. /**
  312. * Get review detail table
  313. *
  314. * @return string
  315. */
  316. protected function getReviewDetailTable()
  317. {
  318. if ($this->_reviewDetailTable === null) {
  319. $this->_reviewDetailTable = $this->getTable('review_detail');
  320. }
  321. return $this->_reviewDetailTable;
  322. }
  323. /**
  324. * Get review status table
  325. *
  326. * @return string
  327. */
  328. protected function getReviewStatusTable()
  329. {
  330. if ($this->_reviewStatusTable === null) {
  331. $this->_reviewStatusTable = $this->getTable('review_status');
  332. }
  333. return $this->_reviewStatusTable;
  334. }
  335. /**
  336. * Get review entity table
  337. *
  338. * @return string
  339. */
  340. protected function getReviewEntityTable()
  341. {
  342. if ($this->_reviewEntityTable === null) {
  343. $this->_reviewEntityTable = $this->getTable('review_entity');
  344. }
  345. return $this->_reviewEntityTable;
  346. }
  347. /**
  348. * Get review store table
  349. *
  350. * @return string
  351. */
  352. protected function getReviewStoreTable()
  353. {
  354. if ($this->_reviewStoreTable === null) {
  355. $this->_reviewStoreTable = $this->getTable('review_store');
  356. }
  357. return $this->_reviewStoreTable;
  358. }
  359. }