123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Review\Block\Customer;
- use Magento\Review\Model\ResourceModel\Review\Product\Collection;
- /**
- * Recent Customer Reviews Block
- *
- * @api
- * @since 100.0.2
- */
- class Recent extends \Magento\Framework\View\Element\Template
- {
- /**
- * Customer list template name
- *
- * @var string
- */
- protected $_template = 'Magento_Review::customer/list.phtml';
- /**
- * Product reviews collection
- *
- * @var Collection
- */
- protected $_collection;
- /**
- * Review resource model
- *
- * @var \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory
- */
- protected $_collectionFactory;
- /**
- * @var \Magento\Customer\Helper\Session\CurrentCustomer
- */
- protected $currentCustomer;
- /**
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $collectionFactory
- * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
- * @param array $data
- */
- public function __construct(
- \Magento\Framework\View\Element\Template\Context $context,
- \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $collectionFactory,
- \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
- array $data = []
- ) {
- $this->_collectionFactory = $collectionFactory;
- parent::__construct($context, $data);
- $this->currentCustomer = $currentCustomer;
- }
- /**
- * Truncate string
- *
- * @param string $value
- * @param int $length
- * @param string $etc
- * @param string &$remainder
- * @param bool $breakWords
- * @return string
- */
- public function truncateString($value, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)
- {
- return $this->filterManager->truncate(
- $value,
- ['length' => $length, 'etc' => $etc, 'remainder' => $remainder, 'breakWords' => $breakWords]
- );
- }
- /**
- * Return collection of reviews
- *
- * @return array|\Magento\Review\Model\ResourceModel\Review\Product\Collection
- */
- public function getReviews()
- {
- if (!($customerId = $this->currentCustomer->getCustomerId())) {
- return [];
- }
- if (!$this->_collection) {
- $this->_collection = $this->_collectionFactory->create();
- $this->_collection
- ->addStoreFilter($this->_storeManager->getStore()->getId())
- ->addCustomerFilter($customerId)
- ->setDateOrder()
- ->setPageSize(5)
- ->load()
- ->addReviewSummary();
- }
- return $this->_collection;
- }
- /**
- * Return review customer view url
- *
- * @return string
- */
- public function getReviewLink()
- {
- return $this->getUrl('review/customer/view/');
- }
- /**
- * Return catalog product view url
- *
- * @return string
- */
- public function getProductLink()
- {
- return $this->getUrl('catalog/product/view/');
- }
- /**
- * Format review date
- *
- * @param string $date
- * @return string
- */
- public function dateFormat($date)
- {
- return $this->formatDate($date, \IntlDateFormatter::SHORT);
- }
- /**
- * Return review customer url
- *
- * @return string
- */
- public function getAllReviewsUrl()
- {
- return $this->getUrl('review/customer');
- }
- /**
- * Return review customer view url for a specific customer/review
- *
- * @param int $id
- * @return string
- */
- public function getReviewUrl($id)
- {
- return $this->getUrl('review/customer/view', ['id' => $id]);
- }
- }
|