Detailed.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Block\Adminhtml\Rating;
  7. use Magento\Review\Model\Rating;
  8. use Magento\Review\Model\Rating\Option;
  9. use Magento\Review\Model\ResourceModel\Rating\Collection as RatingCollection;
  10. use Magento\Review\Model\ResourceModel\Rating\Option\Vote\Collection as VoteCollection;
  11. /**
  12. * Adminhtml detailed rating stars
  13. */
  14. class Detailed extends \Magento\Backend\Block\Template
  15. {
  16. /**
  17. * Vote collection
  18. *
  19. * @var VoteCollection
  20. */
  21. protected $_voteCollection = false;
  22. /**
  23. * Rating detail template name
  24. *
  25. * @var string
  26. */
  27. protected $_template = 'Magento_Review::rating/detailed.phtml';
  28. /**
  29. * Core registry
  30. *
  31. * @var \Magento\Framework\Registry
  32. */
  33. protected $_coreRegistry = null;
  34. /**
  35. * Rating resource model
  36. *
  37. * @var \Magento\Review\Model\ResourceModel\Rating\CollectionFactory
  38. */
  39. protected $_ratingsFactory;
  40. /**
  41. * Rating resource option model
  42. *
  43. * @var \Magento\Review\Model\ResourceModel\Rating\Option\Vote\CollectionFactory
  44. */
  45. protected $_votesFactory;
  46. /**
  47. * @param \Magento\Backend\Block\Template\Context $context
  48. * @param \Magento\Review\Model\ResourceModel\Rating\CollectionFactory $ratingsFactory
  49. * @param \Magento\Review\Model\ResourceModel\Rating\Option\Vote\CollectionFactory $votesFactory
  50. * @param \Magento\Framework\Registry $registry
  51. * @param array $data
  52. */
  53. public function __construct(
  54. \Magento\Backend\Block\Template\Context $context,
  55. \Magento\Review\Model\ResourceModel\Rating\CollectionFactory $ratingsFactory,
  56. \Magento\Review\Model\ResourceModel\Rating\Option\Vote\CollectionFactory $votesFactory,
  57. \Magento\Framework\Registry $registry,
  58. array $data = []
  59. ) {
  60. $this->_ratingsFactory = $ratingsFactory;
  61. $this->_votesFactory = $votesFactory;
  62. $this->_coreRegistry = $registry;
  63. parent::__construct($context, $data);
  64. }
  65. /**
  66. * Initialize review data
  67. *
  68. * @return void
  69. */
  70. protected function _construct()
  71. {
  72. parent::_construct();
  73. if ($this->_coreRegistry->registry('review_data')) {
  74. $this->setReviewId($this->_coreRegistry->registry('review_data')->getReviewId());
  75. }
  76. }
  77. /**
  78. * Get collection of ratings
  79. *
  80. * @return RatingCollection
  81. */
  82. public function getRating()
  83. {
  84. if (!$this->getRatingCollection()) {
  85. if ($this->_coreRegistry->registry('review_data')) {
  86. $stores = $this->_coreRegistry->registry('review_data')->getStores();
  87. $stores = array_diff($stores, [0]);
  88. $ratingCollection = $this->_ratingsFactory->create()->addEntityFilter(
  89. 'product'
  90. )->setStoreFilter(
  91. $stores
  92. )->setActiveFilter(
  93. true
  94. )->setPositionOrder()->load()->addOptionToItems();
  95. $this->_voteCollection = $this->_votesFactory->create()->setReviewFilter(
  96. $this->getReviewId()
  97. )->addOptionInfo()->load()->addRatingOptions();
  98. } elseif (!$this->getIsIndependentMode()) {
  99. $ratingCollection = $this->_ratingsFactory->create()->addEntityFilter(
  100. 'product'
  101. )->setStoreFilter(
  102. null
  103. )->setPositionOrder()->load()->addOptionToItems();
  104. } else {
  105. $stores = $this->getRequest()->getParam('select_stores') ?: $this->getRequest()->getParam('stores');
  106. $ratingCollection = $this->_ratingsFactory->create()->addEntityFilter(
  107. 'product'
  108. )->setStoreFilter(
  109. $stores
  110. )->setPositionOrder()->load()->addOptionToItems();
  111. if ((int)$this->getRequest()->getParam('id')) {
  112. $this->_voteCollection = $this->_votesFactory->create()->setReviewFilter(
  113. (int)$this->getRequest()->getParam('id')
  114. )->addOptionInfo()->load()->addRatingOptions();
  115. }
  116. }
  117. $this->setRatingCollection($ratingCollection->getSize() ? $ratingCollection : false);
  118. }
  119. return $this->getRatingCollection();
  120. }
  121. /**
  122. * Set independent mode
  123. *
  124. * @return $this
  125. */
  126. public function setIndependentMode()
  127. {
  128. $this->setIsIndependentMode(true);
  129. return $this;
  130. }
  131. /**
  132. * Indicator of whether or not a rating is selected
  133. *
  134. * @param Option $option
  135. * @param \Magento\Review\Model\Rating $rating
  136. * @return bool
  137. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  138. */
  139. public function isSelected($option, $rating)
  140. {
  141. if ($this->getIsIndependentMode()) {
  142. $ratings = $this->getRequest()->getParam('ratings');
  143. if (isset($ratings[$option->getRatingId()])) {
  144. return $option->getId() == $ratings[$option->getRatingId()];
  145. } elseif (!$this->_voteCollection) {
  146. return false;
  147. }
  148. }
  149. if ($this->_voteCollection) {
  150. foreach ($this->_voteCollection as $vote) {
  151. if ($option->getId() == $vote->getOptionId()) {
  152. return true;
  153. }
  154. }
  155. }
  156. return false;
  157. }
  158. }