/** * Write method - add/edit the firewall * * @package las * @version 1.0 * * @param string $method type of method: create/update * @return mixed */ public function write($method = 'create') { $validation = new Extension\Validation(); $validation->add('name', new Validator\PresenceOf()); $validation->add('name', new Validator\StringLength(['max' => 32])); $validation->add('content', new Validator\PresenceOf()); $validation->add('description', new Validator\StringLength(['max' => 1024])); $validation->add('status', new Validator\InclusionIn(['domain' => Firewalls::status()])); $validation->setLabels(['name' => __('Name'), 'content' => __('Content'), 'description' => __('Description'), 'status' => __('Status')]); $messages = $validation->validate($_POST); // Return messages if validation not pass if (count($messages)) { return $validation->getMessages(); } else { $this->name = $this->request->getPost('name', 'string'); $this->content = $this->request->getPost('content'); $this->description = $this->request->getPost('description', 'string'); $this->status = $this->request->getPost('status', 'int'); $this->date = date('Y-m-d H:i:s'); // Try to write the record if ($this->{$method}() === true) { return $this; } else { \Las\Bootstrap::log($this->getMessages()); return $this->getMessages(); } } }
/** * Edit action - edit the firewall * * @package las * @version 1.0 */ public function editAction() { // Get id from url params and check if record exist $params = $this->router->getParams(); if (isset($params[0]) && ($firewall = Firewalls::findFirst($params[0]))) { // Set title, pick view and send variables $this->tag->setTitle(__('firewalls') . ' / ' . __('Edit')); $this->view->pick('firewalls/write'); $this->view->setVars(['status' => Firewalls::status(true)]); // Check if the form has been sent if ($this->request->isPost() === true && $this->request->hasPost('submit')) { $valid = $firewall->write('update'); // Check if data are valid if ($valid instanceof Firewalls) { $this->flashSession->success($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Success') . '!</strong> ' . __("The data has been saved.")); } else { $this->view->setVar('errors', $valid); $this->flashSession->warning($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Warning') . '!</strong> ' . __("Please correct the errors.")); } } else { $diff = ['status' => $firewall->status == Firewalls::COMPILED ? Firewalls::ACTIVE : $firewall->status]; // Values to fill out the form $this->tag->setDefaults(array_merge(get_object_vars($firewall), $diff)); } } else { parent::notFoundAction(); } }