Skip to content

Commit

Permalink
[Organizations] Fixes and optimizes code relating to organizations em…
Browse files Browse the repository at this point in the history
…ployees management.
  • Loading branch information
TiSiE committed Apr 10, 2015
1 parent 9468239 commit fecbe69
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 29 deletions.
1 change: 1 addition & 0 deletions module/Organizations/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
namespace Organizations\Controller;

use Auth\Exception\UnauthorizedAccessException;
use Core\Entity\Collection\ArrayCollection;
use Core\Form\SummaryForm;
use Zend\Mvc\Controller\AbstractActionController;
use Organizations\Repository;
use Organizations\Form;
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.
Expand Down Expand Up @@ -121,14 +123,26 @@ 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()) {

/* @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)) {
Expand Down Expand Up @@ -166,7 +180,7 @@ public function editAction()
}

return new JsonModel(array(
'valid' => $form->isValid(),
'valid' => $isValid,
'content' => $content,
));
}
Expand Down Expand Up @@ -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;
}
}
24 changes: 8 additions & 16 deletions module/Organizations/src/Organizations/Entity/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,4 @@ public function init()
),
));
}

}
2 changes: 0 additions & 2 deletions module/Organizations/src/Organizations/Form/Employees.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,4 @@ class Employees extends SummaryForm
// return; // No buttons.
// }



}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
20 changes: 12 additions & 8 deletions module/Organizations/src/Organizations/Repository/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions module/Organizations/view/error/no-parent.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* YAWIK
*
* Template for the "Please create your organization first" page.
*
* @filesource
* @license MIT
* @copyright 2013 - 2015 Cross Solution <http://cross-solution.de>
*
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
*/
?>

<h1><?php echo $this->translate('Sorry, you may not create organizations.'); ?></h1>

<p><?php echo sprintf(
$this->translate('Before you are able to create organizations, you have to %screate your main organization%s first.'),
'<a href="' . $this->url('lang/my-organization', array(), true) . '">',
'</a>'
)
?></p>

0 comments on commit fecbe69

Please sign in to comment.