mirror of
https://github.com/bdnugget/magento2.git
synced 2026-11-07 09:17:35 +00:00
401 lines
11 KiB
PHP
401 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* Copyright © Magento, Inc. All rights reserved.
|
|
* See COPYING.txt for license details.
|
|
*/
|
|
|
|
namespace Magento\Review\Model;
|
|
|
|
use Magento\Framework\DataObject;
|
|
use Magento\Catalog\Model\Product;
|
|
use Magento\Framework\DataObject\IdentityInterface;
|
|
use Magento\Framework\Validator\NotEmpty;
|
|
use Magento\Framework\Validator\ValidateException;
|
|
use Magento\Framework\Validator\ValidatorChain;
|
|
use Magento\Review\Model\ResourceModel\Review\Product\Collection as ProductCollection;
|
|
use Magento\Review\Model\ResourceModel\Review\Status\Collection as StatusCollection;
|
|
|
|
/**
|
|
* Review model
|
|
*
|
|
* @api
|
|
* @method string getCreatedAt()
|
|
* @method \Magento\Review\Model\Review setCreatedAt(string $value)
|
|
* @method \Magento\Review\Model\Review setEntityId(int $value)
|
|
* @method int getEntityPkValue()
|
|
* @method \Magento\Review\Model\Review setEntityPkValue(int $value)
|
|
* @method int getStatusId()
|
|
* @method \Magento\Review\Model\Review setStatusId(int $value)
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
|
* @since 100.0.2
|
|
*/
|
|
class Review extends \Magento\Framework\Model\AbstractModel implements IdentityInterface
|
|
{
|
|
/**
|
|
* Event prefix for observer
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $_eventPrefix = 'review';
|
|
|
|
/**
|
|
* Cache tag value
|
|
*/
|
|
public const CACHE_TAG = 'review_block';
|
|
|
|
/**
|
|
* Product entity review code
|
|
*/
|
|
public const ENTITY_PRODUCT_CODE = 'product';
|
|
|
|
/**
|
|
* Customer entity review code
|
|
*/
|
|
public const ENTITY_CUSTOMER_CODE = 'customer';
|
|
|
|
/**
|
|
* Category entity review code
|
|
*/
|
|
public const ENTITY_CATEGORY_CODE = 'category';
|
|
|
|
/**
|
|
* Approved review status code
|
|
*/
|
|
public const STATUS_APPROVED = 1;
|
|
|
|
/**
|
|
* Pending review status code
|
|
*/
|
|
public const STATUS_PENDING = 2;
|
|
|
|
/**
|
|
* Not Approved review status code
|
|
*/
|
|
public const STATUS_NOT_APPROVED = 3;
|
|
|
|
/**
|
|
* Review product collection factory
|
|
*
|
|
* @var \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory
|
|
*/
|
|
protected $productCollectionFactory;
|
|
|
|
/**
|
|
* Review status collection factory
|
|
*
|
|
* @var \Magento\Review\Model\ResourceModel\Review\Status\CollectionFactory
|
|
*/
|
|
protected $_statusFactory;
|
|
|
|
/**
|
|
* Review model summary factory
|
|
*
|
|
* @var \Magento\Review\Model\Review\SummaryFactory
|
|
*/
|
|
protected $_summaryFactory;
|
|
|
|
/**
|
|
* Review model summary factory
|
|
*
|
|
* @var \Magento\Review\Model\Review\SummaryFactory
|
|
*/
|
|
protected $_summaryModFactory;
|
|
|
|
/**
|
|
* Review model summary
|
|
*
|
|
* @deprecated 100.3.3 Summary factory injected as separate property
|
|
* @see we don't recommend this approach anymore
|
|
* @var \Magento\Review\Model\Review\Summary
|
|
*/
|
|
protected $_reviewSummary;
|
|
|
|
/**
|
|
* Core model store manager interface
|
|
*
|
|
* @var \Magento\Store\Model\StoreManagerInterface
|
|
*/
|
|
protected $_storeManager;
|
|
|
|
/**
|
|
* Url interface
|
|
*
|
|
* @var \Magento\Framework\UrlInterface
|
|
*/
|
|
protected $_urlModel;
|
|
|
|
/**
|
|
* @param \Magento\Framework\Model\Context $context
|
|
* @param \Magento\Framework\Registry $registry
|
|
* @param \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $productFactory
|
|
* @param \Magento\Review\Model\ResourceModel\Review\Status\CollectionFactory $statusFactory
|
|
* @param \Magento\Review\Model\ResourceModel\Review\Summary\CollectionFactory $summaryFactory
|
|
* @param \Magento\Review\Model\Review\SummaryFactory $summaryModFactory
|
|
* @param \Magento\Review\Model\Review\Summary $reviewSummary
|
|
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
|
* @param \Magento\Framework\UrlInterface $urlModel
|
|
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
|
|
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
|
|
* @param array $data
|
|
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
|
|
*/
|
|
public function __construct(
|
|
\Magento\Framework\Model\Context $context,
|
|
\Magento\Framework\Registry $registry,
|
|
\Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $productFactory,
|
|
\Magento\Review\Model\ResourceModel\Review\Status\CollectionFactory $statusFactory,
|
|
\Magento\Review\Model\ResourceModel\Review\Summary\CollectionFactory $summaryFactory,
|
|
\Magento\Review\Model\Review\SummaryFactory $summaryModFactory,
|
|
\Magento\Review\Model\Review\Summary $reviewSummary,
|
|
\Magento\Store\Model\StoreManagerInterface $storeManager,
|
|
\Magento\Framework\UrlInterface $urlModel,
|
|
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
|
|
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
|
|
array $data = []
|
|
) {
|
|
$this->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
|
|
*
|
|
* @deprecated 100.3.3
|
|
* @see we don't recommend this approach anymore
|
|
* @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[]
|
|
* @throws ValidateException
|
|
*/
|
|
public function validate()
|
|
{
|
|
$errors = [];
|
|
|
|
if (!ValidatorChain::is($this->getTitle(), NotEmpty::class)) {
|
|
$errors[] = __('Please enter a review summary.');
|
|
}
|
|
|
|
if (!ValidatorChain::is($this->getNickname(), NotEmpty::class)) {
|
|
$errors[] = __('Please enter a nickname.');
|
|
}
|
|
|
|
if (!ValidatorChain::is($this->getDetail(), NotEmpty::class)) {
|
|
$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 data object to product collection
|
|
*
|
|
* @deprecated 100.3.3
|
|
* @see we don't recommend this approach anymore
|
|
* @param ProductCollection $collection
|
|
* @return $this
|
|
* @throws \Magento\Framework\Exception\NoSuchEntityException
|
|
*/
|
|
public function appendSummary($collection)
|
|
{
|
|
$entityIds = [];
|
|
foreach ($collection->getItems() as $item) {
|
|
$entityIds[] = $item->getEntityId();
|
|
}
|
|
|
|
if (count($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;
|
|
}
|
|
}
|