/** * 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 network * * @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('interface', new Validator\PresenceOf()); $validation->add('interface', new Validator\StringLength(['max' => 32])); $validation->add('interface', new Extension\Uniqueness(['model' => __CLASS__, 'except' => isset($this->interface) ? $this->interface : null])); $validation->add('subnetwork', new Validator\PresenceOf()); $validation->add('subnetwork', new Extension\Ip(['value' => $this->request->getPost('subnetwork')])); $validation->add('subnetwork', new Extension\Uniqueness(['model' => __CLASS__, 'except' => isset($this->subnetwork) ? $this->subnetwork : null])); $validation->add('type', new Validator\PresenceOf()); $validation->add('type', new Validator\InclusionIn(['domain' => Networks::type()])); $validation->add('IP', new Validator\PresenceOf()); $validation->add('IP', new Extension\Ip(['value' => $this->request->getPost('IP')])); $validation->add('IP', new Extension\Uniqueness(['model' => __CLASS__, 'except' => isset($this->IP) ? $this->IP : null])); $validation->add('gateway', new Validator\PresenceOf()); $validation->add('gateway', new \Las\Extension\Ip(['value' => $this->request->getPost('gateway')])); $validation->add('DNS', new Extension\Dns(['allowEmpty' => true])); $validation->add('DNS', new Extension\Together(['with' => ['type' => Networks::WAN], 'allowEmpty' => true])); $validation->add('DHCP', new Extension\Dhcp(['allowEmpty' => true])); $validation->add('DHCP', new Extension\Together(['with' => ['type' => Networks::LAN], 'allowEmpty' => true])); $validation->add('mask', new Validator\InclusionIn(['domain' => array_keys(Networks::mask())])); $validation->add('download', new Validator\Between(['minimum' => 0, 'maximum' => 1000])); $validation->add('upload', new Validator\Between(['minimum' => 0, 'maximum' => 1000])); $validation->add('description', new Validator\StringLength(['max' => 1024])); $validation->add('status', new Validator\InclusionIn(['domain' => Networks::status()])); $validation->setFilters('subnetwork', 'ip2long'); $validation->setFilters('IP', 'ip2long'); $validation->setFilters('gateway', 'ip2long'); $validation->setLabels(['name' => __('Name'), 'interface' => __('Interface'), 'subnetwork' => __('Subnetwork'), 'type' => __('Type'), 'IP' => __('IP'), 'gateway' => __('Gateway'), 'DNS' => __('DNS'), 'DHCP' => __('DHCP'), 'mask' => __('Mask'), 'download' => __('Download'), 'upload' => __('Upload'), '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->interface = $this->request->getPost('interface', 'string'); $this->subnetwork = $this->request->getPost('subnetwork', 'ip2long'); $this->type = $this->request->getPost('type', 'int'); $this->IP = $this->request->getPost('IP', 'ip2long'); $this->mask = $this->request->getPost('mask', 'string'); $this->gateway = $this->request->getPost('gateway', 'ip2long'); $this->DNS = $this->request->getPost('DNS', 'string'); $this->DHCP = $this->request->getPost('DHCP', 'string'); $this->download = $this->request->getPost('download', 'int', 0, true); $this->upload = $this->request->getPost('upload', 'int', 0, true); $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(); } } }