/** * * Done * @return ViewModel */ public function createAction() { $request = $this->getRequest(); $entityManager = $this->getEntityManager(); $roles = $entityManager->getRepository('Authorization\\Entity\\Role')->findAll(); $formRoles = []; foreach ($roles as $role) { $formRoles[$role->getRoleId()] = $role->getRoleName(); } $roleForm = new RoleForm($formRoles); if ($request->isPost()) { $data = $request->getPost(); $roleForm->setInputFilter(new RoleFilter()); $roleForm->setData($data); if ($roleForm->isValid()) { $data = $roleForm->getData(); $role = new EntityRole(); $role->setRoleName($data['role_name']); $parents = new ArrayCollection(); foreach ($data['role_parent'] as $parentRoleId) { $parents->add($entityManager->getReference('Authorization\\Entity\\Role', $parentRoleId)); } $role->addParents($parents); try { $entityManager->persist($role); $entityManager->flush(); $this->redirect()->toRoute('authorization/role', array('action' => 'index')); } catch (Exception $ex) { return new ViewModel(array('message' => $ex->getCode() . ': ' . $ex->getMessage())); } } } return new ViewModel(array('roleForm' => $roleForm)); }
public function createRoleAction() { $this->assertGranted('authorization.role.create'); $view = new ViewModel(['form' => $this->roleForm]); $view->setTemplate('authorization/role/create'); if ($this->getRequest()->isPost()) { $data = $this->params()->fromPost(); $this->roleForm->setData($data); if ($this->roleForm->isValid()) { $this->roleService->createRole($this->roleForm); $this->roleService->flush(); $this->flashmessenger()->addSuccessMessage('Role created.'); return $this->redirect()->toUrl($this->referer()->fromStorage()); } } else { $this->referer()->store(); } return $view; }