public function run() { $faker = \Faker\Factory::create(); Roles::truncate(); $roles = [['name' => 'admin', 'display_name' => 'Administrator', 'discription' => 'Administrator', 'created_at' => new DateTime(), 'updated_at' => new DateTime()], ['name' => 'member', 'display_name' => 'Member', 'discription' => 'Member', 'created_at' => new DateTime(), 'updated_at' => new DateTime()]]; foreach ($roles as $role) { Roles::insert($role); } }
function editAction() { $request = new Bolts_Request($this->getRequest()); $roles_table = new Roles(); $role = null; if ($request->has('id')) { if (!is_null($request->id)) { $role = $roles_table->fetchRow($roles_table->select()->where("id = ?", $request->id)); if (!is_null($role)) { // we do not edit the guest role if ($role->shortname == "guest") { $this->_redirect("/bolts/role"); } $this->view->role = $role->toArray(); $this->view->role_tree = $roles_table->getRoleTree(null, $role->id); $this->view->inherited_ids = $roles_table->getInheritedRoles($role->id); } } } if (is_null($role)) { $this->view->role_tree = $roles_table->getRoleTree(); } if ($this->getRequest()->isPost()) { $errors = array(); if ($request->has('inherit_role')) { $parents = array(); foreach ($request->inherit_role as $inherit_role) { $parents = array_merge($parents, $roles_table->getAllAncestors($inherit_role)); } $inherit_ids = array(); foreach ($request->inherit_role as $inherit_role) { if (!in_array($inherit_role, $parents)) { $inherit_ids[] = $inherit_role; } } } if ($request->has('shortname')) { $shortname = $request->shortname; if (!Bolts_Validate::checkLength($request->shortname, 1, 255)) { $errors[] = $this->_T("Shortname must be between 1 and 255 chars."); } } else { $errors[] = $this->_T("Shortname is a requried field."); } $description = $request->description; $isadmin = (int) $request->checkbox('isadmin'); if (count($errors) == 0) { $data = array('shortname' => $shortname, 'description' => $description, 'isadmin' => $isadmin); //If we have an id, this is an update. $id = (int) $this->_request->getPost('id'); if ($id != 0) { $where = 'id = ' . $id; $roles_table->update($data, $where); } else { //We don't, this is an insert. $id = $roles_table->insert($data); } $roles_table->removeInheritedRole($id); foreach ($inherit_ids as $in_id) { $roles_table->setInheritedRole($id, $in_id); } $this->_redirect("/bolts/role"); } else { $this->view->errors = $errors; } } if ($request->has('id')) { // this is an edit $id = $request->id; if ($id > 0) { $this->view->role = $roles_table->fetchRow('id = ' . $id)->toArray(); } $this->view->inherited_ids = $roles_table->getInheritedRoles($id); } else { foreach ($roles_table->fetchAll()->toArray() as $role) { $role_choices[$role['id']] = $role['shortname']; } $this->view->role_choices = $role_choices; } }