diff --git a/module/Organizations/config/module.config.php b/module/Organizations/config/module.config.php index 4459f2f51..07e959ca8 100644 --- a/module/Organizations/config/module.config.php +++ b/module/Organizations/config/module.config.php @@ -55,6 +55,7 @@ 'organizations/index/edit' => __DIR__ . '/../view/organizations/index/form.phtml', 'organizations/form/employees-fieldset' => __DIR__ . '/../view/form/employees-fieldset.phtml', 'organizations/form/employee-fieldset' => __DIR__ .'/../view/form/employee-fieldset.phtml', + 'organizations/error/no-parent' => __DIR__ . '/../view/error/no-parent.phtml', ), // Where to look for view templates not mapped above 'template_path_stack' => array( diff --git a/module/Organizations/src/Organizations/Controller/IndexController.php b/module/Organizations/src/Organizations/Controller/IndexController.php index 629da0302..728070569 100644 --- a/module/Organizations/src/Organizations/Controller/IndexController.php +++ b/module/Organizations/src/Organizations/Controller/IndexController.php @@ -11,6 +11,7 @@ namespace Organizations\Controller; use Auth\Exception\UnauthorizedAccessException; +use Core\Entity\Collection\ArrayCollection; use Core\Form\SummaryForm; use Zend\Mvc\Controller\AbstractActionController; use Organizations\Repository; @@ -18,6 +19,7 @@ use Zend\Session\Container as Session; use Zend\View\Model\JsonModel; use Core\Entity\PermissionsInterface; +use Zend\View\Model\ViewModel; /** * Main Action Controller for the Organization. @@ -121,7 +123,11 @@ public function editAction() $request = $this->getRequest(); $params = $this->params(); $formIdentifier = $params->fromQuery('form'); - $org = $this->getOrganization(true); + try { + $org = $this->getOrganization(true); + } catch (\RuntimeException $e) { + return $this->getErrorViewModel('no-parent'); + } $container = $this->getFormular($org); if (isset($formIdentifier) && $request->isPost()) { @@ -129,6 +135,14 @@ public function editAction() /* @var $form \Zend\Form\FormInterface */ $postData = $this->params()->fromPost(); $filesData = $this->params()->fromFiles(); + /* due to issues in ZF2 we need to clear the employees collection in the entity, + * prior to binding. Otherwise it is not possible to REMOVE an employee, as the + * MultiCheckbox Validation will FAIL on empty values! + */ + if ("employeesManagement" == $formIdentifier) { + $org->setEmployees(new ArrayCollection()); + } + $form = $container->get($formIdentifier); $form->setData(array_merge($postData, $filesData)); if (!isset($form)) { @@ -166,7 +180,7 @@ public function editAction() } return new JsonModel(array( - 'valid' => $form->isValid(), + 'valid' => $isValid, 'content' => $content, )); } @@ -265,4 +279,14 @@ protected function getOrganization($allowDraft = true) } return $organization; } + + protected function getErrorViewModel($script) + { + $this->getResponse()->setStatusCode(500); + + $model = new ViewModel(); + $model->setTemplate("organizations/error/$script"); + + return $model; + } } diff --git a/module/Organizations/src/Organizations/Entity/Organization.php b/module/Organizations/src/Organizations/Entity/Organization.php index a2049909d..cba3a5c7a 100644 --- a/module/Organizations/src/Organizations/Entity/Organization.php +++ b/module/Organizations/src/Organizations/Entity/Organization.php @@ -276,9 +276,9 @@ public function getPermissionsUserIds($type = null) /* @var $emp EmployeeInterface */ $perm = $emp->getPermissions(); if ($perm->isAllowed($change)) { - $spec[$change][] = $emp->getUser()->getId(); + $spec[PermissionsInterface::PERMISSION_CHANGE][] = $emp->getUser()->getId(); } else if ($perm->isAllowed($view)) { - $spec[$view][] = $emp->getUser()->getId(); + $spec[PermissionsInterface::PERMISSION_VIEW][] = $emp->getUser()->getId(); } } @@ -427,21 +427,13 @@ public function updatePermissions() /* @var $employees null | ArrayCollection | \Doctrine\ODM\MongoDB\PersistentCollection */ $employees = $organization->getEmployees(); - if ($employees && - ( $employees instanceof ArrayCollection - || $employees->isDirty() - || $employees->isInitialized()) - ) { - /* @var $perms Permissions */ - $perms = $this->getPermissions(); - - foreach ($employees as $emp) { - /* @var $emp \Organizations\Entity\Employee */ - $perms->grant($emp->getUser(), PermissionsInterface::PERMISSION_CHANGE, false); - } - $perms->build(); - } + $perms = $this->getPermissions(); + foreach ($employees as $emp) { + /* @var $emp \Organizations\Entity\Employee */ + $perms->grant($emp->getUser(), PermissionsInterface::PERMISSION_CHANGE, false); + } + $perms->build(); } public function setUser(UserInterface $user) { diff --git a/module/Organizations/src/Organizations/Form/EmployeeFieldset.php b/module/Organizations/src/Organizations/Form/EmployeeFieldset.php index 0099d6dfd..33cbb63f7 100644 --- a/module/Organizations/src/Organizations/Form/EmployeeFieldset.php +++ b/module/Organizations/src/Organizations/Form/EmployeeFieldset.php @@ -63,5 +63,4 @@ public function init() ), )); } - } \ No newline at end of file diff --git a/module/Organizations/src/Organizations/Form/Employees.php b/module/Organizations/src/Organizations/Form/Employees.php index 83d9f7bd8..f80740b90 100644 --- a/module/Organizations/src/Organizations/Form/Employees.php +++ b/module/Organizations/src/Organizations/Form/Employees.php @@ -29,6 +29,4 @@ class Employees extends SummaryForm // return; // No buttons. // } - - } \ No newline at end of file diff --git a/module/Organizations/src/Organizations/Form/EmployeesFieldset.php b/module/Organizations/src/Organizations/Form/EmployeesFieldset.php index 67098ca73..a72b239fa 100644 --- a/module/Organizations/src/Organizations/Form/EmployeesFieldset.php +++ b/module/Organizations/src/Organizations/Form/EmployeesFieldset.php @@ -65,6 +65,7 @@ public function init() 'should_create_template' => true, 'use_labeled_items' => false, 'allow_add' => true, + 'allow_remove' => true, 'renderFieldset' => true, 'target_element' => array( 'type' => 'Organizations/EmployeeFieldset' diff --git a/module/Organizations/src/Organizations/Repository/Organization.php b/module/Organizations/src/Organizations/Repository/Organization.php index 08282b70f..c4960ab7a 100644 --- a/module/Organizations/src/Organizations/Repository/Organization.php +++ b/module/Organizations/src/Organizations/Repository/Organization.php @@ -111,13 +111,17 @@ public function findByUser($userOrId) * do not want them to be queried here, so the query needs to check the * "parent" field, too. */ - $qb->addAnd( - $qb->expr()->field('user')->equals($userId), - $qb->expr()->addOr( - $qb->expr()->field('parent')->exists(false), - $qb->expr()->field('parent')->equals(null) - ) - ); +// $qb->addAnd( +// $qb->expr()->field('user')->equals($userId) +// ->addOr( +// $qb->expr()->addOr($qb->expr()->field('parent')->exists(false)) +// ->addOr($qb->expr()->field('parent')->equals(null)) +// ) +// ); + $qb->addAnd($qb->expr()->field('user')->equals($userId)) + ->addAnd($qb->expr()->addOr($qb->expr()->field('parent')->exists(false)) + ->addOr($qb->expr()->field('parent')->equals(null)) + ); $q = $qb->getQuery(); $entity = $q->getSingleResult(); @@ -140,7 +144,7 @@ public function findByEmployee($userOrId) * Employees collection is only set on main organization, * so here, we do not have to query the "parent" field. */ - $entity = $this->findOneBy(array('employees.user' => $userId)); + $entity = $this->findOneBy(array('employees.user' => new \MongoId($userId))); return $entity; } diff --git a/module/Organizations/view/error/no-parent.phtml b/module/Organizations/view/error/no-parent.phtml new file mode 100644 index 000000000..6633fbfb0 --- /dev/null +++ b/module/Organizations/view/error/no-parent.phtml @@ -0,0 +1,23 @@ + + * + * @author Mathias Gelhausen + */ +?> + +

translate('Sorry, you may not create organizations.'); ?>

+ +

translate('Before you are able to create organizations, you have to %screate your main organization%s first.'), + '', + '' + ) +?>

+ \ No newline at end of file