From 0cfc271588c5d426b1698b120db2aceef27fc56c Mon Sep 17 00:00:00 2001 From: jan-stanek Date: Thu, 9 Mar 2017 23:30:29 +0100 Subject: [PATCH 1/4] ProgramAttendeesService --- app/config/config.neon | 1 + app/services/ProgramAttendeesService.php | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 app/services/ProgramAttendeesService.php diff --git a/app/config/config.neon b/app/config/config.neon index 2e1f5b50a..0c3f27586 100644 --- a/app/config/config.neon +++ b/app/config/config.neon @@ -62,6 +62,7 @@ services: - App\Services\MailService - App\Services\PdfExportService(%wwwDir%/pdf) - App\Services\ExcelExportService + - App\Services\ProgramAttendeesService - App\Commands\ClearCacheCommand(%appDir%/..) - App\ApiModule\Services\ScheduleService - App\Model\ACL\PermissionRepository diff --git a/app/services/ProgramAttendeesService.php b/app/services/ProgramAttendeesService.php new file mode 100644 index 000000000..b9945d9a2 --- /dev/null +++ b/app/services/ProgramAttendeesService.php @@ -0,0 +1,9 @@ + Date: Fri, 10 Mar 2017 13:07:53 +0100 Subject: [PATCH 2/4] vylepseni odhlasovani a prihlasovani programu --- .../components/ProgramBlocksGridControl.php | 24 +++++++++- .../ProgramCategoriesGridControl.php | 31 ++++++++----- .../ProgramModule/forms/BlockForm.php | 29 +++++++++++- .../components/RolesGridControl.php | 19 ++++++-- .../components/UsersGridControl.php | 3 +- app/AdminModule/forms/EditUserSeminarForm.php | 4 +- app/WebModule/forms/ApplicationForm.php | 4 +- app/WebModule/forms/RolesForm.php | 4 +- app/config/config.neon | 13 ++++-- app/lang/admin.cs_CZ.neon | 4 +- app/model/ACL/RoleRepository.php | 7 +-- app/model/Program/BlockRepository.php | 7 +-- app/model/Program/Program.php | 18 ++++---- app/model/Program/ProgramRepository.php | 41 +++++++++++++++-- app/model/User/User.php | 44 ------------------- app/model/User/UserRepository.php | 3 ++ app/services/ProgramAttendeesService.php | 10 ++++- 17 files changed, 173 insertions(+), 92 deletions(-) diff --git a/app/AdminModule/ProgramModule/components/ProgramBlocksGridControl.php b/app/AdminModule/ProgramModule/components/ProgramBlocksGridControl.php index cec747e42..fbaa7b1da 100644 --- a/app/AdminModule/ProgramModule/components/ProgramBlocksGridControl.php +++ b/app/AdminModule/ProgramModule/components/ProgramBlocksGridControl.php @@ -9,6 +9,7 @@ use App\Model\Program\BlockRepository; use App\Model\Program\CategoryRepository; +use App\Model\Program\ProgramRepository; use App\Model\Settings\Settings; use App\Model\Settings\SettingsRepository; @@ -34,15 +35,19 @@ class ProgramBlocksGridControl extends Control /** @var CategoryRepository */ private $categoryRepository; + /** @var ProgramRepository */ + private $programRepository; + public function __construct(Translator $translator, BlockRepository $blockRepository, SettingsRepository $settingsRepository, UserRepository $userRepository, - CategoryRepository $categoryRepository) + CategoryRepository $categoryRepository, ProgramRepository $programRepository) { $this->translator = $translator; $this->blockRepository = $blockRepository; $this->settingsRepository = $settingsRepository; $this->userRepository = $userRepository; $this->categoryRepository = $categoryRepository; + $this->programRepository = $programRepository; } public function render() @@ -164,15 +169,30 @@ public function changeMandatory($id, $mandatory) { if (!$p->dbuser->isAllowedModifyBlock($block)) { $p->flashMessage('admin.program.blocks_change_mandatory_denied', 'danger'); } - elseif ($mandatory == 2 && $block->getMandatory() != 2 && $block->getProgramsCount() > 0) { + elseif ($mandatory == 2 && $block->getMandatory() != 2 && + ($block->getProgramsCount() > 1 || + ($block->getProgramsCount() == 1 && $this->programRepository->hasOverlappingProgram( + $block->getPrograms()->first(), + $block->getPrograms()->first()->getStart(), + $block->getPrograms()->first()->getEnd()) + ) + ) + ) { $p->flashMessage('admin.program.blocks_change_mandatory_auto_register_not_allowed', 'danger'); } else { + //odstraneni ucastniku, pokud se odstrani automaticke prihlasovani if ($block->getMandatory() == 2 && $mandatory != 2) { foreach ($block->getPrograms() as $program) { $program->removeAllAttendees(); } } + //pridani ucastniku, pokud je pridana automaticke prihlaseni + if ($mandatory == 2 && $block->getMandatory() != 2) { + foreach ($block->getPrograms() as $program) { + $program->setAttendees($this->userRepository->findProgramAllowed($program)); + } + } $block->setMandatory($mandatory); $this->blockRepository->save($block); diff --git a/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php b/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php index ac9ab92a5..4bf4e0ecc 100644 --- a/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php +++ b/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php @@ -7,6 +7,8 @@ use App\Model\ACL\RoleRepository; use App\Model\Program\Category; use App\Model\Program\CategoryRepository; +use App\Model\Program\ProgramRepository; +use App\Model\User\UserRepository; use Kdyby\Translation\Translator; use Nette\Application\UI\Control; use Nette\Application\UI\Form; @@ -15,26 +17,30 @@ class ProgramCategoriesGridControl extends Control { - /** - * @var Translator - */ + /** @var Translator */ private $translator; - /** - * @var CategoryRepository - */ + /** @var CategoryRepository */ private $categoryRepository; - /** - * @var RoleRepository - */ + /** @var RoleRepository */ private $roleRepository; - public function __construct(Translator $translator, CategoryRepository $categoryRepository, RoleRepository $roleRepository) + /** @var UserRepository */ + private $userRepository; + + /** @var ProgramRepository */ + private $programRepository; + + public function __construct(Translator $translator, CategoryRepository $categoryRepository, + RoleRepository $roleRepository, UserRepository $userRepository, + ProgramRepository $programRepository) { $this->translator = $translator; $this->categoryRepository = $categoryRepository; $this->roleRepository = $roleRepository; + $this->userRepository = $userRepository; + $this->programRepository = $programRepository; } public function render() @@ -122,6 +128,8 @@ public function edit($id, $values) $category->setName($values['name']); $category->setRegisterableRoles($this->roleRepository->findRolesByIds($values['registerableRoles'])); + $this->programRepository->updateUsersPrograms($this->userRepository->findAll()); + $this->categoryRepository->save($category); $p = $this->getPresenter(); @@ -135,6 +143,9 @@ public function handleDelete($id) $category = $this->categoryRepository->findById($id); $this->categoryRepository->remove($category); + $this->programRepository->updateUsersPrograms($this->userRepository->findAll()); + $this->programRepository->getEntityManager()->flush(); + $this->getPresenter()->flashMessage('admin.program.categories_deleted', 'success'); $this->redirect('this'); diff --git a/app/AdminModule/ProgramModule/forms/BlockForm.php b/app/AdminModule/ProgramModule/forms/BlockForm.php index 5e4ac744a..e9a8736e7 100644 --- a/app/AdminModule/ProgramModule/forms/BlockForm.php +++ b/app/AdminModule/ProgramModule/forms/BlockForm.php @@ -8,6 +8,7 @@ use App\Model\Program\Block; use App\Model\Program\BlockRepository; use App\Model\Program\CategoryRepository; +use App\Model\Program\ProgramRepository; use App\Model\Settings\Settings; use App\Model\Settings\SettingsRepository; use App\Model\User\User; @@ -39,15 +40,19 @@ class BlockForm extends Nette\Object /** @var SettingsRepository */ private $settingsRepository; + /** @var ProgramRepository */ + private $programRepository; + public function __construct(BaseForm $baseFormFactory, BlockRepository $blockRepository, UserRepository $userRepository, CategoryRepository $categoryRepository, - SettingsRepository $settingsRepository) + SettingsRepository $settingsRepository, ProgramRepository $programRepository) { $this->baseFormFactory = $baseFormFactory; $this->blockRepository = $blockRepository; $this->userRepository = $userRepository; $this->categoryRepository = $categoryRepository; $this->settingsRepository = $settingsRepository; + $this->programRepository = $programRepository; } public function create($id, $userId) @@ -149,13 +154,26 @@ public function processForm(Form $form, \stdClass $values) { } else if (!$this->user->isAllowedModifyBlock($this->block)) return; + //odstraneni ucastniku, pokud se odstrani automaticke prihlasovani if ($this->block->getMandatory() == 2 && (!array_key_exists('autoRegister', $values) || !$values['autoRegister'])) { foreach ($this->block->getPrograms() as $program) { $program->removeAllAttendees(); } } + //pridani ucastniku, pokud je pridana automaticke prihlaseni + if (array_key_exists('autoRegister', $values) && $values['autoRegister'] && $this->block->getMandatory() != 2) { + foreach ($this->block->getPrograms() as $program) { + $program->setAttendees($this->userRepository->findProgramAllowed($program)); + } + } $category = $values['category'] != '' ? $this->categoryRepository->findById($values['category']) : null; + + //aktualizace ucastniku pri zmene kategorie + if ($this->block->getCategory() != $category) { + $this->programRepository->updateUsersPrograms($this->userRepository->findAll()); + } + $lector = $values['lector'] != '' ? $this->userRepository->findById($values['lector']) : null; $capacity = $values['capacity'] !== '' ? $values['capacity'] : null; @@ -176,7 +194,14 @@ public function processForm(Form $form, \stdClass $values) { public function validateAutoRegister($field, $args) { if ($this->block) { - if ($this->block->getMandatory() != 2 && $this->block->getProgramsCount() > 0) + if ($this->block->getMandatory() != 2 && ($this->block->getProgramsCount() > 1 || + ($this->block->getProgramsCount() == 1 && $this->programRepository->hasOverlappingProgram( + $this->block->getPrograms()->first(), + $this->block->getPrograms()->first()->getStart(), + $this->block->getPrograms()->first()->getEnd()) + ) + ) + ) return false; } return true; diff --git a/app/AdminModule/components/RolesGridControl.php b/app/AdminModule/components/RolesGridControl.php index daeb6a712..42f83a0eb 100644 --- a/app/AdminModule/components/RolesGridControl.php +++ b/app/AdminModule/components/RolesGridControl.php @@ -5,8 +5,10 @@ use App\Model\ACL\Role; use App\Model\ACL\RoleRepository; +use App\Model\Program\ProgramRepository; use App\Model\User\UserRepository; +use App\Services\AttendeesService; use Kdyby\Translation\Translator; use Nette\Application\UI\Control; use Ublaboo\DataGrid\DataGrid; @@ -22,13 +24,18 @@ class RolesGridControl extends Control /** @var UserRepository */ private $userRepository; - public function __construct(Translator $translator, RoleRepository $roleRepository, UserRepository $userRepository) + /** @var ProgramRepository */ + private $programRepository; + + public function __construct(Translator $translator, RoleRepository $roleRepository, UserRepository $userRepository, + ProgramRepository $programRepository) { parent::__construct(); $this->translator = $translator; $this->roleRepository = $roleRepository; $this->userRepository = $userRepository; + $this->programRepository = $programRepository; } public function render() @@ -108,11 +115,17 @@ public function handleDelete($id) $usersInRole = $this->userRepository->findAllInRole($role); + $this->roleRepository->remove($role); + foreach ($usersInRole as $user) { - $user->removeRoleAndUpdatePrograms($role, $this->roleRepository->findBySystemName(Role::NONREGISTERED)); + if ($user->getRoles()->isEmpty()) + $user->addRole($this->roleRepository->findBySystemName(Role::NONREGISTERED)); } - $this->roleRepository->remove($role); + $allUsers = $this->userRepository->findAll(); + $this->programRepository->updateUsersPrograms($allUsers); + + $this->programRepository->getEntityManager()->flush(); $this->getPresenter()->flashMessage('admin.acl.roles_deleted', 'success'); diff --git a/app/AdminModule/components/UsersGridControl.php b/app/AdminModule/components/UsersGridControl.php index 96d78cab8..ec4cd1630 100644 --- a/app/AdminModule/components/UsersGridControl.php +++ b/app/AdminModule/components/UsersGridControl.php @@ -417,7 +417,8 @@ public function groupChangeRoles(array $ids, $value) { if (!$error) { foreach ($users as $user) { - $user->setRolesAndUpdatePrograms($selectedRoles, $this->programRepository->findUserAllowedAutoRegister($user)); + $user->setRoles($selectedRoles); + $this->programRepository->updateUserPrograms($user); $this->userRepository->save($user); } diff --git a/app/AdminModule/forms/EditUserSeminarForm.php b/app/AdminModule/forms/EditUserSeminarForm.php index 9cd58cac2..685f92d3e 100644 --- a/app/AdminModule/forms/EditUserSeminarForm.php +++ b/app/AdminModule/forms/EditUserSeminarForm.php @@ -137,8 +137,8 @@ public function create($id) public function processForm(Form $form, \stdClass $values) { if (!$form['cancel']->isSubmittedBy()) { - $this->user->setRolesAndUpdatePrograms($this->roleRepository->findRolesByIds($values['roles']), - $this->programRepository->findUserAllowedAutoRegister($this->user)); + $this->user->setRoles($this->roleRepository->findRolesByIds($values['roles'])); + $this->programRepository->updateUserPrograms($this->user); $this->user->setApproved($values['approved']); $this->user->setAttended($values['attended']); diff --git a/app/WebModule/forms/ApplicationForm.php b/app/WebModule/forms/ApplicationForm.php index 148d61115..7f0bfd4d4 100644 --- a/app/WebModule/forms/ApplicationForm.php +++ b/app/WebModule/forms/ApplicationForm.php @@ -162,8 +162,8 @@ public function processForm(Form $form, \stdClass $values) { } } - $this->user->setRolesAndUpdatePrograms($roles, $this->programRepository->findUserAllowedAutoRegister($this->user)); - + $this->user->setRoles($roles); + $this->programRepository->updateUserPrograms($this->user); foreach ($this->customInputRepository->findAll() as $customInput) { switch ($customInput->getType()) { diff --git a/app/WebModule/forms/RolesForm.php b/app/WebModule/forms/RolesForm.php index fb74a43c2..44d7dccca 100644 --- a/app/WebModule/forms/RolesForm.php +++ b/app/WebModule/forms/RolesForm.php @@ -95,7 +95,9 @@ public function processForm(Form $form, \stdClass $values) { } } - $this->user->setRolesAndUpdatePrograms($selectedRoles, $this->programRepository->findUserAllowedAutoRegister($this->user)); + $this->user->setRoles($selectedRoles); + $this->programRepository->updateUserPrograms($this->user); + $this->user->setApproved($approved); $this->userRepository->save($this->user); diff --git a/app/config/config.neon b/app/config/config.neon index 0c3f27586..420b76e69 100644 --- a/app/config/config.neon +++ b/app/config/config.neon @@ -62,12 +62,13 @@ services: - App\Services\MailService - App\Services\PdfExportService(%wwwDir%/pdf) - App\Services\ExcelExportService - - App\Services\ProgramAttendeesService - App\Commands\ClearCacheCommand(%appDir%/..) - App\ApiModule\Services\ScheduleService - App\Model\ACL\PermissionRepository - App\Model\ACL\ResourceRepository - - App\Model\ACL\RoleRepository + roleRepository: + class: App\Model\ACL\RoleRepository + inject: yes - App\Model\CMS\Content\ContentRepository - App\Model\CMS\Document\DocumentRepository - App\Model\CMS\Document\TagRepository @@ -75,9 +76,13 @@ services: - App\Model\CMS\NewsRepository - App\Model\CMS\PageRepository - App\Model\Mailing\MailRepository - - App\Model\Program\BlockRepository + blockRepository: + class: App\Model\Program\BlockRepository + inject: yes - App\Model\Program\CategoryRepository - - App\Model\Program\ProgramRepository + programRepository: + class: App\Model\Program\ProgramRepository + inject: yes - App\Model\Program\RoomRepository - App\Model\Settings\CustomInput\CustomInputRepository - App\Model\Settings\SettingsRepository diff --git a/app/lang/admin.cs_CZ.neon b/app/lang/admin.cs_CZ.neon index 442f49109..1b8135fa2 100644 --- a/app/lang/admin.cs_CZ.neon +++ b/app/lang/admin.cs_CZ.neon @@ -171,7 +171,7 @@ program: blocks_mandatory_form: "povinný" blocks_auto_register: "přihlašovat automaticky" blocks_auto_register_note: "Automaticky přihlašovaný program může být v harmonogramu zařazen maximálně jednou a nesmí být ve stejnou dobu jako jiný program." - blocks_auto_register_not_allowed: "Program nemůže být automaticky přihlašovaný, protože už je zařazen v harmonogramu." + blocks_auto_register_not_allowed: "Program nemůže být automaticky přihlašovaný, protože je v harmonogramu vícekrát nebo ve stejnou dobu jako jiný program." blocks_programs_count: "Počet zařazení" blocks_perex: "Stručný popis" blocks_perex_form: "Stručný popis (160 znaků)" @@ -181,7 +181,7 @@ program: blocks_saved: "Programový blok byl úspěšně uložen." blocks_changed_mandatory: "Povinnost programového bloku byla úspěšně upravena." blocks_change_mandatory_denied: "Nemáte oprávnění k úpravě tohoto programového bloku." - blocks_change_mandatory_auto_register_not_allowed: "Program nemůže být automaticky přihlašovaný, protože už je zařazen v harmonogramu." + blocks_change_mandatory_auto_register_not_allowed: "Program nemůže být automaticky přihlašovaný, protože je v harmonogramu vícekrát nebo ve stejnou dobu jako jiný program." blocks_delete_confirm: "Opravdu chcete programový blok odstranit?" blocks_deleted: "Programový blok byl úspěšně odstraněn." blocks_delete_not_allowed: "K odstranění tohoto bloku nemáte oprávnění." diff --git a/app/model/ACL/RoleRepository.php b/app/model/ACL/RoleRepository.php index 7d5bf8981..38c24cd30 100644 --- a/app/model/ACL/RoleRepository.php +++ b/app/model/ACL/RoleRepository.php @@ -15,9 +15,10 @@ class RoleRepository extends EntityRepository /** @var Translator */ private $translator; - public function __construct(EntityManager $em, Mapping\ClassMetadata $class, Translator $translator) - { - parent::__construct($em, $class); + /** + * @param Translator $translator + */ + public function injectTranslator(Translator $translator) { $this->translator = $translator; } diff --git a/app/model/Program/BlockRepository.php b/app/model/Program/BlockRepository.php index 556bf82ea..6a062b648 100644 --- a/app/model/Program/BlockRepository.php +++ b/app/model/Program/BlockRepository.php @@ -13,10 +13,11 @@ class BlockRepository extends EntityRepository /** @var UserRepository */ private $userRepository; - public function __construct(EntityManager $em, Mapping\ClassMetadata $class, UserRepository $userRepository) + /** + * @param UserRepository $userRepository + */ + public function injectUserRepository(UserRepository $userRepository) { - parent::__construct($em, $class); - $this->userRepository = $userRepository; } diff --git a/app/model/Program/Program.php b/app/model/Program/Program.php index f92e4c72a..379f3d1a7 100644 --- a/app/model/Program/Program.php +++ b/app/model/Program/Program.php @@ -81,14 +81,16 @@ public function getAttendees() return $this->attendees; } -// //nefunguje z inverse side, zatim neni potreba -// /** -// * @param ArrayCollection $attendees -// */ -// public function setAttendees($attendees) -// { -// $this->attendees = $attendees; -// } + /** + * @param ArrayCollection $attendees + */ + public function setAttendees($attendees) + { + $this->removeAllAttendees(); + foreach ($attendees as $attendee) { + $this->addAttendee($attendee); + } + } public function addAttendee($user) { if (!$this->attendees->contains($user)) { diff --git a/app/model/Program/ProgramRepository.php b/app/model/Program/ProgramRepository.php index 39ae17b09..c8331d555 100644 --- a/app/model/Program/ProgramRepository.php +++ b/app/model/Program/ProgramRepository.php @@ -5,6 +5,8 @@ use App\ApiModule\DTO\ProgramDetailDTO; use App\Model\User\User; use App\Model\User\UserRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping; use Kdyby\Doctrine\EntityManager; use Kdyby\Doctrine\EntityRepository; @@ -14,10 +16,11 @@ class ProgramRepository extends EntityRepository /** @var UserRepository */ private $userRepository; - public function __construct(EntityManager $em, Mapping\ClassMetadata $class, UserRepository $userRepository) + /** + * @param UserRepository $userRepository + */ + public function injectUserRepository(UserRepository $userRepository) { - parent::__construct($em, $class); - $this->userRepository = $userRepository; } @@ -190,4 +193,36 @@ public function hasOverlappingAutoRegisterProgram(Program $program, \DateTime $s return !empty($qb->getQuery()->getResult()); } + + /** + * @param User $user + */ + public function updateUserPrograms(User $user) { + $this->updateUsersPrograms([$user]); + } + + /** + * @param User[] $users + */ + public function updateUsersPrograms(array $users) { + foreach ($users as $user) { + $oldUsersPrograms = $user->getPrograms(); + $userAllowedPrograms = $this->findUserAllowed($user); + $userAllowedAutoRegisterPrograms = $this->findUserAllowedAutoRegister($user); + + $newUsersPrograms = new ArrayCollection(); + + foreach ($oldUsersPrograms as $oldUsersProgram) { + if (in_array($oldUsersProgram, $userAllowedPrograms)) + $newUsersPrograms->add($oldUsersProgram); + } + + foreach ($userAllowedAutoRegisterPrograms as $userAllowedAutoRegisterProgram) { + if (!$newUsersPrograms->contains($userAllowedAutoRegisterProgram)) + $newUsersPrograms->add($userAllowedAutoRegisterProgram); + } + + $user->setPrograms($newUsersPrograms); + } + } } \ No newline at end of file diff --git a/app/model/User/User.php b/app/model/User/User.php index 67eeaf630..e37cd426e 100644 --- a/app/model/User/User.php +++ b/app/model/User/User.php @@ -306,50 +306,6 @@ public function removeRole(Role $role) return $this->roles->removeElement($role); } - public function removeRoleAndUpdatePrograms($role, $nonregisteredRole) { - $newRoles = new ArrayCollection($this->roles->toArray()); - $newRoles->removeElement($role); - if ($newRoles->isEmpty()) - $newRoles->add($nonregisteredRole); - $this->setRolesAndUpdatePrograms($newRoles); - } - - public function setRolesAndUpdatePrograms($roles, $autoRegisterPrograms = []) { - $registerableCategoriesOld = array(); - $registerableCategoriesNew = array(); - - foreach ($this->getRegisterableCategories() as $category) { - $registerableCategoriesOld[] = $category; - } - - foreach ($this->getRegisterableCategories($roles) as $category) { - $registerableCategoriesNew[] = $category; - } - - $this->roles->clear(); - - //pridani role Neprihlaseny, odstraneni vsech programu - if (count($roles) == 1 && $roles[0]->getSystemName() == Role::NONREGISTERED) { - $this->addRole($roles[0]); - $this->programs->clear(); - } - //nastaveni roli, odebrani nepovolenych programu, pridani automaticky prihlasovanych - else { - foreach ($roles as $role) { - $this->roles->add($role); - } - - foreach ($registerableCategoriesOld as $oldCategory) { - if (!in_array($oldCategory, $registerableCategoriesNew)) - $this->removeProgramsInCategory($oldCategory); - } - - foreach ($autoRegisterPrograms as $autoRegisterProgram) { - $this->addProgram($autoRegisterProgram); - } - } - } - public function isInRole(Role $role) { return $this->roles->filter(function ($item) use ($role) { diff --git a/app/model/User/UserRepository.php b/app/model/User/UserRepository.php index a97e2cbb8..9794eb6d7 100644 --- a/app/model/User/UserRepository.php +++ b/app/model/User/UserRepository.php @@ -4,7 +4,10 @@ use App\Model\ACL\Permission; use App\Model\ACL\Role; +use App\Model\Program\ProgramRepository; use Doctrine\Common\Collections\Criteria; +use Doctrine\ORM\Mapping; +use Kdyby\Doctrine\EntityManager; use Kdyby\Doctrine\EntityRepository; class UserRepository extends EntityRepository diff --git a/app/services/ProgramAttendeesService.php b/app/services/ProgramAttendeesService.php index b9945d9a2..2eaae0dbd 100644 --- a/app/services/ProgramAttendeesService.php +++ b/app/services/ProgramAttendeesService.php @@ -1,6 +1,12 @@ Date: Sat, 11 Mar 2017 22:36:09 +0100 Subject: [PATCH 3/4] opravy chyb --- .../ProgramAttendeesGridControl.php | 2 +- .../ProgramCategoriesGridControl.php | 2 + .../ProgramModule/forms/BlockForm.php | 41 +++++++++++-------- .../components/RolesGridControl.php | 8 ++-- .../components/UsersGridControl.php | 4 +- app/AdminModule/forms/EditUserSeminarForm.php | 5 ++- app/WebModule/forms/ApplicationForm.php | 5 ++- app/WebModule/forms/RolesForm.php | 6 ++- app/model/Program/Program.php | 1 + app/model/Program/ProgramRepository.php | 40 ++++-------------- app/model/User/UserRepository.php | 16 ++++++-- 11 files changed, 67 insertions(+), 63 deletions(-) diff --git a/app/AdminModule/ProgramModule/components/ProgramAttendeesGridControl.php b/app/AdminModule/ProgramModule/components/ProgramAttendeesGridControl.php index 35094af2a..6c792f690 100644 --- a/app/AdminModule/ProgramModule/components/ProgramAttendeesGridControl.php +++ b/app/AdminModule/ProgramModule/components/ProgramAttendeesGridControl.php @@ -82,7 +82,7 @@ public function createComponentProgramAttendeesGrid($name) ->leftJoin('u.programs', 'p', 'WITH', 'p.id = :pid') ->innerJoin('u.roles', 'r') ->innerJoin('r.permissions', 'per') - ->where('u.approved = true') +// ->where('u.approved = true') ->andWhere('per.name = :permission') ->setParameter('pid', $programId) ->setParameter('permission', Permission::CHOOSE_PROGRAMS) diff --git a/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php b/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php index 4bf4e0ecc..ab595472a 100644 --- a/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php +++ b/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php @@ -128,6 +128,8 @@ public function edit($id, $values) $category->setName($values['name']); $category->setRegisterableRoles($this->roleRepository->findRolesByIds($values['registerableRoles'])); + $this->categoryRepository->save($category); + $this->programRepository->updateUsersPrograms($this->userRepository->findAll()); $this->categoryRepository->save($category); diff --git a/app/AdminModule/ProgramModule/forms/BlockForm.php b/app/AdminModule/ProgramModule/forms/BlockForm.php index e9a8736e7..3e578ca40 100644 --- a/app/AdminModule/ProgramModule/forms/BlockForm.php +++ b/app/AdminModule/ProgramModule/forms/BlockForm.php @@ -154,26 +154,10 @@ public function processForm(Form $form, \stdClass $values) { } else if (!$this->user->isAllowedModifyBlock($this->block)) return; - //odstraneni ucastniku, pokud se odstrani automaticke prihlasovani - if ($this->block->getMandatory() == 2 && (!array_key_exists('autoRegister', $values) || !$values['autoRegister'])) { - foreach ($this->block->getPrograms() as $program) { - $program->removeAllAttendees(); - } - } - //pridani ucastniku, pokud je pridana automaticke prihlaseni - if (array_key_exists('autoRegister', $values) && $values['autoRegister'] && $this->block->getMandatory() != 2) { - foreach ($this->block->getPrograms() as $program) { - $program->setAttendees($this->userRepository->findProgramAllowed($program)); - } - } + $oldMandatory = $this->block->getMandatory(); + $oldCategory = $this->block->getCategory(); $category = $values['category'] != '' ? $this->categoryRepository->findById($values['category']) : null; - - //aktualizace ucastniku pri zmene kategorie - if ($this->block->getCategory() != $category) { - $this->programRepository->updateUsersPrograms($this->userRepository->findAll()); - } - $lector = $values['lector'] != '' ? $this->userRepository->findById($values['lector']) : null; $capacity = $values['capacity'] !== '' ? $values['capacity'] : null; @@ -188,6 +172,27 @@ public function processForm(Form $form, \stdClass $values) { $this->block->setTools($values['tools']); $this->blockRepository->save($this->block); + + //odstraneni ucastniku, pokud se odstrani automaticke prihlasovani + if ($oldMandatory == 2 && $this->block->getMandatory() != 2) { + foreach ($this->block->getPrograms() as $program) { + $program->removeAllAttendees(); + } + } + + //pridani ucastniku, pokud je pridano automaticke prihlaseni + if ($oldMandatory != 2 && $this->block->getMandatory() == 2) { + foreach ($this->block->getPrograms() as $program) { + $program->setAttendees($this->userRepository->findProgramAllowed($program)); + } + } + + //aktualizace ucastniku pri zmene kategorie + if ($oldMandatory == $this->block->getMandatory() && $this->block->getCategory() != $oldCategory) { + $this->programRepository->updateUsersPrograms($this->userRepository->findAll()); + } + + $this->blockRepository->save($this->block); } } diff --git a/app/AdminModule/components/RolesGridControl.php b/app/AdminModule/components/RolesGridControl.php index 42f83a0eb..f1c6f1603 100644 --- a/app/AdminModule/components/RolesGridControl.php +++ b/app/AdminModule/components/RolesGridControl.php @@ -118,13 +118,13 @@ public function handleDelete($id) $this->roleRepository->remove($role); foreach ($usersInRole as $user) { - if ($user->getRoles()->isEmpty()) + if ($user->getRoles()->isEmpty()) { $user->addRole($this->roleRepository->findBySystemName(Role::NONREGISTERED)); + $this->userRepository->save($user); + } } - $allUsers = $this->userRepository->findAll(); - $this->programRepository->updateUsersPrograms($allUsers); - + $this->programRepository->updateUsersPrograms($this->userRepository->findAll()); $this->programRepository->getEntityManager()->flush(); $this->getPresenter()->flashMessage('admin.acl.roles_deleted', 'success'); diff --git a/app/AdminModule/components/UsersGridControl.php b/app/AdminModule/components/UsersGridControl.php index ec4cd1630..5f0cecc0d 100644 --- a/app/AdminModule/components/UsersGridControl.php +++ b/app/AdminModule/components/UsersGridControl.php @@ -418,10 +418,12 @@ public function groupChangeRoles(array $ids, $value) { if (!$error) { foreach ($users as $user) { $user->setRoles($selectedRoles); - $this->programRepository->updateUserPrograms($user); $this->userRepository->save($user); } + $this->programRepository->updateUsersPrograms($users->toArray()); + $this->userRepository->getEntityManager()->flush(); + $p->flashMessage('admin.users.users_group_action_changed_roles', 'success'); } diff --git a/app/AdminModule/forms/EditUserSeminarForm.php b/app/AdminModule/forms/EditUserSeminarForm.php index 685f92d3e..3fc3da1ef 100644 --- a/app/AdminModule/forms/EditUserSeminarForm.php +++ b/app/AdminModule/forms/EditUserSeminarForm.php @@ -138,7 +138,6 @@ public function create($id) public function processForm(Form $form, \stdClass $values) { if (!$form['cancel']->isSubmittedBy()) { $this->user->setRoles($this->roleRepository->findRolesByIds($values['roles'])); - $this->programRepository->updateUserPrograms($this->user); $this->user->setApproved($values['approved']); $this->user->setAttended($values['attended']); @@ -173,6 +172,10 @@ public function processForm(Form $form, \stdClass $values) { $this->user->setNote($values['privateNote']); $this->userRepository->save($this->user); + + $this->programRepository->updateUserPrograms($this->user); + + $this->userRepository->save($this->user); } } diff --git a/app/WebModule/forms/ApplicationForm.php b/app/WebModule/forms/ApplicationForm.php index 7f0bfd4d4..78e366a06 100644 --- a/app/WebModule/forms/ApplicationForm.php +++ b/app/WebModule/forms/ApplicationForm.php @@ -163,7 +163,6 @@ public function processForm(Form $form, \stdClass $values) { } $this->user->setRoles($roles); - $this->programRepository->updateUserPrograms($this->user); foreach ($this->customInputRepository->findAll() as $customInput) { switch ($customInput->getType()) { @@ -187,6 +186,10 @@ public function processForm(Form $form, \stdClass $values) { $this->user->setDeparture($values['departure']); + $this->userRepository->save($this->user); + + $this->programRepository->updateUserPrograms($this->user); + $this->userRepository->save($this->user); try { diff --git a/app/WebModule/forms/RolesForm.php b/app/WebModule/forms/RolesForm.php index 44d7dccca..682a23454 100644 --- a/app/WebModule/forms/RolesForm.php +++ b/app/WebModule/forms/RolesForm.php @@ -96,11 +96,13 @@ public function processForm(Form $form, \stdClass $values) { } $this->user->setRoles($selectedRoles); - $this->programRepository->updateUserPrograms($this->user); - $this->user->setApproved($approved); $this->userRepository->save($this->user); + + $this->programRepository->updateUserPrograms($this->user); + + $this->userRepository->save($this->user); } elseif ($form['cancelRegistration']->isSubmittedBy()) { $this->userRepository->remove($this->user); diff --git a/app/model/Program/Program.php b/app/model/Program/Program.php index 379f3d1a7..316233eae 100644 --- a/app/model/Program/Program.php +++ b/app/model/Program/Program.php @@ -5,6 +5,7 @@ use App\ApiModule\DTO\ProgramDetailDTO; use App\Model\User\User; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping as ORM; use Kdyby\Doctrine\Entities\Attributes\Identifier; diff --git a/app/model/Program/ProgramRepository.php b/app/model/Program/ProgramRepository.php index c8331d555..ae63f7d6b 100644 --- a/app/model/Program/ProgramRepository.php +++ b/app/model/Program/ProgramRepository.php @@ -3,6 +3,8 @@ namespace App\Model\Program; use App\ApiModule\DTO\ProgramDetailDTO; +use App\Model\ACL\Permission; +use App\Model\ACL\Resource; use App\Model\User\User; use App\Model\User\UserRepository; use Doctrine\Common\Collections\ArrayCollection; @@ -67,6 +69,9 @@ public function findProgramsIds($programs) */ public function findUserAllowed($user) { + if (!$user->isAllowed(Resource::PROGRAM, Permission::CHOOSE_PROGRAMS)) + return []; + $registerableCategoriesIds = $this->userRepository->findRegisterableCategoriesIdsByUser($user); return $this->createQueryBuilder('p') @@ -79,28 +84,6 @@ public function findUserAllowed($user) ->getResult(); } - /** - * @param User $user - * @return array - */ - public function findUserAllowedAutoRegister($user) - { - $registerableCategoriesIds = $this->userRepository->findRegisterableCategoriesIdsByUser($user); - - return $this->createQueryBuilder('p') - ->select('p') - ->join('p.block', 'b') - ->leftJoin('b.category', 'c') - ->where($this->createQueryBuilder()->expr()->orX( - 'c.id IN (:ids)', - 'b.category IS NULL' - )) - ->andWhere('b.mandatory = 2') - ->setParameter('ids', $registerableCategoriesIds) - ->getQuery() - ->getResult(); - } - /** * @param Program $program * @return int[] @@ -208,20 +191,15 @@ public function updateUsersPrograms(array $users) { foreach ($users as $user) { $oldUsersPrograms = $user->getPrograms(); $userAllowedPrograms = $this->findUserAllowed($user); - $userAllowedAutoRegisterPrograms = $this->findUserAllowedAutoRegister($user); $newUsersPrograms = new ArrayCollection(); - foreach ($oldUsersPrograms as $oldUsersProgram) { - if (in_array($oldUsersProgram, $userAllowedPrograms)) - $newUsersPrograms->add($oldUsersProgram); - } - - foreach ($userAllowedAutoRegisterPrograms as $userAllowedAutoRegisterProgram) { - if (!$newUsersPrograms->contains($userAllowedAutoRegisterProgram)) - $newUsersPrograms->add($userAllowedAutoRegisterProgram); + foreach ($userAllowedPrograms as $userAllowedProgram) { + if ($userAllowedProgram->getBlock()->getMandatory() == 2 || $oldUsersPrograms->contains($userAllowedProgram)) + $newUsersPrograms->add($userAllowedProgram); } + $oldUsersPrograms->clear(); $user->setPrograms($newUsersPrograms); } } diff --git a/app/model/User/UserRepository.php b/app/model/User/UserRepository.php index 9794eb6d7..fdc368f08 100644 --- a/app/model/User/UserRepository.php +++ b/app/model/User/UserRepository.php @@ -4,6 +4,7 @@ use App\Model\ACL\Permission; use App\Model\ACL\Role; +use App\Model\Program\Program; use App\Model\Program\ProgramRepository; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping; @@ -126,15 +127,22 @@ public function findAllApprovedInRoles($rolesIds) { * @param $program * @return array */ - public function findProgramAllowed($program) { - return $this->createQueryBuilder('u') + public function findProgramAllowed(Program $program) { + $qb = $this->createQueryBuilder('u') ->leftJoin('u.programs', 'p', 'WITH', 'p.id = :pid') ->innerJoin('u.roles', 'r') ->innerJoin('r.permissions', 'per') ->where('per.name = :permission') ->setParameter('pid', $program->getId()) - ->setParameter('permission', Permission::CHOOSE_PROGRAMS) - ->getQuery() + ->setParameter('permission', Permission::CHOOSE_PROGRAMS); + + if ($program->getBlock()->getCategory()) { + $qb = $qb->innerJoin('r.registerableCategories', 'c') + ->andWhere('c.id = :cid') + ->setParameter('cid', $program->getBlock()->getCategory()->getId()); + } + + return $qb->getQuery() ->getResult(); } From 865d779e23c1987d7d8c3afa2bbb0137a08e1153 Mon Sep 17 00:00:00 2001 From: jan-stanek Date: Sat, 11 Mar 2017 22:36:09 +0100 Subject: [PATCH 4/4] opravy chyb --- .../ProgramAttendeesGridControl.php | 2 +- .../ProgramCategoriesGridControl.php | 2 + .../ProgramModule/forms/BlockForm.php | 41 +++++++++++-------- .../components/RolesGridControl.php | 8 ++-- .../components/UsersGridControl.php | 4 +- app/AdminModule/forms/EditUserSeminarForm.php | 5 ++- app/WebModule/forms/ApplicationForm.php | 5 ++- app/WebModule/forms/RolesForm.php | 6 ++- app/model/Program/Program.php | 1 + app/model/Program/ProgramRepository.php | 40 ++++-------------- app/model/User/UserRepository.php | 16 ++++++-- app/services/ProgramAttendeesService.php | 15 ------- 12 files changed, 67 insertions(+), 78 deletions(-) delete mode 100644 app/services/ProgramAttendeesService.php diff --git a/app/AdminModule/ProgramModule/components/ProgramAttendeesGridControl.php b/app/AdminModule/ProgramModule/components/ProgramAttendeesGridControl.php index 35094af2a..6c792f690 100644 --- a/app/AdminModule/ProgramModule/components/ProgramAttendeesGridControl.php +++ b/app/AdminModule/ProgramModule/components/ProgramAttendeesGridControl.php @@ -82,7 +82,7 @@ public function createComponentProgramAttendeesGrid($name) ->leftJoin('u.programs', 'p', 'WITH', 'p.id = :pid') ->innerJoin('u.roles', 'r') ->innerJoin('r.permissions', 'per') - ->where('u.approved = true') +// ->where('u.approved = true') ->andWhere('per.name = :permission') ->setParameter('pid', $programId) ->setParameter('permission', Permission::CHOOSE_PROGRAMS) diff --git a/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php b/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php index 4bf4e0ecc..ab595472a 100644 --- a/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php +++ b/app/AdminModule/ProgramModule/components/ProgramCategoriesGridControl.php @@ -128,6 +128,8 @@ public function edit($id, $values) $category->setName($values['name']); $category->setRegisterableRoles($this->roleRepository->findRolesByIds($values['registerableRoles'])); + $this->categoryRepository->save($category); + $this->programRepository->updateUsersPrograms($this->userRepository->findAll()); $this->categoryRepository->save($category); diff --git a/app/AdminModule/ProgramModule/forms/BlockForm.php b/app/AdminModule/ProgramModule/forms/BlockForm.php index e9a8736e7..3e578ca40 100644 --- a/app/AdminModule/ProgramModule/forms/BlockForm.php +++ b/app/AdminModule/ProgramModule/forms/BlockForm.php @@ -154,26 +154,10 @@ public function processForm(Form $form, \stdClass $values) { } else if (!$this->user->isAllowedModifyBlock($this->block)) return; - //odstraneni ucastniku, pokud se odstrani automaticke prihlasovani - if ($this->block->getMandatory() == 2 && (!array_key_exists('autoRegister', $values) || !$values['autoRegister'])) { - foreach ($this->block->getPrograms() as $program) { - $program->removeAllAttendees(); - } - } - //pridani ucastniku, pokud je pridana automaticke prihlaseni - if (array_key_exists('autoRegister', $values) && $values['autoRegister'] && $this->block->getMandatory() != 2) { - foreach ($this->block->getPrograms() as $program) { - $program->setAttendees($this->userRepository->findProgramAllowed($program)); - } - } + $oldMandatory = $this->block->getMandatory(); + $oldCategory = $this->block->getCategory(); $category = $values['category'] != '' ? $this->categoryRepository->findById($values['category']) : null; - - //aktualizace ucastniku pri zmene kategorie - if ($this->block->getCategory() != $category) { - $this->programRepository->updateUsersPrograms($this->userRepository->findAll()); - } - $lector = $values['lector'] != '' ? $this->userRepository->findById($values['lector']) : null; $capacity = $values['capacity'] !== '' ? $values['capacity'] : null; @@ -188,6 +172,27 @@ public function processForm(Form $form, \stdClass $values) { $this->block->setTools($values['tools']); $this->blockRepository->save($this->block); + + //odstraneni ucastniku, pokud se odstrani automaticke prihlasovani + if ($oldMandatory == 2 && $this->block->getMandatory() != 2) { + foreach ($this->block->getPrograms() as $program) { + $program->removeAllAttendees(); + } + } + + //pridani ucastniku, pokud je pridano automaticke prihlaseni + if ($oldMandatory != 2 && $this->block->getMandatory() == 2) { + foreach ($this->block->getPrograms() as $program) { + $program->setAttendees($this->userRepository->findProgramAllowed($program)); + } + } + + //aktualizace ucastniku pri zmene kategorie + if ($oldMandatory == $this->block->getMandatory() && $this->block->getCategory() != $oldCategory) { + $this->programRepository->updateUsersPrograms($this->userRepository->findAll()); + } + + $this->blockRepository->save($this->block); } } diff --git a/app/AdminModule/components/RolesGridControl.php b/app/AdminModule/components/RolesGridControl.php index 42f83a0eb..f1c6f1603 100644 --- a/app/AdminModule/components/RolesGridControl.php +++ b/app/AdminModule/components/RolesGridControl.php @@ -118,13 +118,13 @@ public function handleDelete($id) $this->roleRepository->remove($role); foreach ($usersInRole as $user) { - if ($user->getRoles()->isEmpty()) + if ($user->getRoles()->isEmpty()) { $user->addRole($this->roleRepository->findBySystemName(Role::NONREGISTERED)); + $this->userRepository->save($user); + } } - $allUsers = $this->userRepository->findAll(); - $this->programRepository->updateUsersPrograms($allUsers); - + $this->programRepository->updateUsersPrograms($this->userRepository->findAll()); $this->programRepository->getEntityManager()->flush(); $this->getPresenter()->flashMessage('admin.acl.roles_deleted', 'success'); diff --git a/app/AdminModule/components/UsersGridControl.php b/app/AdminModule/components/UsersGridControl.php index ec4cd1630..5f0cecc0d 100644 --- a/app/AdminModule/components/UsersGridControl.php +++ b/app/AdminModule/components/UsersGridControl.php @@ -418,10 +418,12 @@ public function groupChangeRoles(array $ids, $value) { if (!$error) { foreach ($users as $user) { $user->setRoles($selectedRoles); - $this->programRepository->updateUserPrograms($user); $this->userRepository->save($user); } + $this->programRepository->updateUsersPrograms($users->toArray()); + $this->userRepository->getEntityManager()->flush(); + $p->flashMessage('admin.users.users_group_action_changed_roles', 'success'); } diff --git a/app/AdminModule/forms/EditUserSeminarForm.php b/app/AdminModule/forms/EditUserSeminarForm.php index 685f92d3e..3fc3da1ef 100644 --- a/app/AdminModule/forms/EditUserSeminarForm.php +++ b/app/AdminModule/forms/EditUserSeminarForm.php @@ -138,7 +138,6 @@ public function create($id) public function processForm(Form $form, \stdClass $values) { if (!$form['cancel']->isSubmittedBy()) { $this->user->setRoles($this->roleRepository->findRolesByIds($values['roles'])); - $this->programRepository->updateUserPrograms($this->user); $this->user->setApproved($values['approved']); $this->user->setAttended($values['attended']); @@ -173,6 +172,10 @@ public function processForm(Form $form, \stdClass $values) { $this->user->setNote($values['privateNote']); $this->userRepository->save($this->user); + + $this->programRepository->updateUserPrograms($this->user); + + $this->userRepository->save($this->user); } } diff --git a/app/WebModule/forms/ApplicationForm.php b/app/WebModule/forms/ApplicationForm.php index 7f0bfd4d4..78e366a06 100644 --- a/app/WebModule/forms/ApplicationForm.php +++ b/app/WebModule/forms/ApplicationForm.php @@ -163,7 +163,6 @@ public function processForm(Form $form, \stdClass $values) { } $this->user->setRoles($roles); - $this->programRepository->updateUserPrograms($this->user); foreach ($this->customInputRepository->findAll() as $customInput) { switch ($customInput->getType()) { @@ -187,6 +186,10 @@ public function processForm(Form $form, \stdClass $values) { $this->user->setDeparture($values['departure']); + $this->userRepository->save($this->user); + + $this->programRepository->updateUserPrograms($this->user); + $this->userRepository->save($this->user); try { diff --git a/app/WebModule/forms/RolesForm.php b/app/WebModule/forms/RolesForm.php index 44d7dccca..682a23454 100644 --- a/app/WebModule/forms/RolesForm.php +++ b/app/WebModule/forms/RolesForm.php @@ -96,11 +96,13 @@ public function processForm(Form $form, \stdClass $values) { } $this->user->setRoles($selectedRoles); - $this->programRepository->updateUserPrograms($this->user); - $this->user->setApproved($approved); $this->userRepository->save($this->user); + + $this->programRepository->updateUserPrograms($this->user); + + $this->userRepository->save($this->user); } elseif ($form['cancelRegistration']->isSubmittedBy()) { $this->userRepository->remove($this->user); diff --git a/app/model/Program/Program.php b/app/model/Program/Program.php index 379f3d1a7..316233eae 100644 --- a/app/model/Program/Program.php +++ b/app/model/Program/Program.php @@ -5,6 +5,7 @@ use App\ApiModule\DTO\ProgramDetailDTO; use App\Model\User\User; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping as ORM; use Kdyby\Doctrine\Entities\Attributes\Identifier; diff --git a/app/model/Program/ProgramRepository.php b/app/model/Program/ProgramRepository.php index c8331d555..ae63f7d6b 100644 --- a/app/model/Program/ProgramRepository.php +++ b/app/model/Program/ProgramRepository.php @@ -3,6 +3,8 @@ namespace App\Model\Program; use App\ApiModule\DTO\ProgramDetailDTO; +use App\Model\ACL\Permission; +use App\Model\ACL\Resource; use App\Model\User\User; use App\Model\User\UserRepository; use Doctrine\Common\Collections\ArrayCollection; @@ -67,6 +69,9 @@ public function findProgramsIds($programs) */ public function findUserAllowed($user) { + if (!$user->isAllowed(Resource::PROGRAM, Permission::CHOOSE_PROGRAMS)) + return []; + $registerableCategoriesIds = $this->userRepository->findRegisterableCategoriesIdsByUser($user); return $this->createQueryBuilder('p') @@ -79,28 +84,6 @@ public function findUserAllowed($user) ->getResult(); } - /** - * @param User $user - * @return array - */ - public function findUserAllowedAutoRegister($user) - { - $registerableCategoriesIds = $this->userRepository->findRegisterableCategoriesIdsByUser($user); - - return $this->createQueryBuilder('p') - ->select('p') - ->join('p.block', 'b') - ->leftJoin('b.category', 'c') - ->where($this->createQueryBuilder()->expr()->orX( - 'c.id IN (:ids)', - 'b.category IS NULL' - )) - ->andWhere('b.mandatory = 2') - ->setParameter('ids', $registerableCategoriesIds) - ->getQuery() - ->getResult(); - } - /** * @param Program $program * @return int[] @@ -208,20 +191,15 @@ public function updateUsersPrograms(array $users) { foreach ($users as $user) { $oldUsersPrograms = $user->getPrograms(); $userAllowedPrograms = $this->findUserAllowed($user); - $userAllowedAutoRegisterPrograms = $this->findUserAllowedAutoRegister($user); $newUsersPrograms = new ArrayCollection(); - foreach ($oldUsersPrograms as $oldUsersProgram) { - if (in_array($oldUsersProgram, $userAllowedPrograms)) - $newUsersPrograms->add($oldUsersProgram); - } - - foreach ($userAllowedAutoRegisterPrograms as $userAllowedAutoRegisterProgram) { - if (!$newUsersPrograms->contains($userAllowedAutoRegisterProgram)) - $newUsersPrograms->add($userAllowedAutoRegisterProgram); + foreach ($userAllowedPrograms as $userAllowedProgram) { + if ($userAllowedProgram->getBlock()->getMandatory() == 2 || $oldUsersPrograms->contains($userAllowedProgram)) + $newUsersPrograms->add($userAllowedProgram); } + $oldUsersPrograms->clear(); $user->setPrograms($newUsersPrograms); } } diff --git a/app/model/User/UserRepository.php b/app/model/User/UserRepository.php index 9794eb6d7..fdc368f08 100644 --- a/app/model/User/UserRepository.php +++ b/app/model/User/UserRepository.php @@ -4,6 +4,7 @@ use App\Model\ACL\Permission; use App\Model\ACL\Role; +use App\Model\Program\Program; use App\Model\Program\ProgramRepository; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping; @@ -126,15 +127,22 @@ public function findAllApprovedInRoles($rolesIds) { * @param $program * @return array */ - public function findProgramAllowed($program) { - return $this->createQueryBuilder('u') + public function findProgramAllowed(Program $program) { + $qb = $this->createQueryBuilder('u') ->leftJoin('u.programs', 'p', 'WITH', 'p.id = :pid') ->innerJoin('u.roles', 'r') ->innerJoin('r.permissions', 'per') ->where('per.name = :permission') ->setParameter('pid', $program->getId()) - ->setParameter('permission', Permission::CHOOSE_PROGRAMS) - ->getQuery() + ->setParameter('permission', Permission::CHOOSE_PROGRAMS); + + if ($program->getBlock()->getCategory()) { + $qb = $qb->innerJoin('r.registerableCategories', 'c') + ->andWhere('c.id = :cid') + ->setParameter('cid', $program->getBlock()->getCategory()->getId()); + } + + return $qb->getQuery() ->getResult(); } diff --git a/app/services/ProgramAttendeesService.php b/app/services/ProgramAttendeesService.php deleted file mode 100644 index 2eaae0dbd..000000000 --- a/app/services/ProgramAttendeesService.php +++ /dev/null @@ -1,15 +0,0 @@ -