Save.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Controller\Adminhtml\Product;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Review\Controller\Adminhtml\Product as ProductController;
  9. use Magento\Framework\Controller\ResultFactory;
  10. use Magento\Framework\Exception\LocalizedException;
  11. /**
  12. * Save Review action.
  13. */
  14. class Save extends ProductController implements HttpPostActionInterface
  15. {
  16. /**
  17. * Save Review action.
  18. *
  19. * @return \Magento\Backend\Model\View\Result\Redirect
  20. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  21. */
  22. public function execute()
  23. {
  24. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  25. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  26. if (($data = $this->getRequest()->getPostValue()) && ($reviewId = $this->getRequest()->getParam('id'))) {
  27. $review = $this->reviewFactory->create()->load($reviewId);
  28. if (!$review->getId()) {
  29. $this->messageManager->addError(__('The review was removed by another user or does not exist.'));
  30. } else {
  31. try {
  32. $review->addData($data)->save();
  33. $arrRatingId = $this->getRequest()->getParam('ratings', []);
  34. /** @var \Magento\Review\Model\Rating\Option\Vote $votes */
  35. $votes = $this->_objectManager->create(\Magento\Review\Model\Rating\Option\Vote::class)
  36. ->getResourceCollection()
  37. ->setReviewFilter($reviewId)
  38. ->addOptionInfo()
  39. ->load()
  40. ->addRatingOptions();
  41. foreach ($arrRatingId as $ratingId => $optionId) {
  42. if ($vote = $votes->getItemByColumnValue('rating_id', $ratingId)) {
  43. $this->ratingFactory->create()
  44. ->setVoteId($vote->getId())
  45. ->setReviewId($review->getId())
  46. ->updateOptionVote($optionId);
  47. } else {
  48. $this->ratingFactory->create()
  49. ->setRatingId($ratingId)
  50. ->setReviewId($review->getId())
  51. ->addOptionVote($optionId, $review->getEntityPkValue());
  52. }
  53. }
  54. $review->aggregate();
  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. $nextId = (int)$this->getRequest()->getParam('next_item');
  63. if ($nextId) {
  64. $resultRedirect->setPath('review/*/edit', ['id' => $nextId]);
  65. } elseif ($this->getRequest()->getParam('ret') == 'pending') {
  66. $resultRedirect->setPath('review/*/pending');
  67. } else {
  68. $resultRedirect->setPath('*/*/');
  69. }
  70. return $resultRedirect;
  71. }
  72. $resultRedirect->setPath('review/*/');
  73. return $resultRedirect;
  74. }
  75. }