123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?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;
- }
- }
|