Skip to content

Commit

Permalink
Merge pull request #702 from jan-stanek/oprava-zdvojenych-prihlasek
Browse files Browse the repository at this point in the history
oprava zdvojenych prihlasek, refactoring RoleRepository
  • Loading branch information
jan-stanek authored Feb 22, 2020
2 parents a0be952 + 4b6bbbd commit 2322ba3
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 178 deletions.
133 changes: 33 additions & 100 deletions app/Model/Acl/RoleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ public function findById(?int $id) : ?Role
return $this->findOneBy(['id' => $id]);
}

/**
* Vrací roli podle názvu.
*/
public function findByName(string $name) : ?Role
{
return $this->findOneBy(['name' => $name]);
}

/**
* Vrací systémovou roli podle systémového názvu.
*/
Expand Down Expand Up @@ -79,55 +71,6 @@ public function findOthersNames(int $id) : array
return array_map('current', $names);
}

/**
* Vrací registrovatelné role.
*
* @return Role[]
*/
public function findAllRegisterable() : array
{
return $this->findBy(['registerable' => true]);
}

/**
* Vrací role s omezenou kapacitou.
*
* @return Collection|Role[]
*/
public function findAllWithLimitedCapacity() : Collection
{
$criteria = Criteria::create()
->where(Criteria::expr()->neq('capacity', null));

return $this->matching($criteria);
}

/**
* Vrací role, u kterých se eviduje příjezd a odjezd.
*
* @return Collection|Role[]
*/
public function findAllWithArrivalDeparture() : Collection
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq('displayArrivalDeparture', true));

return $this->matching($criteria);
}

/**
* Vrací role, u kterých je cena počítána podle podakcí.
*
* @return Collection|Role[]
*/
public function findAllWithSubevents() : Collection
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq('fee', null));

return $this->matching($criteria);
}

/**
* Vrací role podle id.
*
Expand Down Expand Up @@ -178,61 +121,51 @@ public function findRolesIds(Collection $roles) : array
}

/**
* Vraci role, ktere jsou tuto chvíli registrovatelné, seřazené podle názvu.
* Vrací role splňující podmínku seřazené podle názvu.
*
* @return Collection|Role[]
*/
public function findAllRegisterableNowOrderedByName() : Collection
public function findFilteredRoles(bool $registerableNowOnly, bool $subeventsRoleOnly, bool $arrivalDepartureOnly, bool $includeUsers, ?User $user = null) : Collection
{
$result = $this->createQueryBuilder('r')
$qb = $this->createQueryBuilder('r');

$query = $qb
->select('r')
->where($this->createQueryBuilder('r')->expr()->andX(
$this->createQueryBuilder('r')->expr()->eq('r.registerable', true),
$this->createQueryBuilder('r')->expr()->orX(
$this->createQueryBuilder('r')->expr()->lte('r.registerableFrom', 'CURRENT_TIMESTAMP()'),
$this->createQueryBuilder('r')->expr()->isNull('r.registerableFrom')
),
$this->createQueryBuilder('r')->expr()->orX(
$this->createQueryBuilder('r')->expr()->gte('r.registerableTo', 'CURRENT_TIMESTAMP()'),
$this->createQueryBuilder('r')->expr()->isNull('r.registerableTo')
)
))
->where('1 = 1');

if ($registerableNowOnly) {
$query = $query
->andWhere($qb->expr()->eq('r.registerable', true))
->andWhere($qb->expr()->orX(
$qb->expr()->lte('r.registerableFrom', 'CURRENT_TIMESTAMP()'),
$qb->expr()->isNull('r.registerableFrom')
))
->andWhere($qb->expr()->orX(
$qb->expr()->gte('r.registerableTo', 'CURRENT_TIMESTAMP()'),
$qb->expr()->isNull('r.registerableTo')
));
}

if ($subeventsRoleOnly) {
$query = $query->andWhere('r.fee IS NULL');
}

if ($arrivalDepartureOnly) {
$query = $query->andWhere('r.displayArrivalDeparture = TRUE');
}

if ($includeUsers) {
$query = $query->orWhere('r in (:users_roles)')->setParameter('users_roles', $user->getRoles());
}

$result = $query
->orderBy('r.name')
->getQuery()
->getResult();

return new ArrayCollection($result);
}

