Recent.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\Review\Model\ResourceModel\Review\Product\Collection;
  8. /**
  9. * Recent Customer Reviews Block
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Recent extends \Magento\Framework\View\Element\Template
  15. {
  16. /**
  17. * Customer list template name
  18. *
  19. * @var string
  20. */
  21. protected $_template = 'Magento_Review::customer/list.phtml';
  22. /**
  23. * Product reviews collection
  24. *
  25. * @var Collection
  26. */
  27. protected $_collection;
  28. /**
  29. * Review resource model
  30. *
  31. * @var \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory
  32. */
  33. protected $_collectionFactory;
  34. /**
  35. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  36. */
  37. protected $currentCustomer;
  38. /**
  39. * @param \Magento\Framework\View\Element\Template\Context $context
  40. * @param \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $collectionFactory
  41. * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
  42. * @param array $data
  43. */
  44. public function __construct(
  45. \Magento\Framework\View\Element\Template\Context $context,
  46. \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $collectionFactory,
  47. \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
  48. array $data = []
  49. ) {
  50. $this->_collectionFactory = $collectionFactory;
  51. parent::__construct($context, $data);
  52. $this->currentCustomer = $currentCustomer;
  53. }
  54. /**
  55. * Truncate string
  56. *
  57. * @param string $value
  58. * @param int $length
  59. * @param string $etc
  60. * @param string &$remainder
  61. * @param bool $breakWords
  62. * @return string
  63. */
  64. public function truncateString($value, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)
  65. {
  66. return $this->filterManager->truncate(
  67. $value,
  68. ['length' => $length, 'etc' => $etc, 'remainder' => $remainder, 'breakWords' => $breakWords]
  69. );
  70. }
  71. /**
  72. * Return collection of reviews
  73. *
  74. * @return array|\Magento\Review\Model\ResourceModel\Review\Product\Collection
  75. */
  76. public function getReviews()
  77. {
  78. if (!($customerId = $this->currentCustomer->getCustomerId())) {
  79. return [];
  80. }
  81. if (!$this->_collection) {
  82. $this->_collection = $this->_collectionFactory->create();
  83. $this->_collection
  84. ->addStoreFilter($this->_storeManager->getStore()->getId())
  85. ->addCustomerFilter($customerId)
  86. ->setDateOrder()
  87. ->setPageSize(5)
  88. ->load()
  89. ->addReviewSummary();
  90. }
  91. return $this->_collection;
  92. }
  93. /**
  94. * Return review customer view url
  95. *
  96. * @return string
  97. */
  98. public function getReviewLink()
  99. {
  100. return $this->getUrl('review/customer/view/');
  101. }
  102. /**
  103. * Return catalog product view url
  104. *
  105. * @return string
  106. */
  107. public function getProductLink()
  108. {
  109. return $this->getUrl('catalog/product/view/');
  110. }
  111. /**
  112. * Format review date
  113. *
  114. * @param string $date
  115. * @return string
  116. */
  117. public function dateFormat($date)
  118. {
  119. return $this->formatDate($date, \IntlDateFormatter::SHORT);
  120. }
  121. /**
  122. * Return review customer url
  123. *
  124. * @return string
  125. */
  126. public function getAllReviewsUrl()
  127. {
  128. return $this->getUrl('review/customer');
  129. }
  130. /**
  131. * Return review customer view url for a specific customer/review
  132. *
  133. * @param int $id
  134. * @return string
  135. */
  136. public function getReviewUrl($id)
  137. {
  138. return $this->getUrl('review/customer/view', ['id' => $id]);
  139. }
  140. }