Form.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Block;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Customer\Model\Context;
  9. use Magento\Customer\Model\Url;
  10. use Magento\Review\Model\ResourceModel\Rating\Collection as RatingCollection;
  11. /**
  12. * Review form block
  13. *
  14. * @api
  15. * @author Magento Core Team <core@magentocommerce.com>
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. * @since 100.0.2
  18. */
  19. class Form extends \Magento\Framework\View\Element\Template
  20. {
  21. /**
  22. * Review data
  23. *
  24. * @var \Magento\Review\Helper\Data
  25. */
  26. protected $_reviewData = null;
  27. /**
  28. * Catalog product model
  29. *
  30. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  31. */
  32. protected $productRepository;
  33. /**
  34. * Rating model
  35. *
  36. * @var \Magento\Review\Model\RatingFactory
  37. */
  38. protected $_ratingFactory;
  39. /**
  40. * @var \Magento\Framework\Url\EncoderInterface
  41. */
  42. protected $urlEncoder;
  43. /**
  44. * Message manager interface
  45. *
  46. * @var \Magento\Framework\Message\ManagerInterface
  47. */
  48. protected $messageManager;
  49. /**
  50. * @var \Magento\Framework\App\Http\Context
  51. */
  52. protected $httpContext;
  53. /**
  54. * @var \Magento\Customer\Model\Url
  55. */
  56. protected $customerUrl;
  57. /**
  58. * @var array
  59. */
  60. protected $jsLayout;
  61. /**
  62. * @var \Magento\Framework\Serialize\Serializer\Json
  63. */
  64. private $serializer;
  65. /**
  66. * Form constructor.
  67. *
  68. * @param \Magento\Framework\View\Element\Template\Context $context
  69. * @param \Magento\Framework\Url\EncoderInterface $urlEncoder
  70. * @param \Magento\Review\Helper\Data $reviewData
  71. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  72. * @param \Magento\Review\Model\RatingFactory $ratingFactory
  73. * @param \Magento\Framework\Message\ManagerInterface $messageManager
  74. * @param \Magento\Framework\App\Http\Context $httpContext
  75. * @param Url $customerUrl
  76. * @param array $data
  77. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  78. * @throws \RuntimeException
  79. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  80. */
  81. public function __construct(
  82. \Magento\Framework\View\Element\Template\Context $context,
  83. \Magento\Framework\Url\EncoderInterface $urlEncoder,
  84. \Magento\Review\Helper\Data $reviewData,
  85. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
  86. \Magento\Review\Model\RatingFactory $ratingFactory,
  87. \Magento\Framework\Message\ManagerInterface $messageManager,
  88. \Magento\Framework\App\Http\Context $httpContext,
  89. \Magento\Customer\Model\Url $customerUrl,
  90. array $data = [],
  91. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  92. ) {
  93. $this->urlEncoder = $urlEncoder;
  94. $this->_reviewData = $reviewData;
  95. $this->productRepository = $productRepository;
  96. $this->_ratingFactory = $ratingFactory;
  97. $this->messageManager = $messageManager;
  98. $this->httpContext = $httpContext;
  99. $this->customerUrl = $customerUrl;
  100. parent::__construct($context, $data);
  101. $this->jsLayout = isset($data['jsLayout']) ? $data['jsLayout'] : [];
  102. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  103. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  104. }
  105. /**
  106. * Initialize review form
  107. *
  108. * @return void
  109. */
  110. protected function _construct()
  111. {
  112. parent::_construct();
  113. $this->setAllowWriteReviewFlag(
  114. $this->httpContext->getValue(Context::CONTEXT_AUTH)
  115. || $this->_reviewData->getIsGuestAllowToWrite()
  116. );
  117. if (!$this->getAllowWriteReviewFlag()) {
  118. $queryParam = $this->urlEncoder->encode(
  119. $this->getUrl('*/*/*', ['_current' => true]) . '#review-form'
  120. );
  121. $this->setLoginLink(
  122. $this->getUrl(
  123. 'customer/account/login/',
  124. [Url::REFERER_QUERY_PARAM_NAME => $queryParam]
  125. )
  126. );
  127. }
  128. $this->setTemplate('Magento_Review::form.phtml');
  129. }
  130. /**
  131. * @return string
  132. */
  133. public function getJsLayout()
  134. {
  135. return $this->serializer->serialize($this->jsLayout);
  136. }
  137. /**
  138. * Get product info
  139. *
  140. * @return \Magento\Catalog\Api\Data\ProductInterface
  141. * @throws \Magento\Framework\Exception\NoSuchEntityException
  142. */
  143. public function getProductInfo()
  144. {
  145. return $this->productRepository->getById(
  146. $this->getProductId(),
  147. false,
  148. $this->_storeManager->getStore()->getId()
  149. );
  150. }
  151. /**
  152. * Get review product post action
  153. *
  154. * @return string
  155. */
  156. public function getAction()
  157. {
  158. return $this->getUrl(
  159. 'review/product/post',
  160. [
  161. '_secure' => $this->getRequest()->isSecure(),
  162. 'id' => $this->getProductId(),
  163. ]
  164. );
  165. }
  166. /**
  167. * Get collection of ratings
  168. *
  169. * @return RatingCollection
  170. * @throws \Magento\Framework\Exception\LocalizedException
  171. */
  172. public function getRatings()
  173. {
  174. return $this->_ratingFactory->create()->getResourceCollection()->addEntityFilter(
  175. 'product'
  176. )->setPositionOrder()->addRatingPerStoreName(
  177. $this->_storeManager->getStore()->getId()
  178. )->setStoreFilter(
  179. $this->_storeManager->getStore()->getId()
  180. )->setActiveFilter(
  181. true
  182. )->load()->addOptionToItems();
  183. }
  184. /**
  185. * Return register URL
  186. *
  187. * @return string
  188. */
  189. public function getRegisterUrl()
  190. {
  191. return $this->customerUrl->getRegisterUrl();
  192. }
  193. /**
  194. * Get review product id
  195. *
  196. * @return int
  197. */
  198. protected function getProductId()
  199. {
  200. return $this->getRequest()->getParam('id', false);
  201. }
  202. }