formKeyValidator = $formKeyValidator; $this->requestFactory = $requestFactory; $this->requestRepository = $requestRepository; $this->productRepository = $productRepository; $this->storeManager = $storeManager; $this->jsonEncoder = $jsonEncoder; $this->helper = $helper; $this->transportBuilder = $transportBuilder; $this->logger = $logger; $this->escapehtml = $escapehtml; $this->validator = $validator ?? ObjectManager::getInstance()->get(Validator::class); } public function execute() { $message = [ 'error' => __( 'Sorry. There is a problem with Your Quote Request.' . ' Please try again or use Contact Us link in the menu.' ) ]; if ($this->getRequest()->isPost()) { try { $data = $this->getValidData(); /** @var Request $model */ $model = $this->requestFactory->create(); $model->addData($data); $this->requestRepository->save($model); $message = ['success' => __('Thanks for contacting us. We\'ll respond to you as soon as possible. ')]; $this->sendAdminNotification($model); $this->sendAutoReply($model); } catch (LocalizedException $e) { $message = ['error' => $e->getMessage()]; } catch (\Exception $e) { $this->logger->error($e->getMessage()); } } $this->getResponse()->representJson( $this->jsonEncoder->encode($message) ); } /** * Validates all data * * @throws LocalizedException * @return array */ private function getValidData() { $data = $this->getRequest()->getPostValue(); if (!$this->formKeyValidator->validate($this->getRequest())) { throw new LocalizedException( __('Form key is not valid. Please try to reload the page.') ); } if (!$this->validator->isValidEmail($data['email'])) { throw new LocalizedException(__('Please enter a valid email address.')); } $data['name'] = $this->escapehtml->outputFilter(trim($data['name'])); if (!$this->validator->isNotEmpty($data['name'])) { throw new LocalizedException(__('Please enter a name.')); } $data['phone'] = $this->escapehtml->outputFilter(trim($data['phone'])); if (!$this->validator->isNotEmpty($data['phone'])) { throw new LocalizedException(__('Please enter a phone.')); } $data['product_id'] = (int)$data['product_id']; if (!$this->validator->isNotEmpty($data['product_id']) || !($product = $this->productRepository->getById($data['product_id'])) ) { throw new LocalizedException(__('There are no product for your request.')); } else { $data['product_name'] = $product->getName(); } if (array_key_exists('comment', $data)) { $data['comment'] = $this->escapehtml->outputFilter(trim($data['comment'])); } if ($this->helper->isGDPREnabled() && (!isset($data['gdpr']) || !$data['gdpr'])) { throw new LocalizedException(__('Please agree to the Privacy Policy')); } $data['store_id'] = $this->storeManager->getStore()->getId(); return $data; } /** * @param Request $model */ private function sendAdminNotification(Request $model) { $emailTo = trim($this->helper->getModuleConfig('admin_email/to')); if ($emailTo) { $emailTo = explode(',', $emailTo); $sender = $this->helper->getModuleConfig('admin_email/sender'); $template = $this->helper->getModuleConfig('admin_email/template'); $this->sendEmail($model, $sender, $emailTo, $template); } } /** * @param Request $model */ private function sendAutoReply(Request $model) { $enabled = $this->helper->getModuleConfig('reply_email/enabled'); if ($enabled) { $emailTo = [$model->getEmail()]; $sender = $this->helper->getModuleConfig('reply_email/sender'); $template = $this->helper->getModuleConfig('reply_email/template'); $this->sendEmail($model, $sender, $emailTo, $template); } } /** * @param Request $model * @param $sender * @param array $emailTo * @param $template */ private function sendEmail(Request $model, $sender, $emailTo, $template) { try { $store = $this->storeManager->getStore(); $first = array_shift($emailTo); $data = [ 'website_name' => $store->getWebsite()->getName(), 'group_name' => $store->getGroup()->getName(), 'store_name' => $store->getName(), 'store' => $store, 'request' => $model, 'customer_name' => $model->getName() ]; $transport = $this->transportBuilder->setTemplateIdentifier( $template )->setTemplateOptions( ['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $store->getId()] )->setTemplateVars( $data )->setFrom( $sender )->addTo( $first, $model->getName() )->addTo( $emailTo, $model->getName() )->getTransport(); $transport->sendMessage(); } catch (\Exception $e) { $this->logger->critical($e); } } }