Review.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Model;
  7. use Magento\Framework\DataObject;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Framework\DataObject\IdentityInterface;
  10. use Magento\Review\Model\ResourceModel\Review\Product\Collection as ProductCollection;
  11. use Magento\Review\Model\ResourceModel\Review\Status\Collection as StatusCollection;
  12. /**
  13. * Review model
  14. *
  15. * @api
  16. * @method string getCreatedAt()
  17. * @method \Magento\Review\Model\Review setCreatedAt(string $value)
  18. * @method \Magento\Review\Model\Review setEntityId(int $value)
  19. * @method int getEntityPkValue()
  20. * @method \Magento\Review\Model\Review setEntityPkValue(int $value)
  21. * @method int getStatusId()
  22. * @method \Magento\Review\Model\Review setStatusId(int $value)
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. * @since 100.0.2
  25. */
  26. class Review extends \Magento\Framework\Model\AbstractModel implements IdentityInterface
  27. {
  28. /**
  29. * Event prefix for observer
  30. *
  31. * @var string
  32. */
  33. protected $_eventPrefix = 'review';
  34. /**
  35. * Cache tag
  36. */
  37. const CACHE_TAG = 'review_block';
  38. /**
  39. * Product entity review code
  40. */
  41. const ENTITY_PRODUCT_CODE = 'product';
  42. /**
  43. * Customer entity review code
  44. */
  45. const ENTITY_CUSTOMER_CODE = 'customer';
  46. /**
  47. * Category entity review code
  48. */
  49. const ENTITY_CATEGORY_CODE = 'category';
  50. /**
  51. * Approved review status code
  52. */
  53. const STATUS_APPROVED = 1;
  54. /**
  55. * Pending review status code
  56. */
  57. const STATUS_PENDING = 2;
  58. /**
  59. * Not Approved review status code
  60. */
  61. const STATUS_NOT_APPROVED = 3;
  62. /**
  63. * Review product collection factory
  64. *
  65. * @var \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory
  66. */
  67. protected $productCollectionFactory;
  68. /**
  69. * Review status collection factory
  70. *
  71. * @var \Magento\Review\Model\ResourceModel\Review\Status\CollectionFactory
  72. */
  73. protected $_statusFactory;
  74. /**
  75. * Review model summary factory
  76. *
  77. * @var \Magento\Review\Model\Review\SummaryFactory
  78. */
  79. protected $_summaryFactory;
  80. /**
  81. * Review model summary factory
  82. *
  83. * @var \Magento\Review\Model\Review\SummaryFactory
  84. */
  85. protected $_summaryModFactory;
  86. /**
  87. * Review model summary
  88. *
  89. * @var \Magento\Review\Model\Review\Summary
  90. */
  91. protected $_reviewSummary;
  92. /**
  93. * Core model store manager interface
  94. *
  95. * @var \Magento\Store\Model\StoreManagerInterface
  96. */
  97. protected $_storeManager;
  98. /**
  99. * Url interface
  100. *
  101. * @var \Magento\Framework\UrlInterface
  102. */
  103. protected $_urlModel;
  104. /**
  105. * @param \Magento\Framework\Model\Context $context
  106. * @param \Magento\Framework\Registry $registry
  107. * @param \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $productFactory
  108. * @param \Magento\Review\Model\ResourceModel\Review\Status\CollectionFactory $statusFactory
  109. * @param \Magento\Review\Model\ResourceModel\Review\Summary\CollectionFactory $summaryFactory
  110. * @param \Magento\Review\Model\Review\SummaryFactory $summaryModFactory
  111. * @param \Magento\Review\Model\Review\Summary $reviewSummary
  112. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  113. * @param \Magento\Framework\UrlInterface $urlModel
  114. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  115. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  116. * @param array $data
  117. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  118. */
  119. public function __construct(
  120. \Magento\Framework\Model\Context $context,
  121. \Magento\Framework\Registry $registry,
  122. \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $productFactory,
  123. \Magento\Review\Model\ResourceModel\Review\Status\CollectionFactory $statusFactory,
  124. \Magento\Review\Model\ResourceModel\Review\Summary\CollectionFactory $summaryFactory,
  125. \Magento\Review\Model\Review\SummaryFactory $summaryModFactory,
  126. \Magento\Review\Model\Review\Summary $reviewSummary,
  127. \Magento\Store\Model\StoreManagerInterface $storeManager,
  128. \Magento\Framework\UrlInterface $urlModel,
  129. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  130. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  131. array $data = []
  132. ) {
  133. $this->productCollectionFactory = $productFactory;
  134. $this->_statusFactory = $statusFactory;
  135. $this->_summaryFactory = $summaryFactory;
  136. $this->_summaryModFactory = $summaryModFactory;
  137. $this->_reviewSummary = $reviewSummary;
  138. $this->_storeManager = $storeManager;
  139. $this->_urlModel = $urlModel;
  140. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  141. }
  142. /**
  143. * Initialization
  144. *
  145. * @return void
  146. */
  147. protected function _construct()
  148. {
  149. $this->_init(\Magento\Review\Model\ResourceModel\Review::class);
  150. }
  151. /**
  152. * Get product collection
  153. *
  154. * @return ProductCollection
  155. */
  156. public function getProductCollection()
  157. {
  158. return $this->productCollectionFactory->create();
  159. }
  160. /**
  161. * Get status collection
  162. *
  163. * @return StatusCollection
  164. */
  165. public function getStatusCollection()
  166. {
  167. return $this->_statusFactory->create();
  168. }
  169. /**
  170. * Get total reviews
  171. *
  172. * @param int $entityPkValue
  173. * @param bool $approvedOnly
  174. * @param int $storeId
  175. * @return int
  176. */
  177. public function getTotalReviews($entityPkValue, $approvedOnly = false, $storeId = 0)
  178. {
  179. return $this->getResource()->getTotalReviews($entityPkValue, $approvedOnly, $storeId);
  180. }
  181. /**
  182. * Aggregate reviews
  183. *
  184. * @return $this
  185. */
  186. public function aggregate()
  187. {
  188. $this->getResource()->aggregate($this);
  189. return $this;
  190. }
  191. /**
  192. * Get entity summary
  193. *
  194. * @param Product $product
  195. * @param int $storeId
  196. * @return void
  197. */
  198. public function getEntitySummary($product, $storeId = 0)
  199. {
  200. $summaryData = $this->_summaryModFactory->create()->setStoreId($storeId)->load($product->getId());
  201. $summary = new \Magento\Framework\DataObject();
  202. $summary->setData($summaryData->getData());
  203. $product->setRatingSummary($summary);
  204. }
  205. /**
  206. * Get pending status
  207. *
  208. * @return int
  209. */
  210. public function getPendingStatus()
  211. {
  212. return self::STATUS_PENDING;
  213. }
  214. /**
  215. * Get review product view url
  216. *
  217. * @return string
  218. */
  219. public function getReviewUrl()
  220. {
  221. return $this->_urlModel->getUrl('review/product/view', ['id' => $this->getReviewId()]);
  222. }
  223. /**
  224. * Get product view url
  225. *
  226. * @param string|int $productId
  227. * @param string|int $storeId
  228. * @return string
  229. */
  230. public function getProductUrl($productId, $storeId)
  231. {
  232. if ($storeId) {
  233. $this->_urlModel->setScope($storeId);
  234. }
  235. return $this->_urlModel->getUrl('catalog/product/view', ['id' => $productId]);
  236. }
  237. /**
  238. * Validate review summary fields
  239. *
  240. * @return bool|string[]
  241. */
  242. public function validate()
  243. {
  244. $errors = [];
  245. if (!\Zend_Validate::is($this->getTitle(), 'NotEmpty')) {
  246. $errors[] = __('Please enter a review summary.');
  247. }
  248. if (!\Zend_Validate::is($this->getNickname(), 'NotEmpty')) {
  249. $errors[] = __('Please enter a nickname.');
  250. }
  251. if (!\Zend_Validate::is($this->getDetail(), 'NotEmpty')) {
  252. $errors[] = __('Please enter a review.');
  253. }
  254. if (empty($errors)) {
  255. return true;
  256. }
  257. return $errors;
  258. }
  259. /**
  260. * Perform actions after object delete
  261. *
  262. * @return \Magento\Framework\Model\AbstractModel
  263. */
  264. public function afterDeleteCommit()
  265. {
  266. $this->getResource()->afterDeleteCommit($this);
  267. return parent::afterDeleteCommit();
  268. }
  269. /**
  270. * Append review summary to product collection
  271. *
  272. * @param ProductCollection $collection
  273. * @return $this
  274. */
  275. public function appendSummary($collection)
  276. {
  277. $entityIds = [];
  278. foreach ($collection->getItems() as $item) {
  279. $entityIds[] = $item->getEntityId();
  280. }
  281. if (sizeof($entityIds) == 0) {
  282. return $this;
  283. }
  284. $summaryData = $this->_summaryFactory->create()
  285. ->addEntityFilter($entityIds)
  286. ->addStoreFilter($this->_storeManager->getStore()->getId())
  287. ->load();
  288. foreach ($collection->getItems() as $item) {
  289. foreach ($summaryData as $summary) {
  290. if ($summary->getEntityPkValue() == $item->getEntityId()) {
  291. $item->setRatingSummary($summary);
  292. }
  293. }
  294. if (!$item->getRatingSummary()) {
  295. $item->setRatingSummary(new DataObject());
  296. }
  297. }
  298. return $this;
  299. }
  300. /**
  301. * Check if current review approved or not
  302. *
  303. * @return bool
  304. */
  305. public function isApproved()
  306. {
  307. return $this->getStatusId() == self::STATUS_APPROVED;
  308. }
  309. /**
  310. * Check if current review available on passed store
  311. *
  312. * @param int|\Magento\Store\Model\Store $store
  313. * @return bool
  314. */
  315. public function isAvailableOnStore($store = null)
  316. {
  317. $store = $this->_storeManager->getStore($store);
  318. if ($store) {
  319. return in_array($store->getId(), (array) $this->getStores());
  320. }
  321. return false;
  322. }
  323. /**
  324. * Get review entity type id by code
  325. *
  326. * @param string $entityCode
  327. * @return int|bool
  328. */
  329. public function getEntityIdByCode($entityCode)
  330. {
  331. return $this->getResource()->getEntityIdByCode($entityCode);
  332. }
  333. /**
  334. * Return unique ID(s) for each object in system
  335. *
  336. * @return array
  337. */
  338. public function getIdentities()
  339. {
  340. $tags = [];
  341. if ($this->getEntityPkValue()) {
  342. $tags[] = Product::CACHE_TAG . '_' . $this->getEntityPkValue();
  343. }
  344. return $tags;
  345. }
  346. }