/**
* Vrací role, které jsou v tuto chvíli registrovatelné nebo je uživatel má, seřazené podle názvu.
*
* @return Role[]
*/
public function findAllRegisterableNowOrUsersOrderedByName(User $user) : array
{
return $this->createQueryBuilder('r')
->select('r')
->leftJoin('r.users', 'u')
->where($this->createQueryBuilder('r')->expr()->orX(
$this->createQueryBuilder('r')->expr()->andX(
$this->createQueryBuilder('r')->expr()->eq('r.registerable', true),
$this->createQueryBuilder('r')->expr()->orX(
$this->createQueryBuilder('r')->expr()->lte('r.registerableFrom', 'CURRENT_TIMESTAMP()'),
$this->createQueryBuilder('r')->expr()->isNull('r.registerableFrom')
),
$this->createQueryBuilder('r')->expr()->orX(
$this->createQueryBuilder('r')->expr()->gte('r.registerableTo', 'CURRENT_TIMESTAMP()'),
$this->createQueryBuilder('r')->expr()->isNull('r.registerableTo')
)
),
$this->createQueryBuilder('r')->expr()->eq('u.id', $user->getId())
))
->orderBy('r.name')
->getQuery()
->getResult();
}

public function incrementOccupancy(Role $role) : void
{
$this->_em->createQuery('UPDATE App\Model\Acl\Role r SET r.occupancy = r.occupancy + 1 WHERE r.id = :rid')
Expand Down
33 changes: 1 addition & 32 deletions app/Model/Structure/SubeventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,43 +54,12 @@ public function findAllNames() : array
return array_map('current', $names);
}

/**
* Vrací podakce, seřazené podle názvu.
*
* @return Collection|Subevent[]
*/
public function findAllOrderedByName() : Collection
{
$result = $this->createQueryBuilder('s')
->orderBy('s.name')
->getQuery()
->getResult();

return new ArrayCollection($result);
}

/**
* Vrací vytvořené podakce, seřazené podle názvu.
*
* @return Collection|Subevent[]
*/
public function findExplicitOrderedByName() : Collection
{
$result = $this->createQueryBuilder('s')
->where('s.implicit = FALSE')
->orderBy('s.name')
->getQuery()
->getResult();

return new ArrayCollection($result);
}

