Post.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Store\Model\Store;
  11. use Magento\Framework\Exception\LocalizedException;
  12. class Post extends ProductController implements HttpPostActionInterface
  13. {
  14. /**
  15. * @return \Magento\Backend\Model\View\Result\Redirect
  16. */
  17. public function execute()
  18. {
  19. $productId = $this->getRequest()->getParam('product_id', false);
  20. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  21. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  22. if ($data = $this->getRequest()->getPostValue()) {
  23. /** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
  24. $storeManager = $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
  25. if ($storeManager->hasSingleStore()) {
  26. $data['stores'] = [
  27. $storeManager->getStore(true)->getId(),
  28. ];
  29. } elseif (isset($data['select_stores'])) {
  30. $data['stores'] = $data['select_stores'];
  31. }
  32. $review = $this->reviewFactory->create()->setData($data);
  33. try {
  34. $review->setEntityId(1) // product
  35. ->setEntityPkValue($productId)
  36. ->setStoreId(Store::DEFAULT_STORE_ID)
  37. ->setStatusId($data['status_id'])
  38. ->setCustomerId(null)//null is for administrator only
  39. ->save();
  40. $arrRatingId = $this->getRequest()->getParam('ratings', []);
  41. foreach ($arrRatingId as $ratingId => $optionId) {
  42. $this->ratingFactory->create()
  43. ->setRatingId($ratingId)
  44. ->setReviewId($review->getId())
  45. ->addOptionVote($optionId, $productId);
  46. }
  47. $review->aggregate();
  48. $this->messageManager->addSuccess(__('You saved the review.'));
  49. if ($this->getRequest()->getParam('ret') == 'pending') {
  50. $resultRedirect->setPath('review/*/pending');
  51. } else {
  52. $resultRedirect->setPath('review/*/');
  53. }
  54. return $resultRedirect;
  55. } catch (LocalizedException $e) {
  56. $this->messageManager->addError($e->getMessage());
  57. } catch (\Exception $e) {
  58. $this->messageManager->addException($e, __('Something went wrong while saving this review.'));
  59. }
  60. }
  61. $resultRedirect->setPath('review/*/');
  62. return $resultRedirect;
  63. }
  64. }