Ejemplo n.º 1
0
 /**
  * Edit action method
  *
  * @return void
  */
 public function edit($id)
 {
     $role = new Model\Role();
     $role->getById($id);
     if (!isset($role->id)) {
         $this->redirect('/roles');
     }
     $this->prepareView('roles/edit.phtml');
     $this->view->title = 'Roles';
     $this->view->role_name = $role->name;
     $fields = $this->application->config()['forms']['App\\Form\\Role'];
     $config = $this->application->config();
     $resources = ['----' => '----'];
     $parents = ['----' => '----'];
     $roles = (new Model\Role())->getAll();
     if (count($roles) > 0) {
         foreach ($roles as $r) {
             if ($r['id'] != $id) {
                 $parents[$r['id']] = $r['name'];
             }
         }
     }
     foreach ($config['resources'] as $resource => $perms) {
         if (strpos($resource, '|') !== false) {
             $resource = explode('|', $resource);
             $resources[$resource[0]] = $resource[1];
         } else {
             $resources[$resource] = $resource;
         }
     }
     $fields[0]['role_parent_id']['value'] = $parents;
     $fields[1]['name']['attributes']['onkeyup'] = 'pop.changeTitle(this.value);';
     $fields[2]['resource_1']['value'] = $resources;
     $this->view->form = new Form\Role($fields);
     $this->view->form->addFilter('htmlentities', [ENT_QUOTES, 'UTF-8'])->setFieldValues($role->toArray());
     if ($this->request->isPost()) {
         $this->view->form->addFilter('strip_tags')->setFieldValues($this->request->getPost());
         if ($this->view->form->isValid()) {
             $role = new Model\Role();
             $role->update($this->request->getPost(), $this->sess);
             $this->view->id = $role->id;
             $this->sess->setRequestValue('saved', true);
             $this->redirect('/roles/edit/' . $role->id);
         }
     }
     $this->send();
 }