Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Sendmail.php
Go to the documentation of this file.
1 <?php
8 
11 
13 {
18 
22  protected $catalogSession;
23 
33  public function __construct(
34  \Magento\Framework\App\Action\Context $context,
35  \Magento\Framework\Registry $coreRegistry,
36  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
37  \Magento\SendFriend\Model\SendFriend $sendFriend,
38  \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
39  \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
40  \Magento\Catalog\Model\Session $catalogSession
41  ) {
42  parent::__construct($context, $coreRegistry, $formKeyValidator, $sendFriend, $productRepository);
43  $this->categoryRepository = $categoryRepository;
44  $this->catalogSession = $catalogSession;
45  }
46 
53  public function execute()
54  {
56  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
57 
58  if (!$this->_formKeyValidator->validate($this->getRequest())) {
59  $resultRedirect->setPath('sendfriend/product/send', ['_current' => true]);
60  return $resultRedirect;
61  }
62 
63  $product = $this->_initProduct();
64  $data = $this->getRequest()->getPostValue();
65 
66  if (!$product || !$data) {
68  $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
69  $resultForward->forward('noroute');
70  return $resultForward;
71  }
72 
73  $categoryId = $this->getRequest()->getParam('cat_id', null);
74  if ($categoryId) {
75  try {
76  $category = $this->categoryRepository->get($categoryId);
77  } catch (NoSuchEntityException $noEntityException) {
78  $category = null;
79  }
80  if ($category) {
81  $product->setCategory($category);
82  $this->_coreRegistry->register('current_category', $category);
83  }
84  }
85 
86  $this->sendFriend->setSender($this->getRequest()->getPost('sender'));
87  $this->sendFriend->setRecipients($this->getRequest()->getPost('recipients'));
88  $this->sendFriend->setProduct($product);
89 
90  try {
91  $validate = $this->sendFriend->validate();
92  if ($validate === true) {
93  $this->sendFriend->send();
94  $this->messageManager->addSuccess(__('The link to a friend was sent.'));
95  $url = $product->getProductUrl();
96  $resultRedirect->setUrl($this->_redirect->success($url));
97  return $resultRedirect;
98  } else {
99  if (is_array($validate)) {
100  foreach ($validate as $errorMessage) {
101  $this->messageManager->addError($errorMessage);
102  }
103  } else {
104  $this->messageManager->addError(__('We found some problems with the data.'));
105  }
106  }
107  } catch (\Magento\Framework\Exception\LocalizedException $e) {
108  $this->messageManager->addError($e->getMessage());
109  } catch (\Exception $e) {
110  $this->messageManager->addException($e, __('Some emails were not sent.'));
111  }
112 
113  // save form data
114  $this->catalogSession->setSendfriendFormData($data);
115 
116  $url = $this->_url->getUrl('sendfriend/product/send', ['_current' => true]);
117  $resultRedirect->setUrl($this->_redirect->error($url));
118  return $resultRedirect;
119  }
120 }
_redirect($path, $arguments=[])
Definition: Action.php:167
__()
Definition: __.php:13
__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)
Definition: Sendmail.php:33