/** * Edit action - edit the network * * @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]) && ($network = Networks::findFirst($params[0]))) { // Set title, pick view and send variables $this->tag->setTitle(__('Networks') . ' / ' . __('Edit')); $this->view->pick('networks/write'); $this->view->setVars(['type' => Networks::type(true), 'status' => Networks::status(true), 'bitRate' => Settings::options('bitRate', $this->las['general']['bitRate'])]); // Check if the form has been sent if ($this->request->isPost() === true && $this->request->hasPost('submit')) { $valid = $network->write('update'); // Check if data are valid if ($valid instanceof Networks) { $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 = ['subnetwork' => long2ip($network->subnetwork), 'IP' => long2ip($network->IP), 'gateway' => long2ip($network->gateway)]; // Values to fill out the form $this->tag->setDefaults(array_merge(get_object_vars($network), $diff)); } } else { parent::notFoundAction(); } }
/** * Write method - add/edit the device * * @package las * @version 1.0 * * @param string $method type: create/update * @return mixed */ public function write($method = 'create') { $validation = new Extension\Validation(); $this->getDI()->getShared('filter')->add('mac', function ($mac) { return strtoupper(str_replace('-', ':', $mac)); }); $validation->add('name', new Validator\PresenceOf()); $validation->add('name', new Validator\StringLength(['min' => 3, 'max' => 32])); $validation->add('name', new Validator\Regex(['pattern' => '/([A-Z][A-Z0-9_-]{2,})/'])); $validation->add('network', new Validator\PresenceOf()); $validation->add('network', new Validator\InclusionIn(['domain' => Arr::from_model($this->networks, null, 'id')])); $validation->add('client', new Validator\PresenceOf()); $validation->add('client', new Validator\InclusionIn(['domain' => Arr::from_model($this->clients, null, 'id')])); $validation->add('type', new Validator\PresenceOf()); $validation->add('type', new Validator\InclusionIn(['domain' => Devices::type()])); $validation->add('IP', new Validator\PresenceOf()); $validation->add('IP', new Extension\Ip(['value' => $this->request->getPost('IP')])); $validation->add('IP', new Validator\ExclusionIn(['domain' => Arr::from_model($this->networks, null, 'IP'), 'message' => __('Field :field is reserved')])); $validation->add('IP', new Extension\Uniqueness(['model' => __CLASS__, 'except' => isset($this->IP) ? $this->IP : null])); $network = Networks::findFirst($this->request->getPost('network', 'int')); $validation->add('IP', new Extension\Cidr(['cidr' => $network->subnetwork . $network->mask])); $validation->add('MAC', new Validator\PresenceOf()); $validation->add('MAC', new Validator\Regex(['pattern' => '/([0-9a-fA-F]{2}[:|\\-]){5}[0-9a-fA-F]{2}/'])); $validation->add('MAC', new Extension\Uniqueness(['model' => __CLASS__, 'except' => isset($this->MAC) ? $this->MAC : null])); $validation->add('description', new Validator\StringLength(['max' => 1024])); $validation->add('status', new Validator\InclusionIn(['domain' => Devices::status()])); $validation->setFilters('IP', 'ip2long'); $validation->setFilters('MAC', 'mac'); $validation->setLabels(['name' => __('Name'), 'network' => __('Network'), 'client' => __('Client'), 'type' => __('Type'), 'IP' => __('IP'), 'MAC' => __('MAC'), '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->network_id = $this->request->getPost('network', 'int'); $this->client_id = $this->request->getPost('client', 'int'); $this->type = $this->request->getPost('type', 'int'); $this->IP = $this->request->getPost('IP', 'ip2long'); $this->MAC = $this->request->getPost('MAC', 'mac'); $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(); } } }