Post.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Controller\Product;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Review\Controller\Product as ProductController;
  9. use Magento\Framework\Controller\ResultFactory;
  10. use Magento\Review\Model\Review;
  11. class Post extends ProductController implements HttpPostActionInterface
  12. {
  13. /**
  14. * Submit new review action
  15. *
  16. * @return \Magento\Framework\Controller\Result\Redirect
  17. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  18. * @SuppressWarnings(PHPMD.NPathComplexity)
  19. */
  20. public function execute()
  21. {
  22. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  23. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  24. if (!$this->formKeyValidator->validate($this->getRequest())) {
  25. $resultRedirect->setUrl($this->_redirect->getRefererUrl());
  26. return $resultRedirect;
  27. }
  28. $data = $this->reviewSession->getFormData(true);
  29. if ($data) {
  30. $rating = [];
  31. if (isset($data['ratings']) && is_array($data['ratings'])) {
  32. $rating = $data['ratings'];
  33. }
  34. } else {
  35. $data = $this->getRequest()->getPostValue();
  36. $rating = $this->getRequest()->getParam('ratings', []);
  37. }
  38. if (($product = $this->initProduct()) && !empty($data)) {
  39. /** @var \Magento\Review\Model\Review $review */
  40. $review = $this->reviewFactory->create()->setData($data);
  41. $review->unsetData('review_id');
  42. $validate = $review->validate();
  43. if ($validate === true) {
  44. try {
  45. $review->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
  46. ->setEntityPkValue($product->getId())
  47. ->setStatusId(Review::STATUS_PENDING)
  48. ->setCustomerId($this->customerSession->getCustomerId())
  49. ->setStoreId($this->storeManager->getStore()->getId())
  50. ->setStores([$this->storeManager->getStore()->getId()])
  51. ->save();
  52. foreach ($rating as $ratingId => $optionId) {
  53. $this->ratingFactory->create()
  54. ->setRatingId($ratingId)
  55. ->setReviewId($review->getId())
  56. ->setCustomerId($this->customerSession->getCustomerId())
  57. ->addOptionVote($optionId, $product->getId());
  58. }
  59. $review->aggregate();
  60. $this->messageManager->addSuccess(__('You submitted your review for moderation.'));
  61. } catch (\Exception $e) {
  62. $this->reviewSession->setFormData($data);
  63. $this->messageManager->addError(__('We can\'t post your review right now.'));
  64. }
  65. } else {
  66. $this->reviewSession->setFormData($data);
  67. if (is_array($validate)) {
  68. foreach ($validate as $errorMessage) {
  69. $this->messageManager->addError($errorMessage);
  70. }
  71. } else {
  72. $this->messageManager->addError(__('We can\'t post your review right now.'));
  73. }
  74. }
  75. }
  76. $redirectUrl = $this->reviewSession->getRedirectUrl(true);
  77. $resultRedirect->setUrl($redirectUrl ?: $this->_redirect->getRedirectUrl());
  78. return $resultRedirect;
  79. }
  80. }