コード例 #1
0
 public function send()
 {
     // Check for request forgeries.
     JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
     // Get the data from the form POST
     $data = $this->input->post->get('cfreport', array(), 'array');
     $itemId = Joomla\Utilities\ArrayHelper::getValue($data, 'id');
     if (!$itemId) {
         $redirectOptions = array('force_direction' => CrowdfundingHelperRoute::getReportRoute());
         $this->displayNotice(JText::_('COM_CROWDFUNDING_ERROR_INVALID_PROJECT'), $redirectOptions);
         return;
     }
     // Get project
     $item = Crowdfunding\Project::getInstance(JFactory::getDbo(), $itemId);
     $redirectOptions = array('force_direction' => CrowdfundingHelperRoute::getReportRoute($item->getId()));
     $model = $this->getModel();
     /** @var $model CrowdfundingModelReport */
     $form = $model->getForm($data, false);
     /** @var $form JForm */
     if (!$form) {
         throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_FORM_CANNOT_BE_LOADED'));
     }
     // Test if the data is valid.
     $validData = $model->validate($form, $data);
     // Check for validation errors.
     if ($validData === false) {
         $errors = $form->getErrors();
         $error = array_shift($errors);
         $msg = $error->getMessage();
         $this->displayNotice($msg, $redirectOptions);
         return;
     }
     try {
         $userId = (int) JFactory::getUser()->get('id');
         if ($userId > 0) {
             $validData['user_id'] = $userId;
         }
         $model->save($validData);
     } catch (RuntimeException $e) {
         $this->displayNotice($e->getMessage(), $redirectOptions);
         return;
     } catch (Exception $e) {
         JLog::add($e->getMessage(), JLog::ERROR, 'com_crowdfunding');
         throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM'));
     }
     // Redirect to next page
     $this->displayMessage(JText::_('COM_CROWDFUNDING_REPORT_SENT_SUCCESSFULLY'), $redirectOptions);
 }