Example #1
0
 /**
  * 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;
     }
 }