public function save()
{
$this->events()->trigger(__CLASS__, 'before.save', $this);
$arraySave = array('name' => $this->getName(), 'description' => $this->getDescription());
try {
$roleId = $this->getId();
if (empty($roleId)) {
$this->insert($arraySave);
$this->setId($this->getLastInsertId());
} else {
$this->update($arraySave, array('id' => $this->getId()));
}
$permissions = $this->getPermissions();
if (!empty($permissions)) {
$aclTable = new TableGateway('user_acl', $this->getAdapter());
$aclTable->delete(array('user_acl_role_id' => $this->getId()));
foreach ($permissions as $permissionId => $value) {
if (!empty($value)) {
$aclTable->insert(array('user_acl_role_id' => $this->getId(), 'user_acl_permission_id' => $permissionId));
}
}
}
$this->events()->trigger(__CLASS__, 'after.save', $this);
return $this->getId();
} catch (\Exception $e) {
$this->events()->trigger(__CLASS__, 'after.save.failed', $this);
throw new \Gc\Exception($e->getMessage(), $e->getCode(), $e);
}
}
/** * Create role * * @return \Zend\View\Model\ViewModel|array */ public function createAction() { $form = new RoleForm(); $form->initPermissions(); $form->setAttribute('action', $this->url()->fromRoute('config/user/role/create')); if ($this->getRequest()->isPost()) { $post = $this->getRequest()->getPost()->toArray(); $form->setData($post); if ($form->isValid()) { $roleModel = new Role\Model(); $roleModel->addData($form->getInputFilter()->getValues()); $roleModel->save(); $this->flashMessenger()->addSuccessMessage('Role saved!'); return $this->redirect()->toRoute('config/user/role/edit', array('id' => $roleModel->getId())); } $this->flashMessenger()->addErrorMessage('Role can not saved!'); $this->useFlashMessenger(); } return array('form' => $form); }