Sendmail.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SendFriend\Controller\Product;
  7. use Magento\Framework\App\Action\HttpPostActionInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Framework\Controller\ResultFactory;
  11. use Magento\SendFriend\Model\CaptchaValidator;
  12. /**
  13. * Class Sendmail. Represents request flow logic of 'sendmail' feature
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class Sendmail extends \Magento\SendFriend\Controller\Product implements HttpPostActionInterface
  18. {
  19. /**
  20. * @var \Magento\Catalog\Api\CategoryRepositoryInterface
  21. */
  22. protected $categoryRepository;
  23. /**
  24. * @var \Magento\Catalog\Model\Session
  25. */
  26. protected $catalogSession;
  27. /**
  28. * @var CaptchaValidator
  29. */
  30. private $captchaValidator;
  31. /**
  32. * Sendmail class construct
  33. *
  34. * @param \Magento\Framework\App\Action\Context $context
  35. * @param \Magento\Framework\Registry $coreRegistry
  36. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  37. * @param \Magento\SendFriend\Model\SendFriend $sendFriend
  38. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  39. * @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
  40. * @param \Magento\Catalog\Model\Session $catalogSession
  41. * @param CaptchaValidator|null $captchaValidator
  42. */
  43. public function __construct(
  44. \Magento\Framework\App\Action\Context $context,
  45. \Magento\Framework\Registry $coreRegistry,
  46. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
  47. \Magento\SendFriend\Model\SendFriend $sendFriend,
  48. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
  49. \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
  50. \Magento\Catalog\Model\Session $catalogSession,
  51. CaptchaValidator $captchaValidator = null
  52. ) {
  53. parent::__construct($context, $coreRegistry, $formKeyValidator, $sendFriend, $productRepository);
  54. $this->categoryRepository = $categoryRepository;
  55. $this->catalogSession = $catalogSession;
  56. $this->captchaValidator = $captchaValidator ?: ObjectManager::getInstance()->create(CaptchaValidator::class);
  57. }
  58. /**
  59. * Send Email Post Action
  60. *
  61. * @return \Magento\Framework\Controller\ResultInterface
  62. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  63. * @SuppressWarnings(PHPMD.NPathComplexity)
  64. */
  65. public function execute()
  66. {
  67. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  68. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  69. $product = $this->_initProduct();
  70. $data = $this->getRequest()->getPostValue();
  71. if (!$product || !$data) {
  72. /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
  73. $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
  74. $resultForward->forward('noroute');
  75. return $resultForward;
  76. }
  77. $categoryId = $this->getRequest()->getParam('cat_id', null);
  78. if ($categoryId) {
  79. try {
  80. $category = $this->categoryRepository->get($categoryId);
  81. } catch (NoSuchEntityException $noEntityException) {
  82. $category = null;
  83. }
  84. if ($category) {
  85. $product->setCategory($category);
  86. $this->_coreRegistry->register('current_category', $category);
  87. }
  88. }
  89. $this->sendFriend->setSender($this->getRequest()->getPost('sender'));
  90. $this->sendFriend->setRecipients($this->getRequest()->getPost('recipients'));
  91. $this->sendFriend->setProduct($product);
  92. try {
  93. $validate = $this->sendFriend->validate();
  94. $this->captchaValidator->validateSending($this->getRequest());
  95. if ($validate === true) {
  96. $this->sendFriend->send();
  97. $this->messageManager->addSuccess(__('The link to a friend was sent.'));
  98. $url = $product->getProductUrl();
  99. $resultRedirect->setUrl($this->_redirect->success($url));
  100. return $resultRedirect;
  101. } else {
  102. if (is_array($validate)) {
  103. foreach ($validate as $errorMessage) {
  104. $this->messageManager->addError($errorMessage);
  105. }
  106. } else {
  107. $this->messageManager->addError(__('We found some problems with the data.'));
  108. }
  109. }
  110. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  111. $this->messageManager->addError($e->getMessage());
  112. } catch (\Exception $e) {
  113. $this->messageManager->addException($e, __('Some emails were not sent.'));
  114. }
  115. // save form data
  116. $this->catalogSession->setSendfriendFormData($data);
  117. $url = $this->_url->getUrl('sendfriend/product/send', ['_current' => true]);
  118. $resultRedirect->setUrl($this->_redirect->error($url));
  119. return $resultRedirect;
  120. }
  121. }