1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Review\Controller\Product;
- use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
- use Magento\Review\Controller\Product as ProductController;
- use Magento\Framework\Controller\ResultFactory;
- use Magento\Review\Model\Review;
- class Post extends ProductController implements HttpPostActionInterface
- {
- /**
- * Submit new review action
- *
- * @return \Magento\Framework\Controller\Result\Redirect
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function execute()
- {
- /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
- $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
- if (!$this->formKeyValidator->validate($this->getRequest())) {
- $resultRedirect->setUrl($this->_redirect->getRefererUrl());
- return $resultRedirect;
- }
- $data = $this->reviewSession->getFormData(true);
- if ($data) {
- $rating = [];
- if (isset($data['ratings']) && is_array($data['ratings'])) {
- $rating = $data['ratings'];
- }
- } else {
- $data = $this->getRequest()->getPostValue();
- $rating = $this->getRequest()->getParam('ratings', []);
- }
- if (($product = $this->initProduct()) && !empty($data)) {
- /** @var \Magento\Review\Model\Review $review */
- $review = $this->reviewFactory->create()->setData($data);
- $review->unsetData('review_id');
- $validate = $review->validate();
- if ($validate === true) {
- try {
- $review->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
- ->setEntityPkValue($product->getId())
- ->setStatusId(Review::STATUS_PENDING)
- ->setCustomerId($this->customerSession->getCustomerId())
- ->setStoreId($this->storeManager->getStore()->getId())
- ->setStores([$this->storeManager->getStore()->getId()])
- ->save();
- foreach ($rating as $ratingId => $optionId) {
- $this->ratingFactory->create()
- ->setRatingId($ratingId)
- ->setReviewId($review->getId())
- ->setCustomerId($this->customerSession->getCustomerId())
- ->addOptionVote($optionId, $product->getId());
- }
- $review->aggregate();
- $this->messageManager->addSuccess(__('You submitted your review for moderation.'));
- } catch (\Exception $e) {
- $this->reviewSession->setFormData($data);
- $this->messageManager->addError(__('We can\'t post your review right now.'));
- }
- } else {
- $this->reviewSession->setFormData($data);
- if (is_array($validate)) {
- foreach ($validate as $errorMessage) {
- $this->messageManager->addError($errorMessage);
- }
- } else {
- $this->messageManager->addError(__('We can\'t post your review right now.'));
- }
- }
- }
- $redirectUrl = $this->reviewSession->getRedirectUrl(true);
- $resultRedirect->setUrl($redirectUrl ?: $this->_redirect->getRedirectUrl());
- return $resultRedirect;
- }
- }
|