View.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Block\Customer;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Review\Model\ResourceModel\Rating\Option\Vote\Collection as VoteCollection;
  9. use Magento\Review\Model\Review;
  10. /**
  11. * Customer Review detailed view block
  12. *
  13. * @api
  14. * @author Magento Core Team <core@magentocommerce.com>
  15. * @since 100.0.2
  16. */
  17. class View extends \Magento\Catalog\Block\Product\AbstractProduct
  18. {
  19. /**
  20. * Customer view template name
  21. *
  22. * @var string
  23. */
  24. protected $_template = 'Magento_Review::customer/view.phtml';
  25. /**
  26. * Catalog product model
  27. *
  28. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  29. */
  30. protected $productRepository;
  31. /**
  32. * Review model
  33. *
  34. * @var \Magento\Review\Model\ReviewFactory
  35. */
  36. protected $_reviewFactory;
  37. /**
  38. * Rating option model
  39. *
  40. * @var \Magento\Review\Model\Rating\Option\VoteFactory
  41. */
  42. protected $_voteFactory;
  43. /**
  44. * Rating model
  45. *
  46. * @var \Magento\Review\Model\RatingFactory
  47. */
  48. protected $_ratingFactory;
  49. /**
  50. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  51. */
  52. protected $currentCustomer;
  53. /**
  54. * @param \Magento\Catalog\Block\Product\Context $context
  55. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  56. * @param \Magento\Review\Model\ReviewFactory $reviewFactory
  57. * @param \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory
  58. * @param \Magento\Review\Model\RatingFactory $ratingFactory
  59. * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
  60. * @param array $data
  61. */
  62. public function __construct(
  63. \Magento\Catalog\Block\Product\Context $context,
  64. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
  65. \Magento\Review\Model\ReviewFactory $reviewFactory,
  66. \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory,
  67. \Magento\Review\Model\RatingFactory $ratingFactory,
  68. \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
  69. array $data = []
  70. ) {
  71. $this->productRepository = $productRepository;
  72. $this->_reviewFactory = $reviewFactory;
  73. $this->_voteFactory = $voteFactory;
  74. $this->_ratingFactory = $ratingFactory;
  75. $this->currentCustomer = $currentCustomer;
  76. parent::__construct(
  77. $context,
  78. $data
  79. );
  80. }
  81. /**
  82. * Initialize review id
  83. *
  84. * @return void
  85. */
  86. protected function _construct()
  87. {
  88. parent::_construct();
  89. $this->setReviewId($this->getRequest()->getParam('id', false));
  90. }
  91. /**
  92. * Get product data
  93. *
  94. * @return Product
  95. */
  96. public function getProductData()
  97. {
  98. if ($this->getReviewId() && !$this->getProductCacheData()) {
  99. $product = $this->productRepository->getById($this->getReviewData()->getEntityPkValue());
  100. $this->setProductCacheData($product);
  101. }
  102. return $this->getProductCacheData();
  103. }
  104. /**
  105. * Get review data
  106. *
  107. * @return Review
  108. */
  109. public function getReviewData()
  110. {
  111. if ($this->getReviewId() && !$this->getReviewCachedData()) {
  112. $this->setReviewCachedData($this->_reviewFactory->create()->load($this->getReviewId()));
  113. }
  114. return $this->getReviewCachedData();
  115. }
  116. /**
  117. * Return review customer url
  118. *
  119. * @return string
  120. */
  121. public function getBackUrl()
  122. {
  123. return $this->getUrl('review/customer');
  124. }
  125. /**
  126. * Get review rating collection
  127. *
  128. * @return VoteCollection
  129. */
  130. public function getRating()
  131. {
  132. if (!$this->getRatingCollection()) {
  133. $ratingCollection = $this->_voteFactory->create()->getResourceCollection()->setReviewFilter(
  134. $this->getReviewId()
  135. )->addRatingInfo(
  136. $this->_storeManager->getStore()->getId()
  137. )->setStoreFilter(
  138. $this->_storeManager->getStore()->getId()
  139. )->load();
  140. $this->setRatingCollection($ratingCollection->getSize() ? $ratingCollection : false);
  141. }
  142. return $this->getRatingCollection();
  143. }
  144. /**
  145. * Get rating summary
  146. *
  147. * @return array
  148. */
  149. public function getRatingSummary()
  150. {
  151. if (!$this->getRatingSummaryCache()) {
  152. $this->setRatingSummaryCache(
  153. $this->_ratingFactory->create()->getEntitySummary($this->getProductData()->getId())
  154. );
  155. }
  156. return $this->getRatingSummaryCache();
  157. }
  158. /**
  159. * Get total reviews
  160. *
  161. * @return int
  162. */
  163. public function getTotalReviews()
  164. {
  165. if (!$this->getTotalReviewsCache()) {
  166. $this->setTotalReviewsCache(
  167. $this->_reviewFactory->create()->getTotalReviews($this->getProductData()->getId()),
  168. false,
  169. $this->_storeManager->getStore()->getId()
  170. );
  171. }
  172. return $this->getTotalReviewsCache();
  173. }
  174. /**
  175. * Get formatted date
  176. *
  177. * @param string $date
  178. * @return string
  179. */
  180. public function dateFormat($date)
  181. {
  182. return $this->formatDate($date, \IntlDateFormatter::LONG);
  183. }
  184. /**
  185. * Get product reviews summary
  186. *
  187. * @param \Magento\Catalog\Model\Product $product
  188. * @param bool $templateType
  189. * @param bool $displayIfNoReviews
  190. * @return string
  191. */
  192. public function getReviewsSummaryHtml(
  193. \Magento\Catalog\Model\Product $product,
  194. $templateType = false,
  195. $displayIfNoReviews = false
  196. ) {
  197. if (!$product->getRatingSummary()) {
  198. $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
  199. }
  200. return parent::getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
  201. }
  202. /**
  203. * @return string
  204. */
  205. protected function _toHtml()
  206. {
  207. return $this->currentCustomer->getCustomerId() ? parent::_toHtml() : '';
  208. }
  209. }