Example #1
0
 /**
  * Save action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function execute()
 {
     $data = $this->getRequest()->getPostValue();
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultRedirectFactory->create();
     if ($data) {
         /** @var \Swissup\Testimonials\Model\Data $model */
         $model = $this->_objectManager->create('Swissup\\Testimonials\\Model\\Data');
         $id = $this->getRequest()->getParam('testimonial_id');
         if ($id) {
             $model->load($id);
         }
         $model->setData($data);
         $this->_eventManager->dispatch('testimonial_prepare_save', ['testimonial' => $model, 'request' => $this->getRequest()]);
         $imageName = $this->uploadModel->uploadFileAndGetName('image', $this->imageModel->getBaseDir(), $data);
         $model->setImage($imageName);
         try {
             $model->save();
             $this->messageManager->addSuccess(__('Testimonial has been saved.'));
             $this->_objectManager->get('Magento\\Backend\\Model\\Session')->setFormData(false);
             if ($this->getRequest()->getParam('back')) {
                 return $resultRedirect->setPath('*/*/edit', ['testimonial_id' => $model->getId(), '_current' => true]);
             }
             return $resultRedirect->setPath('*/*/');
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\RuntimeException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (\Exception $e) {
             $this->messageManager->addException($e, __('Something went wrong while saving the testimonial.'));
         }
         $this->_getSession()->setFormData($data);
         return $resultRedirect->setPath('*/*/edit', ['testimonial_id' => $this->getRequest()->getParam('testimonial_id')]);
     }
     return $resultRedirect->setPath('*/*/');
 }
Example #2
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;
     }
 }