/** * Check CAPTCHA on New Testmonial page * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { $formId = 'testimonial_new'; $captcha = $this->_helper->getCaptcha($formId); if ($captcha->isRequired()) { /** @var \Magento\Framework\App\Action\Action $controller */ $controller = $observer->getControllerAction(); if (!$captcha->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) { $this->_testimonialSession->setFormData($controller->getRequest()->getPost()); $this->messageManager->addError(__('Incorrect CAPTCHA.')); $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true); $this->redirect->redirect($controller->getResponse(), $this->redirect->getRefererUrl()); } } }
/** * Dispatch request * * @param RequestInterface $request * @return \Magento\Framework\App\ResponseInterface */ public function dispatch(RequestInterface $request) { $allowGuest = $this->_objectManager->get('Magento\\Review\\Helper\\Data')->getIsGuestAllowToWrite(); if (!$request->isDispatched()) { return parent::dispatch($request); } if (!$allowGuest && $request->getActionName() == 'post' && $request->isPost()) { if (!$this->_customerSession->isLoggedIn()) { $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true); $this->_customerSession->setBeforeAuthUrl($this->_url->getUrl('*/*/*', ['_current' => true])); $this->_reviewSession->setFormData($request->getPostValue())->setRedirectUrl($this->_redirect->getRefererUrl()); $this->getResponse()->setRedirect($this->_objectManager->get('Magento\\Customer\\Model\\Url')->getLoginUrl()); } } return parent::dispatch($request); }
/** * Save user testimonial * * @return void * @throws \Exception */ public function execute() { $post = $this->getRequest()->getPostValue(); if (!$post) { $this->_redirectReferer(); return; } try { $error = false; if (!\Zend_Validate::is(trim($post['name']), 'NotEmpty')) { $error = true; } if (!\Zend_Validate::is(trim($post['message']), 'NotEmpty')) { $error = true; } if (!\Zend_Validate::is(trim($post['email']), 'EmailAddress')) { $error = true; } if ($error) { throw new \Exception(); } $post['store_id'] = $this->_storeManager->getStore()->getId(); $post['status'] = $this->_configHelper->isAutoApprove() ? TestimonialsModel::STATUS_ENABLED : TestimonialsModel::STATUS_AWAITING_APPROVAL; $model = $this->_objectManager->create('Swissup\\Testimonials\\Model\\Data'); $model->setData($post); $imageName = $this->_uploadModel->uploadFileAndGetName('image', $this->_imageModel->getBaseDir(), $post, ['jpg', 'jpeg', 'gif', 'png', 'bmp']); $model->setImage($imageName); $this->_eventManager->dispatch('testimonials_save_new', ['item' => $model]); $model->save(); $this->messageManager->addSuccess(__($this->_configHelper->getSentMessage())); $this->_redirectReferer(); return; } catch (\Exception $e) { $this->messageManager->addError(__($e->getMessage())); $this->_testimonialSession->setFormData($post)->setRedirectUrl($this->_redirect->getRefererUrl()); $this->_redirectReferer(); return; } }