ListCustomer.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. /**
  10. * Customer Reviews list block
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class ListCustomer extends \Magento\Customer\Block\Account\Dashboard
  16. {
  17. /**
  18. * Product reviews collection
  19. *
  20. * @var \Magento\Review\Model\ResourceModel\Review\Product\Collection
  21. */
  22. protected $_collection;
  23. /**
  24. * Review resource model
  25. *
  26. * @var \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory
  27. */
  28. protected $_collectionFactory;
  29. /**
  30. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  31. */
  32. protected $currentCustomer;
  33. /**
  34. * @param \Magento\Framework\View\Element\Template\Context $context
  35. * @param \Magento\Customer\Model\Session $customerSession
  36. * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
  37. * @param CustomerRepositoryInterface $customerRepository
  38. * @param AccountManagementInterface $customerAccountManagement
  39. * @param \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $collectionFactory
  40. * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
  41. * @param array $data
  42. */
  43. public function __construct(
  44. \Magento\Framework\View\Element\Template\Context $context,
  45. \Magento\Customer\Model\Session $customerSession,
  46. \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
  47. CustomerRepositoryInterface $customerRepository,
  48. AccountManagementInterface $customerAccountManagement,
  49. \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $collectionFactory,
  50. \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
  51. array $data = []
  52. ) {
  53. $this->_collectionFactory = $collectionFactory;
  54. parent::__construct(
  55. $context,
  56. $customerSession,
  57. $subscriberFactory,
  58. $customerRepository,
  59. $customerAccountManagement,
  60. $data
  61. );
  62. $this->currentCustomer = $currentCustomer;
  63. }
  64. /**
  65. * Get html code for toolbar
  66. *
  67. * @return string
  68. */
  69. public function getToolbarHtml()
  70. {
  71. return $this->getChildHtml('toolbar');
  72. }
  73. /**
  74. * Initializes toolbar
  75. *
  76. * @return \Magento\Framework\View\Element\AbstractBlock
  77. */
  78. protected function _prepareLayout()
  79. {
  80. if ($this->getReviews()) {
  81. $toolbar = $this->getLayout()->createBlock(
  82. \Magento\Theme\Block\Html\Pager::class,
  83. 'customer_review_list.toolbar'
  84. )->setCollection(
  85. $this->getReviews()
  86. );
  87. $this->setChild('toolbar', $toolbar);
  88. }
  89. return parent::_prepareLayout();
  90. }
  91. /**
  92. * Get reviews
  93. *
  94. * @return bool|\Magento\Review\Model\ResourceModel\Review\Product\Collection
  95. */
  96. public function getReviews()
  97. {
  98. if (!($customerId = $this->currentCustomer->getCustomerId())) {
  99. return false;
  100. }
  101. if (!$this->_collection) {
  102. $this->_collection = $this->_collectionFactory->create();
  103. $this->_collection
  104. ->addStoreFilter($this->_storeManager->getStore()->getId())
  105. ->addCustomerFilter($customerId)
  106. ->setDateOrder();
  107. }
  108. return $this->_collection;
  109. }
  110. /**
  111. * Get review link
  112. *
  113. * @return string
  114. * @deprecated 100.2.0
  115. */
  116. public function getReviewLink()
  117. {
  118. return $this->getUrl('review/customer/view/');
  119. }
  120. /**
  121. * Get review URL
  122. *
  123. * @param \Magento\Review\Model\Review $review
  124. * @return string
  125. * @since 100.2.0
  126. */
  127. public function getReviewUrl($review)
  128. {
  129. return $this->getUrl('review/customer/view', ['id' => $review->getReviewId()]);
  130. }
  131. /**
  132. * Get product link
  133. *
  134. * @return string
  135. * @deprecated 100.2.0
  136. */
  137. public function getProductLink()
  138. {
  139. return $this->getUrl('catalog/product/view/');
  140. }
  141. /**
  142. * Get product URL
  143. *
  144. * @param \Magento\Catalog\Model\Product $product
  145. * @return string
  146. * @since 100.2.0
  147. */
  148. public function getProductUrl($product)
  149. {
  150. return $product->getProductUrl();
  151. }
  152. /**
  153. * Format date in short format
  154. *
  155. * @param string $date
  156. * @return string
  157. */
  158. public function dateFormat($date)
  159. {
  160. return $this->formatDate($date, \IntlDateFormatter::SHORT);
  161. }
  162. /**
  163. * Add review summary
  164. *
  165. * @return \Magento\Framework\View\Element\AbstractBlock
  166. */
  167. protected function _beforeToHtml()
  168. {
  169. $reviews = $this->getReviews();
  170. if ($reviews) {
  171. $reviews->load()->addReviewSummary();
  172. }
  173. return parent::_beforeToHtml();
  174. }
  175. }