View.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Block;
  7. /**
  8. * Review detailed view block
  9. *
  10. * @api
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @since 100.0.2
  13. */
  14. class View extends \Magento\Catalog\Block\Product\AbstractProduct
  15. {
  16. /**
  17. * View template name
  18. *
  19. * @var string
  20. */
  21. protected $_template = 'Magento_Review::view.phtml';
  22. /**
  23. * Rating option model
  24. *
  25. * @var \Magento\Review\Model\Rating\Option\VoteFactory
  26. */
  27. protected $_voteFactory;
  28. /**
  29. * Rating model factory
  30. *
  31. * @var \Magento\Review\Model\RatingFactory
  32. */
  33. protected $_ratingFactory;
  34. /**
  35. * Review model
  36. *
  37. * @var \Magento\Review\Model\ReviewFactory
  38. */
  39. protected $_reviewFactory;
  40. /**
  41. * @param \Magento\Catalog\Block\Product\Context $context
  42. * @param \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory
  43. * @param \Magento\Review\Model\RatingFactory $ratingFactory
  44. * @param \Magento\Review\Model\ReviewFactory $reviewFactory
  45. * @param array $data
  46. */
  47. public function __construct(
  48. \Magento\Catalog\Block\Product\Context $context,
  49. \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory,
  50. \Magento\Review\Model\RatingFactory $ratingFactory,
  51. \Magento\Review\Model\ReviewFactory $reviewFactory,
  52. array $data = []
  53. ) {
  54. $this->_voteFactory = $voteFactory;
  55. $this->_reviewFactory = $reviewFactory;
  56. $this->_ratingFactory = $ratingFactory;
  57. parent::__construct(
  58. $context,
  59. $data
  60. );
  61. }
  62. /**
  63. * Retrieve current product model from registry
  64. *
  65. * @return \Magento\Catalog\Model\Product
  66. */
  67. public function getProductData()
  68. {
  69. return $this->_coreRegistry->registry('current_product');
  70. }
  71. /**
  72. * Retrieve current review model from registry
  73. *
  74. * @return \Magento\Review\Model\Review
  75. */
  76. public function getReviewData()
  77. {
  78. return $this->_coreRegistry->registry('current_review');
  79. }
  80. /**
  81. * Prepare link to review list for current product
  82. *
  83. * @return string
  84. */
  85. public function getBackUrl()
  86. {
  87. return $this->getUrl('*/*/list', ['id' => $this->getProductData()->getId()]);
  88. }
  89. /**
  90. * Retrieve collection of ratings
  91. *
  92. * @return \Magento\Review\Model\ResourceModel\Rating\Option\Vote\Collection
  93. */
  94. public function getRating()
  95. {
  96. if (!$this->getRatingCollection()) {
  97. $ratingCollection = $this->_voteFactory->create()->getResourceCollection()->setReviewFilter(
  98. $this->getReviewId()
  99. )->setStoreFilter(
  100. $this->_storeManager->getStore()->getId()
  101. )->addRatingInfo(
  102. $this->_storeManager->getStore()->getId()
  103. )->load();
  104. $this->setRatingCollection($ratingCollection->getSize() ? $ratingCollection : false);
  105. }
  106. return $this->getRatingCollection();
  107. }
  108. /**
  109. * Retrieve rating summary for current product
  110. *
  111. * @return string
  112. */
  113. public function getRatingSummary()
  114. {
  115. if (!$this->getRatingSummaryCache()) {
  116. $this->setRatingSummaryCache(
  117. $this->_ratingFactory->create()->getEntitySummary($this->getProductData()->getId())
  118. );
  119. }
  120. return $this->getRatingSummaryCache();
  121. }
  122. /**
  123. * Retrieve total review count for current product
  124. *
  125. * @return string
  126. */
  127. public function getTotalReviews()
  128. {
  129. if (!$this->getTotalReviewsCache()) {
  130. $this->setTotalReviewsCache(
  131. $this->_reviewFactory->create()->getTotalReviews(
  132. $this->getProductData()->getId(),
  133. false,
  134. $this->_storeManager->getStore()->getId()
  135. )
  136. );
  137. }
  138. return $this->getTotalReviewsCache();
  139. }
  140. /**
  141. * Format date in long format
  142. *
  143. * @param string $date
  144. * @return string
  145. */
  146. public function dateFormat($date)
  147. {
  148. return $this->formatDate($date, \IntlDateFormatter::LONG);
  149. }
  150. /**
  151. * Get product reviews summary
  152. *
  153. * @param \Magento\Catalog\Model\Product $product
  154. * @param bool $templateType
  155. * @param bool $displayIfNoReviews
  156. * @return string
  157. */
  158. public function getReviewsSummaryHtml(
  159. \Magento\Catalog\Model\Product $product,
  160. $templateType = false,
  161. $displayIfNoReviews = false
  162. ) {
  163. if (!$product->getRatingSummary()) {
  164. $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
  165. }
  166. return parent::getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
  167. }
  168. }