Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
12 
13 class Save extends ProductController implements HttpPostActionInterface
14 {
19  public function execute()
20  {
22  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
23  if (($data = $this->getRequest()->getPostValue()) && ($reviewId = $this->getRequest()->getParam('id'))) {
24  $review = $this->reviewFactory->create()->load($reviewId);
25  if (!$review->getId()) {
26  $this->messageManager->addError(__('The review was removed by another user or does not exist.'));
27  } else {
28  try {
29  $review->addData($data)->save();
30 
31  $arrRatingId = $this->getRequest()->getParam('ratings', []);
33  $votes = $this->_objectManager->create(\Magento\Review\Model\Rating\Option\Vote::class)
34  ->getResourceCollection()
35  ->setReviewFilter($reviewId)
36  ->addOptionInfo()
37  ->load()
38  ->addRatingOptions();
39  foreach ($arrRatingId as $ratingId => $optionId) {
40  if ($vote = $votes->getItemByColumnValue('rating_id', $ratingId)) {
41  $this->ratingFactory->create()
42  ->setVoteId($vote->getId())
43  ->setReviewId($review->getId())
44  ->updateOptionVote($optionId);
45  } else {
46  $this->ratingFactory->create()
47  ->setRatingId($ratingId)
48  ->setReviewId($review->getId())
49  ->addOptionVote($optionId, $review->getEntityPkValue());
50  }
51  }
52 
53  $review->aggregate();
54 
55  $this->messageManager->addSuccess(__('You saved the review.'));
56  } catch (LocalizedException $e) {
57  $this->messageManager->addError($e->getMessage());
58  } catch (\Exception $e) {
59  $this->messageManager->addException($e, __('Something went wrong while saving this review.'));
60  }
61  }
62 
63  $nextId = (int)$this->getRequest()->getParam('next_item');
64  if ($nextId) {
65  $resultRedirect->setPath('review/*/edit', ['id' => $nextId]);
66  } elseif ($this->getRequest()->getParam('ret') == 'pending') {
67  $resultRedirect->setPath('*/*/pending');
68  } else {
69  $resultRedirect->setPath('*/*/');
70  }
71  return $resultRedirect;
72  }
73  $resultRedirect->setPath('review/*/');
74  return $resultRedirect;
75  }
76 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13