Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
View.php
Go to the documentation of this file.
1 <?php
7 
11 
20 {
26  protected $_template = 'Magento_Review::customer/view.phtml';
27 
33  protected $productRepository;
34 
40  protected $_reviewFactory;
41 
47  protected $_voteFactory;
48 
54  protected $_ratingFactory;
55 
59  protected $currentCustomer;
60 
70  public function __construct(
71  \Magento\Catalog\Block\Product\Context $context,
73  \Magento\Review\Model\ReviewFactory $reviewFactory,
74  \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory,
75  \Magento\Review\Model\RatingFactory $ratingFactory,
76  \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
77  array $data = []
78  ) {
79  $this->productRepository = $productRepository;
80  $this->_reviewFactory = $reviewFactory;
81  $this->_voteFactory = $voteFactory;
82  $this->_ratingFactory = $ratingFactory;
83  $this->currentCustomer = $currentCustomer;
84  parent::__construct(
85  $context,
86  $data
87  );
88  }
89 
95  protected function _construct()
96  {
97  parent::_construct();
98  $this->setReviewId($this->getRequest()->getParam('id', false));
99  }
100 
106  public function getProductData()
107  {
108  if ($this->getReviewId() && !$this->getProductCacheData()) {
109  $product = $this->productRepository->getById($this->getReviewData()->getEntityPkValue());
110  $this->setProductCacheData($product);
111  }
112  return $this->getProductCacheData();
113  }
114 
120  public function getReviewData()
121  {
122  if ($this->getReviewId() && !$this->getReviewCachedData()) {
123  $this->setReviewCachedData($this->_reviewFactory->create()->load($this->getReviewId()));
124  }
125  return $this->getReviewCachedData();
126  }
127 
133  public function getBackUrl()
134  {
135  return $this->getUrl('review/customer');
136  }
137 
143  public function getRating()
144  {
145  if (!$this->getRatingCollection()) {
146  $ratingCollection = $this->_voteFactory->create()->getResourceCollection()->setReviewFilter(
147  $this->getReviewId()
148  )->addRatingInfo(
149  $this->_storeManager->getStore()->getId()
150  )->setStoreFilter(
151  $this->_storeManager->getStore()->getId()
152  )->load();
153 
154  $this->setRatingCollection($ratingCollection->getSize() ? $ratingCollection : false);
155  }
156 
157  return $this->getRatingCollection();
158  }
159 
165  public function getRatingSummary()
166  {
167  if (!$this->getRatingSummaryCache()) {
168  $this->setRatingSummaryCache(
169  $this->_ratingFactory->create()->getEntitySummary($this->getProductData()->getId())
170  );
171  }
172  return $this->getRatingSummaryCache();
173  }
174 
180  public function getTotalReviews()
181  {
182  if (!$this->getTotalReviewsCache()) {
183  $this->setTotalReviewsCache(
184  $this->_reviewFactory->create()->getTotalReviews($this->getProductData()->getId()),
185  false,
186  $this->_storeManager->getStore()->getId()
187  );
188  }
189  return $this->getTotalReviewsCache();
190  }
191 
198  public function dateFormat($date)
199  {
200  return $this->formatDate($date, \IntlDateFormatter::LONG);
201  }
202 
211  public function getReviewsSummaryHtml(
212  \Magento\Catalog\Model\Product $product,
213  $templateType = false,
214  $displayIfNoReviews = false
215  ) {
216  if (!$product->getRatingSummary()) {
217  $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
218  }
219  return parent::getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
220  }
221 
225  protected function _toHtml()
226  {
227  return $this->currentCustomer->getCustomerId() ? parent::_toHtml() : '';
228  }
229 }
__construct(\Magento\Catalog\Block\Product\Context $context, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Review\Model\ReviewFactory $reviewFactory, \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory, \Magento\Review\Model\RatingFactory $ratingFactory, \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer, array $data=[])
Definition: View.php:70
formatDate( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
$templateType
Definition: list.phtml:37
getReviewsSummaryHtml(\Magento\Catalog\Model\Product $product, $templateType=false, $displayIfNoReviews=false)
Definition: View.php:211