/**
* Vrací podakce splňující podmínku seřazené podle názvu.
*
* @return Collection|Subevent[]
*/
public function findSubeventsOrderedByName(bool $explicitOnly, bool $registerableNowOnly, bool $notRegisteredOnly, bool $includeUsers, ?User $user = null) : Collection
public function findFilteredSubevents(bool $explicitOnly, bool $registerableNowOnly, bool $notRegisteredOnly, bool $includeUsers, ?User $user = null) : Collection
{
$qb = $this->createQueryBuilder('s');

Expand Down
32 changes: 3 additions & 29 deletions app/Services/AclService.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,39 +245,13 @@ public function getRolesWithoutRolesOptionsWithApprovedUsersCount(array $without
}

/**
* Vrací seznam rolí s obsazenostmi jako možnosti pro select.
* Vrací seznam rolí splňujících podmínku, s informací o obsazenosti, jako možnosti pro select.
*
* @return string[]
*/
public function getRegisterableNowOptionsWithCapacity() : array
public function getRolesOptionsWithCapacity(bool $registerableNowOnly, bool $includeUsers, ?User $user = null) : array
{
$roles = $this->roleRepository->findAllRegisterableNowOrderedByName();

$options = [];
foreach ($roles as $role) {
if ($role->hasLimitedCapacity()) {
$options[$role->getId()] = $this->translator->translate('web.common.role_option', null, [
'role' => $role->getName(),
'occupied' => $role->countUsers(),
'total' => $role->getCapacity(),
]);
} else {
$options[$role->getId()] = $role->getName();
}
}

return $options;
}

/**
* Vrací seznam rolí, které jsou v tuto chvíli registrovatelné nebo je uživatel má, s informací o jejich
* obsazenosti, jako možnosti pro select.
*
* @return string[]
*/
public function getRegisterableNowOrUsersOptionsWithCapacity(User $user) : array
{
$roles = $this->roleRepository->findAllRegisterableNowOrUsersOrderedByName($user);
$roles = $this->roleRepository->findFilteredRoles($registerableNowOnly, false, false, $includeUsers, $user);

$options = [];
foreach ($roles as $role) {
Expand Down
4 changes: 4 additions & 0 deletions app/Services/ApplicationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,10 @@ private function createRolesApplication(User $user, Collection $roles, User $cre
$user->setRolesApplicationDate(new DateTimeImmutable());
$this->userRepository->save($user);

if ($user->getRolesApplication() != null) {
throw new InvalidArgumentException('User is already registered.');
}

$application = new RolesApplication();
$application->setUser($user);
$application->setRoles($roles);
Expand Down
4 changes: 2 additions & 2 deletions app/Services/ExcelExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public function exportUsersSubeventsAndCategories(Collection $users, string $fil
$sheet->getColumnDimensionByColumn($column)->setAutoSize(false);
$sheet->getColumnDimensionByColumn($column++)->setWidth(20);

foreach ($this->subeventRepository->findExplicitOrderedByName() as $subevent) {
foreach ($this->subeventRepository->findFilteredSubevents(true, false, false, false) as $subevent) {
$sheet->setCellValueByColumnAndRow($column, $row, $subevent->getName());
$sheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
$sheet->getColumnDimensionByColumn($column)->setAutoSize(false);
Expand Down Expand Up @@ -524,7 +524,7 @@ public function exportUsersSubeventsAndCategories(Collection $users, string $fil

$sheet->setCellValueByColumnAndRow($column++, $row, $user->getNickname());

foreach ($this->subeventRepository->findExplicitOrderedByName() as $subevent) {
foreach ($this->subeventRepository->findFilteredSubevents(true, false, false, false) as $subevent) {
$sheet->setCellValueByColumnAndRow($column++, $row, $user->hasSubevent($subevent)
? $this->translator->translate('common.export.common.yes')
: $this->translator->translate('common.export.common.no'));
Expand Down
4 changes: 2 additions & 2 deletions app/Services/SubeventService.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getSubeventsWithoutSubeventOptions(int $subeventId) : array
*/
public function getSubeventsOptionsWithCapacity(bool $explicitOnly, bool $registerableNowOnly, bool $notRegisteredOnly, bool $includeUsers, ?User $user = null) : array
{
$subevents = $this->subeventRepository->findSubeventsOrderedByName($explicitOnly, $registerableNowOnly, $notRegisteredOnly, $includeUsers, $user);
$subevents = $this->subeventRepository->findFilteredSubevents($explicitOnly, $registerableNowOnly, $notRegisteredOnly, $includeUsers, $user);

$options = [];
foreach ($subevents as $subevent) {
Expand All @@ -106,7 +106,7 @@ public function getSubeventsOptionsWithCapacity(bool $explicitOnly, bool $regist
*/
public function getSubeventsOptionsWithUsersCount() : array
{
$subevents = $this->subeventRepository->findAllOrderedByName();
$subevents = $this->subeventRepository->findFilteredSubevents(false, false, false, false);

$options = [];
foreach ($subevents as $subevent) {
Expand Down
4 changes: 2 additions & 2 deletions app/WebModule/Components/ApplicationContentControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function render(?ContentDto $content = null) : void

$template->unapprovedRole = $user->isInRole($this->roleRepository->findBySystemName(Role::UNAPPROVED)->getName());
$template->nonregisteredRole = $user->isInRole($this->roleRepository->findBySystemName(Role::NONREGISTERED)->getName());
$template->noRegisterableRole = $this->roleRepository->findAllRegisterableNowOrderedByName()->isEmpty();
$template->noRegisterableRole = $this->roleRepository->findFilteredRoles(true, false, false, false)->isEmpty();
$template->registrationStart = $this->roleRepository->getRegistrationStart();
$template->registrationEnd = $this->roleRepository->getRegistrationEnd();
$template->bankAccount = $this->settingsService->getValue(Settings::ACCOUNT_NUMBER);
Expand All @@ -112,7 +112,7 @@ public function render(?ContentDto $content = null) : void
}

$template->explicitSubeventsExists = $explicitSubeventsExists;
$template->rolesWithSubevents = json_encode($this->roleRepository->findRolesIds($this->roleRepository->findAllWithSubevents()));
$template->rolesWithSubevents = json_encode($this->roleRepository->findRolesIds($this->roleRepository->findFilteredRoles(false, true, false, false)));

$template->render();
}
Expand Down
4 changes: 2 additions & 2 deletions app/WebModule/Components/ApplicationsGridControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function add(stdClass $values) : void
$this->redirect('this');
}

foreach ($this->subeventRepository->findExplicitOrderedByName() as $subevent) {
foreach ($this->subeventRepository->findFilteredSubevents(true, false, false, false) as $subevent) {
if (! $this->validators->validateSubeventsIncompatible($selectedAndUsersSubevents, $subevent)) {
$message = $this->translator->translate(
'web.profile.applications_incompatible_subevents_selected',
Expand Down Expand Up @@ -323,7 +323,7 @@ public function edit(string $id, stdClass $values) : void
$this->redirect('this');
}

foreach ($this->subeventRepository->findExplicitOrderedByName() as $subevent) {
foreach ($this->subeventRepository->findFilteredSubevents(true, false, false, false) as $subevent) {
if (! $this->validators->validateSubeventsIncompatible($selectedAndUsersSubevents, $subevent)) {
$message = $this->translator->translate(
'web.profile.applications_incompatible_subevents_selected',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
}
</script>

<div class="row">
<div class="row mb-3">
<div class="col">
<div class="card card-body bg-light pb-1">
{control applicationForm}
Expand Down
12 changes: 6 additions & 6 deletions app/WebModule/Forms/ApplicationFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public function processForm(Form $form, stdClass $values) : void
if (property_exists($values, 'roles')) {
$roles = $this->roleRepository->findRolesByIds($values->roles);
} else {
$roles = $this->roleRepository->findAllRegisterableNowOrderedByName();
$roles = $this->roleRepository->findFilteredRoles(true, false, false, false);
}

//podakce
Expand Down Expand Up @@ -421,7 +421,7 @@ private function addSubeventsSelect(Form $form) : void
->addRule(Form::FILLED, 'web.application_content.subevents_empty');

//generovani chybovych hlasek pro vsechny kombinace podakci
foreach ($this->subeventRepository->findExplicitOrderedByName() as $subevent) {
foreach ($this->subeventRepository->findFilteredSubevents(true, false, false, false) as $subevent) {
if (! $subevent->getIncompatibleSubevents()->isEmpty()) {
$subeventsSelect->addRule(
[$this, 'validateSubeventsIncompatible'],
Expand Down Expand Up @@ -453,7 +453,7 @@ private function addSubeventsSelect(Form $form) : void
*/
private function addRolesSelect(Form $form) : void
{
$registerableOptions = $this->aclService->getRegisterableNowOptionsWithCapacity();
$registerableOptions = $this->aclService->getRolesOptionsWithCapacity(true, false);

$rolesSelect = $form->addMultiSelect('roles', 'web.application_content.roles')->setItems(
$registerableOptions
Expand All @@ -463,7 +463,7 @@ private function addRolesSelect(Form $form) : void
->addRule([$this, 'validateRolesRegisterable'], 'web.application_content.role_is_not_registerable');

//generovani chybovych hlasek pro vsechny kombinace roli
foreach ($this->roleRepository->findAllRegisterableNowOrUsersOrderedByName($this->user) as $role) {
foreach ($this->roleRepository->findFilteredRoles(true, false, false, true, $this->user) as $role) {
if (! $role->getIncompatibleRoles()->isEmpty()) {
$rolesSelect->addRule(
[$this, 'validateRolesIncompatible'],
Expand All @@ -490,7 +490,7 @@ private function addRolesSelect(Form $form) : void
}

$ids = [];
foreach ($this->roleRepository->findAllWithArrivalDeparture() as $role) {
foreach ($this->roleRepository->findFilteredRoles(false, false, true, false) as $role) {
$ids[] = (string) $role->getId();
}

Expand Down Expand Up @@ -606,7 +606,7 @@ public function validateRolesRegisterable(MultiSelectBox $field) : bool
*/
public function toggleSubeventsRequired(MultiSelectBox $field) : bool
{
$rolesWithSubevents = $this->roleRepository->findRolesIds($this->roleRepository->findAllWithSubevents());
$rolesWithSubevents = $this->roleRepository->findRolesIds($this->roleRepository->findFilteredRoles(false, true, false, false));
foreach ($field->getValue() as $roleId) {
if (in_array($roleId, $rolesWithSubevents)) {
return true;
Expand Down
Loading

0 comments on commit 2322ba3

Please sign in to comment.