PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-review/Model/ResourceModel/Review/Collection.php

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