/** * Write method - add/edit the service * * @package las * @version 1.0 * * @param string $method type: 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(['min' => 3, 'max' => 32])); $validation->add('chain', new Validator\PresenceOf()); $validation->add('chain', new Validator\InclusionIn(['domain' => Services::chain(), 'message' => __('Field :field must be a part of list: :domain', [':domain' => join(', ', Services::chain(true))])])); $validation->add('protocol', new Validator\InclusionIn(['domain' => Services::protocol(), 'allowEmpty' => true])); $validation->add('direction', new Extension\Together(['withOr' => ['client', 'device', 'IP'], 'allowEmpty' => true])); $validation->add('client', new Extension\Together(['with' => ['direction'], 'without' => ['device', 'IP'], 'allowEmpty' => true])); $validation->add('device', new Extension\Together(['with' => ['direction'], 'without' => ['IP'], 'allowEmpty' => true])); $validation->add('IP', new Extension\Together(['with' => ['direction'], 'allowEmpty' => true])); $validation->add('client', new Validator\InclusionIn(['domain' => Arr::from_model($this->clients, null, 'id'), 'allowEmpty' => true])); $validation->add('device', new Validator\InclusionIn(['domain' => Arr::from_model($this->devices, null, 'id'), 'allowEmpty' => true])); $validation->add('IP', new Extension\Ip(['value' => $this->request->getPost('IP'), 'allowEmpty' => true])); $validation->add('string', new Validator\StringLength(['max' => 128])); $validation->add('portDirection', new Extension\Together(['with' => ['startingPort'], 'allowEmpty' => true])); $validation->add('startingPort', new Validator\StringLength(['max' => 16])); $validation->add('startingPort', new Extension\Together(['with' => ['portDirection'], 'allowEmpty' => true])); $validation->add('endingPort', new Validator\Between(['minimum' => 1, 'maximum' => 65535, 'allowEmpty' => true])); $validation->add('endingPort', new Extension\Together(['with' => ['startingPort'], 'allowEmpty' => true])); $validation->add('priority', new Validator\InclusionIn(['domain' => Services::priority()])); $validation->add('sorting', new Validator\Between(['minimum' => 1, 'maximum' => 65535, 'allowEmpty' => true])); $validation->add('description', new Validator\StringLength(['max' => 1024])); $validation->add('status', new Validator\InclusionIn(['domain' => Services::status()])); $validation->setFilters('IP', 'ip2long'); $validation->setLabels(['name' => __('Name'), 'chain' => __('Chain'), 'protocol' => __('Protocol'), 'direction' => __('Direction'), 'client' => __('Client'), 'device' => __('Device'), 'IP' => __('IP'), 'string' => __('String'), 'portDirection' => __('Port direction'), 'startingPort' => __('Starting port'), 'endingPort' => __('Ending port'), 'lengthEnd' => __('Length end'), 'lengthStart' => __('Length start'), 'priority' => __('Priority'), 'sorting' => __('Sorting'), '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->chain = $this->request->getPost('chain', 'int'); $this->protocol = $this->request->getPost('protocol', 'int', null, true); $this->direction = $this->request->getPost('direction', 'int', null, true); $this->client_id = $this->request->getPost('client', 'int', 0, true); $this->device_id = $this->request->getPost('device', 'int', 0, true); $this->IP = $this->request->getPost('IP', 'ip2long'); $this->portDirection = $this->request->getPost('portDirection', 'int'); $this->startingPort = $this->request->getPost('startingPort', 'string', null, true); $this->endingPort = $this->request->getPost('endingPort', 'int', null, true); $this->internalStartingPort = $this->request->getPost('internalStartingPort', 'int', null, true); $this->internalEndingPort = $this->request->getPost('internalEndingPort', 'int', null, true); $this->priority = $this->request->getPost('priority', 'int', 0, true); $this->sorting = $this->request->getPost('sorting', 'int', 100, true); $this->description = $this->request->getPost('description', 'string', null, true); $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 service * * @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]) && ($service = Services::findFirst($params[0]))) { $clients = Clients::find(['status=:status:', 'bind' => ['status' => Clients::ACTIVE]]); $devices = Devices::find(['status=:status:', 'bind' => ['status' => Devices::ACTIVE]]); if (!count($clients)) { $this->flashSession->notice($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Notice') . '!</strong> ' . __("Please add the client first") . ': ' . $this->tag->linkTo('admin/clients/add', __('Add'))); } if (!count($devices)) { $this->flashSession->notice($this->tag->linkTo(['#', 'class' => 'close', 'title' => __("Close"), '×']) . '<strong>' . __('Notice') . '!</strong> ' . __("Please add the device first") . ': ' . $this->tag->linkTo('admin/devices/add', __('Add'))); } // Set title, pick view and send variables $this->tag->setTitle(__('Services') . ' / ' . __('Add')); $this->view->pick('services/write'); $this->view->setVars(['clients' => $clients, 'devices' => $devices, 'chain' => Services::chain(true), 'direction' => Services::direction(true), 'status' => Services::status(true), 'priority' => Services::priority(true), 'protocol' => Services::protocol(true), 'portDirection' => Services::portDirection(true)]); // Check if the form has been sent if ($this->request->isPost() === true && $this->request->hasPost('submit')) { $service->__set('clients', $clients); $service->__set('devices', $devices); $valid = $service->write('update'); // Check if data are valid if ($valid instanceof Services) { $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 = ['client' => $service->client_id, 'device' => $service->device_id, 'IP' => $service->IP ? long2ip($service->IP) : null]; // Values to fill out the form $this->tag->setDefaults(array_merge(get_object_vars($service), $diff)); } } else { parent::notFoundAction(); } }