Product.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Controller;
  7. use Magento\Catalog\Model\Product as CatalogProduct;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Review\Model\Review;
  11. /**
  12. * Review controller
  13. *
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. abstract class Product extends \Magento\Framework\App\Action\Action
  17. {
  18. /**
  19. * Core registry
  20. *
  21. * @var \Magento\Framework\Registry
  22. */
  23. protected $coreRegistry = null;
  24. /**
  25. * Customer session model
  26. *
  27. * @var \Magento\Customer\Model\Session
  28. */
  29. protected $customerSession;
  30. /**
  31. * Generic session
  32. *
  33. * @var \Magento\Framework\Session\Generic
  34. */
  35. protected $reviewSession;
  36. /**
  37. * Catalog category model
  38. *
  39. * @var \Magento\Catalog\Api\CategoryRepositoryInterface
  40. */
  41. protected $categoryRepository;
  42. /**
  43. * Logger
  44. *
  45. * @var \Psr\Log\LoggerInterface
  46. */
  47. protected $logger;
  48. /**
  49. * Catalog product model
  50. *
  51. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  52. */
  53. protected $productRepository;
  54. /**
  55. * Review model
  56. *
  57. * @var \Magento\Review\Model\ReviewFactory
  58. */
  59. protected $reviewFactory;
  60. /**
  61. * Rating model
  62. *
  63. * @var \Magento\Review\Model\RatingFactory
  64. */
  65. protected $ratingFactory;
  66. /**
  67. * Catalog design model
  68. *
  69. * @var \Magento\Catalog\Model\Design
  70. */
  71. protected $catalogDesign;
  72. /**
  73. * Core model store manager interface
  74. *
  75. * @var \Magento\Store\Model\StoreManagerInterface
  76. */
  77. protected $storeManager;
  78. /**
  79. * Core form key validator
  80. *
  81. * @var \Magento\Framework\Data\Form\FormKey\Validator
  82. */
  83. protected $formKeyValidator;
  84. /**
  85. * @param \Magento\Framework\App\Action\Context $context
  86. * @param \Magento\Framework\Registry $coreRegistry
  87. * @param \Magento\Customer\Model\Session $customerSession
  88. * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
  89. * @param \Psr\Log\LoggerInterface $logger
  90. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  91. * @param \Magento\Review\Model\ReviewFactory $reviewFactory
  92. * @param \Magento\Review\Model\RatingFactory $ratingFactory
  93. * @param \Magento\Catalog\Model\Design $catalogDesign
  94. * @param \Magento\Framework\Session\Generic $reviewSession
  95. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  96. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  97. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  98. */
  99. public function __construct(
  100. \Magento\Framework\App\Action\Context $context,
  101. \Magento\Framework\Registry $coreRegistry,
  102. \Magento\Customer\Model\Session $customerSession,
  103. \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
  104. \Psr\Log\LoggerInterface $logger,
  105. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
  106. \Magento\Review\Model\ReviewFactory $reviewFactory,
  107. \Magento\Review\Model\RatingFactory $ratingFactory,
  108. \Magento\Catalog\Model\Design $catalogDesign,
  109. \Magento\Framework\Session\Generic $reviewSession,
  110. \Magento\Store\Model\StoreManagerInterface $storeManager,
  111. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  112. ) {
  113. $this->storeManager = $storeManager;
  114. $this->coreRegistry = $coreRegistry;
  115. $this->customerSession = $customerSession;
  116. $this->reviewSession = $reviewSession;
  117. $this->categoryRepository = $categoryRepository;
  118. $this->logger = $logger;
  119. $this->productRepository = $productRepository;
  120. $this->reviewFactory = $reviewFactory;
  121. $this->ratingFactory = $ratingFactory;
  122. $this->catalogDesign = $catalogDesign;
  123. $this->formKeyValidator = $formKeyValidator;
  124. parent::__construct($context);
  125. }
  126. /**
  127. * Dispatch request
  128. *
  129. * @param RequestInterface $request
  130. * @return \Magento\Framework\App\ResponseInterface
  131. */
  132. public function dispatch(RequestInterface $request)
  133. {
  134. $allowGuest = $this->_objectManager->get(\Magento\Review\Helper\Data::class)->getIsGuestAllowToWrite();
  135. if (!$request->isDispatched()) {
  136. return parent::dispatch($request);
  137. }
  138. if (!$allowGuest && $request->getActionName() == 'post' && $request->isPost()) {
  139. if (!$this->customerSession->isLoggedIn()) {
  140. $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
  141. $this->customerSession->setBeforeAuthUrl($this->_url->getUrl('*/*/*', ['_current' => true]));
  142. $this->reviewSession->setFormData(
  143. $request->getPostValue()
  144. )->setRedirectUrl(
  145. $this->_redirect->getRefererUrl()
  146. );
  147. $this->getResponse()->setRedirect(
  148. $this->_objectManager->get(\Magento\Customer\Model\Url::class)->getLoginUrl()
  149. );
  150. }
  151. }
  152. return parent::dispatch($request);
  153. }
  154. /**
  155. * Initialize and check product
  156. *
  157. * @return \Magento\Catalog\Model\Product|bool
  158. */
  159. protected function initProduct()
  160. {
  161. $this->_eventManager->dispatch('review_controller_product_init_before', ['controller_action' => $this]);
  162. $categoryId = (int)$this->getRequest()->getParam('category', false);
  163. $productId = (int)$this->getRequest()->getParam('id');
  164. $product = $this->loadProduct($productId);
  165. if (!$product) {
  166. return false;
  167. }
  168. if ($categoryId) {
  169. $category = $this->categoryRepository->get($categoryId);
  170. $this->coreRegistry->register('current_category', $category);
  171. }
  172. try {
  173. $this->_eventManager->dispatch('review_controller_product_init', ['product' => $product]);
  174. $this->_eventManager->dispatch(
  175. 'review_controller_product_init_after',
  176. ['product' => $product, 'controller_action' => $this]
  177. );
  178. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  179. $this->logger->critical($e);
  180. return false;
  181. }
  182. return $product;
  183. }
  184. /**
  185. * Load product model with data by passed id.
  186. * Return false if product was not loaded or has incorrect status.
  187. *
  188. * @param int $productId
  189. * @return bool|CatalogProduct
  190. */
  191. protected function loadProduct($productId)
  192. {
  193. if (!$productId) {
  194. return false;
  195. }
  196. try {
  197. $product = $this->productRepository->getById($productId);
  198. if (!in_array($this->storeManager->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
  199. throw new NoSuchEntityException();
  200. }
  201. if (!$product->isVisibleInCatalog() || !$product->isVisibleInSiteVisibility()) {
  202. throw new NoSuchEntityException();
  203. }
  204. } catch (NoSuchEntityException $noEntityException) {
  205. return false;
  206. }
  207. $this->coreRegistry->register('current_product', $product);
  208. $this->coreRegistry->register('product', $product);
  209. return $product;
  210. }
  211. }