productCollectionFactory = $productFactory; $this->_statusFactory = $statusFactory; $this->_summaryFactory = $summaryFactory; $this->_summaryModFactory = $summaryModFactory; $this->_reviewSummary = $reviewSummary; $this->_storeManager = $storeManager; $this->_urlModel = $urlModel; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } /** * Initialization * * @return void */ protected function _construct() { $this->_init(\Magento\Review\Model\ResourceModel\Review::class); } /** * Get product collection * * @return ProductCollection */ public function getProductCollection() { return $this->productCollectionFactory->create(); } /** * Get status collection * * @return StatusCollection */ public function getStatusCollection() { return $this->_statusFactory->create(); } /** * Get total reviews * * @param int $entityPkValue * @param bool $approvedOnly * @param int $storeId * @return int */ public function getTotalReviews($entityPkValue, $approvedOnly = false, $storeId = 0) { return $this->getResource()->getTotalReviews($entityPkValue, $approvedOnly, $storeId); } /** * Aggregate reviews * * @return $this */ public function aggregate() { $this->getResource()->aggregate($this); return $this; } /** * Get entity summary * * @param Product $product * @param int $storeId * @return void */ public function getEntitySummary($product, $storeId = 0) { $summaryData = $this->_summaryModFactory->create()->setStoreId($storeId)->load($product->getId()); $summary = new \Magento\Framework\DataObject(); $summary->setData($summaryData->getData()); $product->setRatingSummary($summary); } /** * Get pending status * * @return int */ public function getPendingStatus() { return self::STATUS_PENDING; } /** * Get review product view url * * @return string */ public function getReviewUrl() { return $this->_urlModel->getUrl('review/product/view', ['id' => $this->getReviewId()]); } /** * Get product view url * * @param string|int $productId * @param string|int $storeId * @return string */ public function getProductUrl($productId, $storeId) { if ($storeId) { $this->_urlModel->setScope($storeId); } return $this->_urlModel->getUrl('catalog/product/view', ['id' => $productId]); } /** * Validate review summary fields * * @return bool|string[] */ public function validate() { $errors = []; if (!\Zend_Validate::is($this->getTitle(), 'NotEmpty')) { $errors[] = __('Please enter a review summary.'); } if (!\Zend_Validate::is($this->getNickname(), 'NotEmpty')) { $errors[] = __('Please enter a nickname.'); } if (!\Zend_Validate::is($this->getDetail(), 'NotEmpty')) { $errors[] = __('Please enter a review.'); } if (empty($errors)) { return true; } return $errors; } /** * Perform actions after object delete * * @return \Magento\Framework\Model\AbstractModel */ public function afterDeleteCommit() { $this->getResource()->afterDeleteCommit($this); return parent::afterDeleteCommit(); } /** * Append review summary to product collection * * @param ProductCollection $collection * @return $this */ public function appendSummary($collection) { $entityIds = []; foreach ($collection->getItems() as $item) { $entityIds[] = $item->getEntityId(); } if (sizeof($entityIds) == 0) { return $this; } $summaryData = $this->_summaryFactory->create() ->addEntityFilter($entityIds) ->addStoreFilter($this->_storeManager->getStore()->getId()) ->load(); foreach ($collection->getItems() as $item) { foreach ($summaryData as $summary) { if ($summary->getEntityPkValue() == $item->getEntityId()) { $item->setRatingSummary($summary); } } if (!$item->getRatingSummary()) { $item->setRatingSummary(new DataObject()); } } return $this; } /** * Check if current review approved or not * * @return bool */ public function isApproved() { return $this->getStatusId() == self::STATUS_APPROVED; } /** * Check if current review available on passed store * * @param int|\Magento\Store\Model\Store $store * @return bool */ public function isAvailableOnStore($store = null) { $store = $this->_storeManager->getStore($store); if ($store) { return in_array($store->getId(), (array) $this->getStores()); } return false; } /** * Get review entity type id by code * * @param string $entityCode * @return int|bool */ public function getEntityIdByCode($entityCode) { return $this->getResource()->getEntityIdByCode($entityCode); } /** * Return unique ID(s) for each object in system * * @return array */ public function getIdentities() { $tags = []; if ($this->getEntityPkValue()) { $tags[] = Product::CACHE_TAG . '_' . $this->getEntityPkValue(); } return $tags; } }