PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Review/Model/Review.php

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