Example #1
0
 /**
  * Show Send to a Friend Form
  *
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function execute()
 {
     $product = $this->_initProduct();
     if (!$product) {
         /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
         $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
         $resultForward->forward('noroute');
         return $resultForward;
     }
     if ($this->sendFriend->getMaxSendsToFriend() && $this->sendFriend->isExceedLimit()) {
         $this->messageManager->addNotice(__('You can\'t send messages more than %1 times an hour.', $this->sendFriend->getMaxSendsToFriend()));
     }
     /** @var \Magento\Framework\View\Result\Page $resultPage */
     $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
     $this->_eventManager->dispatch('sendfriend_product', ['product' => $product]);
     $data = $this->catalogSession->getSendfriendFormData();
     if ($data) {
         $this->catalogSession->setSendfriendFormData(true);
         $block = $resultPage->getLayout()->getBlock('sendfriend.send');
         if ($block) {
             /** @var \Magento\SendFriend\Block\Send $block */
             $block->setFormData($data);
         }
     }
     return $resultPage;
 }
Example #2
0
 /**
  * Send Email Post Action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function execute()
 {
     /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     if (!$this->_formKeyValidator->validate($this->getRequest())) {
         $resultRedirect->setPath('sendfriend/product/send', ['_current' => true]);
         return $resultRedirect;
     }
     $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();
         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;
 }