Example #1
0
 /**
  * Method to save the configuration.
  *
  * @return  void
  */
 public function saveTask()
 {
     // Check for request forgeries.
     Request::checkToken() or exit(Lang::txt('JINVALID_TOKEN'));
     // Check if the user is authorized to do this.
     if (!User::authorise('core.admin')) {
         App::redirect(Route::url('index.php', false), Lang::txt('JERROR_ALERTNOAUTHOR'));
         return;
     }
     // Set FTP credentials, if given.
     \JClientHelper::setCredentialsFromRequest('ftp');
     // Initialise variables.
     $model = new Models\Application();
     $form = $model->getForm();
     $data = Request::getVar('jform', array(), 'post', 'array');
     // Validate the posted data.
     $return = $model->validate($form, $data);
     // Check for validation errors.
     if ($return === false) {
         // Get the validation messages.
         $errors = $model->getErrors();
         // Push up to three validation messages out to the user.
         for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
             if ($errors[$i] instanceof Exception) {
                 Notify::warning($errors[$i]->getMessage());
             } else {
                 Notify::warning($errors[$i]);
             }
         }
         // Save the data in the session.
         User::setState($this->_option . '.config.global.data', $data);
         // Redirect back to the edit screen.
         App::redirect(Route::url('index.php?option=' . $this->_option . '&view=application', false));
         return false;
     }
     try {
         // Attempt to save the configuration.
         $data = $return;
         $return = $model->save($data);
     } catch (\Hubzero\Config\Exception\FileNotFoundException $e) {
         $message = $e->getMessage();
         if (strpos($message, 'No configuration file found and no installation code available.') !== false) {
             error_log($message);
         } else {
             throw new Exception($message, 500);
         }
     }
     // Check the return value.
     if ($return === false) {
         // Save the data in the session.
         User::setState($this->_option . '.config.global.data', $data);
         // Save failed, go back to the screen and display a notice.
         App::redirect(Route::url('index.php?option=' . $this->_option . '&view=application', false), Lang::txt('JERROR_SAVE_FAILED', $model->getError()), 'error');
         return false;
     }
     // Set the success message.
     $message = Lang::txt('COM_CONFIG_SAVE_SUCCESS');
     // Set the redirect based on the task.
     switch (Request::getCmd('task')) {
         case 'apply':
             App::redirect(Route::url('index.php?option=' . $this->_option, false), $message);
             break;
         case 'save':
         default:
             App::redirect(Route::url('index.php', false), $message);
             break;
     }
 }