123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Contact\Controller\Index;
- use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
- use Magento\Contact\Model\ConfigInterface;
- use Magento\Contact\Model\MailInterface;
- use Magento\Framework\App\Action\Context;
- use Magento\Framework\App\Request\DataPersistorInterface;
- use Magento\Framework\Controller\Result\Redirect;
- use Magento\Framework\Exception\LocalizedException;
- use Psr\Log\LoggerInterface;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\DataObject;
- class Post extends \Magento\Contact\Controller\Index implements HttpPostActionInterface
- {
- /**
- * @var DataPersistorInterface
- */
- private $dataPersistor;
- /**
- * @var Context
- */
- private $context;
- /**
- * @var MailInterface
- */
- private $mail;
- /**
- * @var LoggerInterface
- */
- private $logger;
- /**
- * @param Context $context
- * @param ConfigInterface $contactsConfig
- * @param MailInterface $mail
- * @param DataPersistorInterface $dataPersistor
- * @param LoggerInterface $logger
- */
- public function __construct(
- Context $context,
- ConfigInterface $contactsConfig,
- MailInterface $mail,
- DataPersistorInterface $dataPersistor,
- LoggerInterface $logger = null
- ) {
- parent::__construct($context, $contactsConfig);
- $this->context = $context;
- $this->mail = $mail;
- $this->dataPersistor = $dataPersistor;
- $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
- }
- /**
- * Post user question
- *
- * @return Redirect
- */
- public function execute()
- {
- if (!$this->getRequest()->isPost()) {
- return $this->resultRedirectFactory->create()->setPath('*/*/');
- }
- try {
- $this->sendEmail($this->validatedParams());
- $this->messageManager->addSuccessMessage(
- __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
- );
- $this->dataPersistor->clear('contact_us');
- } catch (LocalizedException $e) {
- $this->messageManager->addErrorMessage($e->getMessage());
- $this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
- } catch (\Exception $e) {
- $this->logger->critical($e);
- $this->messageManager->addErrorMessage(
- __('An error occurred while processing your form. Please try again later.')
- );
- $this->dataPersistor->set('contact_us', $this->getRequest()->getParams());
- }
- return $this->resultRedirectFactory->create()->setPath('contact/index');
- }
- /**
- * @param array $post Post data from contact form
- * @return void
- */
- private function sendEmail($post)
- {
- $this->mail->send(
- $post['email'],
- ['data' => new DataObject($post)]
- );
- }
- /**
- * @return array
- * @throws \Exception
- */
- private function validatedParams()
- {
- $request = $this->getRequest();
- if (trim($request->getParam('name')) === '') {
- throw new LocalizedException(__('Enter the Name and try again.'));
- }
- if (trim($request->getParam('comment')) === '') {
- throw new LocalizedException(__('Enter the comment and try again.'));
- }
- if (false === \strpos($request->getParam('email'), '@')) {
- throw new LocalizedException(__('The email address is invalid. Verify the email address and try again.'));
- }
- if (trim($request->getParam('hideit')) !== '') {
- throw new \Exception();
- }
- return $request->getParams();
- }
- }
|