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