123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Review\Controller;
- use Magento\Catalog\Model\Product as CatalogProduct;
- use Magento\Framework\App\RequestInterface;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Review\Model\Review;
- /**
- * Review controller
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- abstract class Product extends \Magento\Framework\App\Action\Action
- {
- /**
- * Core registry
- *
- * @var \Magento\Framework\Registry
- */
- protected $coreRegistry = null;
- /**
- * Customer session model
- *
- * @var \Magento\Customer\Model\Session
- */
- protected $customerSession;
- /**
- * Generic session
- *
- * @var \Magento\Framework\Session\Generic
- */
- protected $reviewSession;
- /**
- * Catalog category model
- *
- * @var \Magento\Catalog\Api\CategoryRepositoryInterface
- */
- protected $categoryRepository;
- /**
- * Logger
- *
- * @var \Psr\Log\LoggerInterface
- */
- protected $logger;
- /**
- * Catalog product model
- *
- * @var \Magento\Catalog\Api\ProductRepositoryInterface
- */
- protected $productRepository;
- /**
- * Review model
- *
- * @var \Magento\Review\Model\ReviewFactory
- */
- protected $reviewFactory;
- /**
- * Rating model
- *
- * @var \Magento\Review\Model\RatingFactory
- */
- protected $ratingFactory;
- /**
- * Catalog design model
- *
- * @var \Magento\Catalog\Model\Design
- */
- protected $catalogDesign;
- /**
- * Core model store manager interface
- *
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $storeManager;
- /**
- * Core form key validator
- *
- * @var \Magento\Framework\Data\Form\FormKey\Validator
- */
- protected $formKeyValidator;
- /**
- * @param \Magento\Framework\App\Action\Context $context
- * @param \Magento\Framework\Registry $coreRegistry
- * @param \Magento\Customer\Model\Session $customerSession
- * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
- * @param \Psr\Log\LoggerInterface $logger
- * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
- * @param \Magento\Review\Model\ReviewFactory $reviewFactory
- * @param \Magento\Review\Model\RatingFactory $ratingFactory
- * @param \Magento\Catalog\Model\Design $catalogDesign
- * @param \Magento\Framework\Session\Generic $reviewSession
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- \Magento\Framework\App\Action\Context $context,
- \Magento\Framework\Registry $coreRegistry,
- \Magento\Customer\Model\Session $customerSession,
- \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
- \Psr\Log\LoggerInterface $logger,
- \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
- \Magento\Review\Model\ReviewFactory $reviewFactory,
- \Magento\Review\Model\RatingFactory $ratingFactory,
- \Magento\Catalog\Model\Design $catalogDesign,
- \Magento\Framework\Session\Generic $reviewSession,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
- ) {
- $this->storeManager = $storeManager;
- $this->coreRegistry = $coreRegistry;
- $this->customerSession = $customerSession;
- $this->reviewSession = $reviewSession;
- $this->categoryRepository = $categoryRepository;
- $this->logger = $logger;
- $this->productRepository = $productRepository;
- $this->reviewFactory = $reviewFactory;
- $this->ratingFactory = $ratingFactory;
- $this->catalogDesign = $catalogDesign;
- $this->formKeyValidator = $formKeyValidator;
- parent::__construct($context);
- }
- /**
- * Dispatch request
- *
- * @param RequestInterface $request
- * @return \Magento\Framework\App\ResponseInterface
- */
- public function dispatch(RequestInterface $request)
- {
- $allowGuest = $this->_objectManager->get(\Magento\Review\Helper\Data::class)->getIsGuestAllowToWrite();
- if (!$request->isDispatched()) {
- return parent::dispatch($request);
- }
- if (!$allowGuest && $request->getActionName() == 'post' && $request->isPost()) {
- if (!$this->customerSession->isLoggedIn()) {
- $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
- $this->customerSession->setBeforeAuthUrl($this->_url->getUrl('*/*/*', ['_current' => true]));
- $this->reviewSession->setFormData(
- $request->getPostValue()
- )->setRedirectUrl(
- $this->_redirect->getRefererUrl()
- );
- $this->getResponse()->setRedirect(
- $this->_objectManager->get(\Magento\Customer\Model\Url::class)->getLoginUrl()
- );
- }
- }
- return parent::dispatch($request);
- }
- /**
- * Initialize and check product
- *
- * @return \Magento\Catalog\Model\Product|bool
- */
- protected function initProduct()
- {
- $this->_eventManager->dispatch('review_controller_product_init_before', ['controller_action' => $this]);
- $categoryId = (int)$this->getRequest()->getParam('category', false);
- $productId = (int)$this->getRequest()->getParam('id');
- $product = $this->loadProduct($productId);
- if (!$product) {
- return false;
- }
- if ($categoryId) {
- $category = $this->categoryRepository->get($categoryId);
- $this->coreRegistry->register('current_category', $category);
- }
- try {
- $this->_eventManager->dispatch('review_controller_product_init', ['product' => $product]);
- $this->_eventManager->dispatch(
- 'review_controller_product_init_after',
- ['product' => $product, 'controller_action' => $this]
- );
- } catch (\Magento\Framework\Exception\LocalizedException $e) {
- $this->logger->critical($e);
- return false;
- }
- return $product;
- }
- /**
- * Load product model with data by passed id.
- * Return false if product was not loaded or has incorrect status.
- *
- * @param int $productId
- * @return bool|CatalogProduct
- */
- protected function loadProduct($productId)
- {
- if (!$productId) {
- return false;
- }
- try {
- $product = $this->productRepository->getById($productId);
- if (!in_array($this->storeManager->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
- throw new NoSuchEntityException();
- }
- if (!$product->isVisibleInCatalog() || !$product->isVisibleInSiteVisibility()) {
- throw new NoSuchEntityException();
- }
- } catch (NoSuchEntityException $noEntityException) {
- return false;
- }
- $this->coreRegistry->register('current_product', $product);
- $this->coreRegistry->register('product', $product);
- return $product;
- }
- }
|