123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Review\Block\Adminhtml\Rating;
- use Magento\Review\Model\Rating;
- use Magento\Review\Model\Rating\Option;
- use Magento\Review\Model\ResourceModel\Rating\Collection as RatingCollection;
- use Magento\Review\Model\ResourceModel\Rating\Option\Vote\Collection as VoteCollection;
- /**
- * Adminhtml detailed rating stars
- */
- class Detailed extends \Magento\Backend\Block\Template
- {
- /**
- * Vote collection
- *
- * @var VoteCollection
- */
- protected $_voteCollection = false;
- /**
- * Rating detail template name
- *
- * @var string
- */
- protected $_template = 'Magento_Review::rating/detailed.phtml';
- /**
- * Core registry
- *
- * @var \Magento\Framework\Registry
- */
- protected $_coreRegistry = null;
- /**
- * Rating resource model
- *
- * @var \Magento\Review\Model\ResourceModel\Rating\CollectionFactory
- */
- protected $_ratingsFactory;
- /**
- * Rating resource option model
- *
- * @var \Magento\Review\Model\ResourceModel\Rating\Option\Vote\CollectionFactory
- */
- protected $_votesFactory;
- /**
- * @param \Magento\Backend\Block\Template\Context $context
- * @param \Magento\Review\Model\ResourceModel\Rating\CollectionFactory $ratingsFactory
- * @param \Magento\Review\Model\ResourceModel\Rating\Option\Vote\CollectionFactory $votesFactory
- * @param \Magento\Framework\Registry $registry
- * @param array $data
- */
- public function __construct(
- \Magento\Backend\Block\Template\Context $context,
- \Magento\Review\Model\ResourceModel\Rating\CollectionFactory $ratingsFactory,
- \Magento\Review\Model\ResourceModel\Rating\Option\Vote\CollectionFactory $votesFactory,
- \Magento\Framework\Registry $registry,
- array $data = []
- ) {
- $this->_ratingsFactory = $ratingsFactory;
- $this->_votesFactory = $votesFactory;
- $this->_coreRegistry = $registry;
- parent::__construct($context, $data);
- }
- /**
- * Initialize review data
- *
- * @return void
- */
- protected function _construct()
- {
- parent::_construct();
- if ($this->_coreRegistry->registry('review_data')) {
- $this->setReviewId($this->_coreRegistry->registry('review_data')->getReviewId());
- }
- }
- /**
- * Get collection of ratings
- *
- * @return RatingCollection
- */
- public function getRating()
- {
- if (!$this->getRatingCollection()) {
- if ($this->_coreRegistry->registry('review_data')) {
- $stores = $this->_coreRegistry->registry('review_data')->getStores();
- $stores = array_diff($stores, [0]);
- $ratingCollection = $this->_ratingsFactory->create()->addEntityFilter(
- 'product'
- )->setStoreFilter(
- $stores
- )->setActiveFilter(
- true
- )->setPositionOrder()->load()->addOptionToItems();
- $this->_voteCollection = $this->_votesFactory->create()->setReviewFilter(
- $this->getReviewId()
- )->addOptionInfo()->load()->addRatingOptions();
- } elseif (!$this->getIsIndependentMode()) {
- $ratingCollection = $this->_ratingsFactory->create()->addEntityFilter(
- 'product'
- )->setStoreFilter(
- null
- )->setPositionOrder()->load()->addOptionToItems();
- } else {
- $stores = $this->getRequest()->getParam('select_stores') ?: $this->getRequest()->getParam('stores');
- $ratingCollection = $this->_ratingsFactory->create()->addEntityFilter(
- 'product'
- )->setStoreFilter(
- $stores
- )->setPositionOrder()->load()->addOptionToItems();
- if ((int)$this->getRequest()->getParam('id')) {
- $this->_voteCollection = $this->_votesFactory->create()->setReviewFilter(
- (int)$this->getRequest()->getParam('id')
- )->addOptionInfo()->load()->addRatingOptions();
- }
- }
- $this->setRatingCollection($ratingCollection->getSize() ? $ratingCollection : false);
- }
- return $this->getRatingCollection();
- }
- /**
- * Set independent mode
- *
- * @return $this
- */
- public function setIndependentMode()
- {
- $this->setIsIndependentMode(true);
- return $this;
- }
- /**
- * Indicator of whether or not a rating is selected
- *
- * @param Option $option
- * @param \Magento\Review\Model\Rating $rating
- * @return bool
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function isSelected($option, $rating)
- {
- if ($this->getIsIndependentMode()) {
- $ratings = $this->getRequest()->getParam('ratings');
- if (isset($ratings[$option->getRatingId()])) {
- return $option->getId() == $ratings[$option->getRatingId()];
- } elseif (!$this->_voteCollection) {
- return false;
- }
- }
- if ($this->_voteCollection) {
- foreach ($this->_voteCollection as $vote) {
- if ($option->getId() == $vote->getOptionId()) {
- return true;
- }
- }
- }
- return false;
- }
- }
|