123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Review\Controller\Adminhtml\Product;
- use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
- use Magento\Review\Controller\Adminhtml\Product as ProductController;
- use Magento\Framework\Controller\ResultFactory;
- use Magento\Framework\Exception\LocalizedException;
- /**
- * Save Review action.
- */
- class Save extends ProductController implements HttpPostActionInterface
- {
- /**
- * Save Review action.
- *
- * @return \Magento\Backend\Model\View\Result\Redirect
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- public function execute()
- {
- /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
- $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
- if (($data = $this->getRequest()->getPostValue()) && ($reviewId = $this->getRequest()->getParam('id'))) {
- $review = $this->reviewFactory->create()->load($reviewId);
- if (!$review->getId()) {
- $this->messageManager->addError(__('The review was removed by another user or does not exist.'));
- } else {
- try {
- $review->addData($data)->save();
- $arrRatingId = $this->getRequest()->getParam('ratings', []);
- /** @var \Magento\Review\Model\Rating\Option\Vote $votes */
- $votes = $this->_objectManager->create(\Magento\Review\Model\Rating\Option\Vote::class)
- ->getResourceCollection()
- ->setReviewFilter($reviewId)
- ->addOptionInfo()
- ->load()
- ->addRatingOptions();
- foreach ($arrRatingId as $ratingId => $optionId) {
- if ($vote = $votes->getItemByColumnValue('rating_id', $ratingId)) {
- $this->ratingFactory->create()
- ->setVoteId($vote->getId())
- ->setReviewId($review->getId())
- ->updateOptionVote($optionId);
- } else {
- $this->ratingFactory->create()
- ->setRatingId($ratingId)
- ->setReviewId($review->getId())
- ->addOptionVote($optionId, $review->getEntityPkValue());
- }
- }
- $review->aggregate();
- $this->messageManager->addSuccess(__('You saved the review.'));
- } catch (LocalizedException $e) {
- $this->messageManager->addError($e->getMessage());
- } catch (\Exception $e) {
- $this->messageManager->addException($e, __('Something went wrong while saving this review.'));
- }
- }
- $nextId = (int)$this->getRequest()->getParam('next_item');
- if ($nextId) {
- $resultRedirect->setPath('review/*/edit', ['id' => $nextId]);
- } elseif ($this->getRequest()->getParam('ret') == 'pending') {
- $resultRedirect->setPath('review/*/pending');
- } else {
- $resultRedirect->setPath('*/*/');
- }
- return $resultRedirect;
- }
- $resultRedirect->setPath('review/*/');
- return $resultRedirect;
- }
- }
|