Beispiel #1
0
 /**
  * Save a handler
  *
  * @return void
  */
 public function saveTask()
 {
     // Check for request forgeries
     Request::checkToken();
     $handler = Handler::oneOrNew(Request::getInt('id'))->set(['tool_id' => Request::getInt('tool'), 'prompt' => Request::getString('prompt')]);
     $rules = [];
     // Set the rule info on the handler
     foreach (Request::getVar('rules', array(), 'post') as $rule) {
         // First check and make sure we don't save a completely empty rule
         if (empty($rule['extension']) && empty($rule['quantity'])) {
             break;
         }
         $rules[] = Rule::oneOrNew(isset($rule['id']) ? $rule['id'] : 0)->set($rule);
     }
     // Save the handler info
     if (!$handler->save()) {
         // Something went wrong...return errors
         foreach ($handler->getErrors() as $error) {
             Notify::error($error);
         }
         // Attach the rules so that we don't lose them
         $handler->attach('rules', $rules);
         $this->view->setLayout('edit');
         $this->view->task = 'edit';
         $this->editTask($handler);
         return;
     }
     // Now try to save the rules
     if (!$handler->rules()->saveAll($rules)) {
         foreach ($rules as $rule) {
             // Something went wrong...return errors
             foreach ($rule->getErrors() as $error) {
                 Notify::error($error);
             }
         }
         // Attach the rules so that we don't lose them
         $handler->attach('rules', $rules);
         $this->view->setLayout('edit');
         $this->view->task = 'edit';
         $this->editTask($handler);
         return;
     }
     // Grab the array ids
     $ids = array_map(function ($rule) {
         return $rule->id;
     }, $rules);
     // Now process the implicit deletes
     foreach (Rule::whereEquals('handler_id', $handler->id) as $rule) {
         if (!in_array($rule->id, $ids)) {
             $rule->destroy();
         }
     }
     // Redirect
     App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_TOOLS_HANDLER_SUCCESSFULLY_SAVED'));
 }