From 0b7651f5197c8b9f7d923957dab0d66bd8b5b7fa Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 7 Mar 2023 10:56:03 +0100 Subject: [PATCH] FIX changes to repository: using ACLAware instead of deprecated method --- .../Repository/ActivityACLAwareRepository.php | 100 ++++++++++++++---- .../ActivityACLAwareRepositoryInterface.php | 1 + .../Repository/ActivityRepository.php | 92 ++++++++-------- 3 files changed, 125 insertions(+), 68 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepository.php b/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepository.php index 45401543a..697bc5213 100644 --- a/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepository.php +++ b/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepository.php @@ -64,31 +64,31 @@ final class ActivityACLAwareRepository implements ActivityACLAwareRepositoryInte $this->security = $security; } - public function findByAccompanyingPeriod( - AccompanyingPeriod $period, - string $role, - ?int $start = 0, - ?DateTime $before = null, - ?DateTime $after = null, - ?array $userJob = [], - ?array $activityTypes = [], - bool $onlyMe = false, - ?int $limit = 1000, - ?array $orderBy = [] - ): array { - $user = $this->security->getUser(); - $center = $this->centerResolverDispatcher->resolveCenter($period); + /* public function findByAccompanyingPeriod( + AccompanyingPeriod $period, + string $role, + ?int $start = 0, + ?DateTime $before = null, + ?DateTime $after = null, + ?array $userJob = [], + ?array $activityTypes = [], + bool $onlyMe = false, + ?int $limit = 1000, + ?array $orderBy = [] + ): array { + $user = $this->security->getUser(); + $center = $this->centerResolverDispatcher->resolveCenter($period); - if (0 === count($orderBy)) { - $orderBy = ['date' => 'DESC']; - } + if (0 === count($orderBy)) { + $orderBy = ['date' => 'DESC']; + } - $scopes = $this->authorizationHelper - ->getReachableCircles($user, $role, $center); + $scopes = $this->authorizationHelper + ->getReachableCircles($user, $role, $center); - return $this->em->getRepository(Activity::class) - ->findByAccompanyingPeriod($period, $scopes, true, $before, $after, $userJob, $activityTypes, $onlyMe, $limit, $start, $orderBy); - } + return $this->em->getRepository(Activity::class) + ->findByAccompanyingPeriod($period, $scopes, true, $before, $after, $userJob, $activityTypes, $onlyMe, $limit, $start, $orderBy); + }*/ public function findByAccompanyingPeriodSimplified(AccompanyingPeriod $period, ?int $limit = 1000): array { @@ -210,6 +210,62 @@ final class ActivityACLAwareRepository implements ActivityACLAwareRepositoryInte ]; } + /** + * @return Activity[] + */ + private function findByAccompanyingPeriod( + AccompanyingPeriod $period, + string $role, + ?int $start = 0, + ?DateTime $before = null, + ?DateTime $after = null, + ?array $userJob = [], + ?array $activityTypes = [], + bool $onlyMe = false, + ?int $limit = 100, + ?int $offset = 0, + ?array $orderBy = ['date' => 'desc'] + ): array { + $qb = $this->createQueryBuilder('a'); + $qb->select('a'); + + $user = $this->security->getUser(); + $center = $this->centerResolverDispatcher->resolveCenter($period); + + if (0 === count($orderBy)) { + $orderBy = ['date' => 'DESC']; + } + + $scopes = $this->authorizationHelper + ->getReachableCircles($user, $role, $center); + + $qb + ->where( + $qb->expr()->orX( + $qb->expr()->in('a.scope', ':scopes'), + $qb->expr()->isNull('a.scope') + ) + ) + ->setParameter('scopes', $scopes); + + $qb + ->andWhere( + $qb->expr()->eq('a.accompanyingPeriod', ':period') + ) + ->setParameter('period', $period); + + //Add filter queries + $this->repository->addQueryFilters($qb, $userJob, $activityTypes, $after, $before, $onlyMe); + + foreach ($orderBy as $k => $dir) { + $qb->addOrderBy('a.' . $k, $dir); + } + + $qb->setMaxResults($limit)->setFirstResult($offset); + + return $qb->getQuery()->getResult(); + } + private function getFromClauseCenter(array $args): string { $metadataActivity = $this->em->getClassMetadata(Activity::class); diff --git a/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepositoryInterface.php b/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepositoryInterface.php index 7592faa02..323ea9080 100644 --- a/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepositoryInterface.php +++ b/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepositoryInterface.php @@ -31,6 +31,7 @@ interface ActivityACLAwareRepositoryInterface ?array $activityTypes = [], bool $onlyMe = false, ?int $limit = 1000, + ?int $offset = 0, ?array $orderBy = [] ): array; diff --git a/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php b/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php index c93ad168a..fde726d36 100644 --- a/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php +++ b/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php @@ -39,6 +39,52 @@ class ActivityRepository extends ServiceEntityRepository $this->security = $security; } + /** + * @param array|UserJob $jobs + * @param array|ActivityType $types + * @param DateTime $dateFrom + * @param DateTime $dateTo + */ + public function addQueryFilters(QueryBuilder $qb, array $jobs, array $types, ?DateTime $dateFrom, ?DateTime $dateTo, bool $onlyMe): QueryBuilder + { + if (0 < count($jobs)) { + //TODO check for jobs of all users involved + $qb->innerJoin('a.user', 'u'); + $qb->andWhere($qb->expr()->in('u.userJob', ':jobs')) + ->setParameter('jobs', $jobs); + } + + if (0 < count($types)) { + $qb->andWhere($qb->expr()->in('a.activityType', ':types')) + ->setParameter('types', $types); + } + + if (null !== $dateFrom && null !== $dateTo) { + $qb->andWhere($qb->expr()->between( + 'a.date', + ':date_from', + ':date_to' + )) + ->setParameter('date_from', $dateFrom) + ->setParameter('date_to', $dateTo); + } elseif (null !== $dateFrom && null === $dateTo) { + $qb->andWhere($qb->expr()->gt('a.date', ':date_from')) + ->setParameter('date_from', $dateFrom); + } elseif (null === $dateFrom && null !== $dateTo) { + $qb->andWhere($qb->expr()->lt('a.date', ':date_to')) + ->setParameter('date_to', $dateTo); + } + + if (true === $onlyMe) { + $currentUser = $this->security->getUser(); + + $qb->andWhere($qb->expr()->eq('a.user', ':currentUser')) + ->setParameter('currentUser', $currentUser); + } + + return $qb; + } + /** * @deprecated use @see{ActivityACLAwareRepositoryInterface::findByAccompanyingPeriod} * @@ -120,50 +166,4 @@ class ActivityRepository extends ServiceEntityRepository return $qb->getQuery()->getResult(); } - - /** - * @param array|UserJob $jobs - * @param array|ActivityType $types - * @param DateTime $dateFrom - * @param DateTime $dateTo - */ - private function addQueryFilters(QueryBuilder $qb, array $jobs, array $types, ?DateTime $dateFrom, ?DateTime $dateTo, bool $onlyMe): QueryBuilder - { - if (0 < count($jobs)) { - //TODO check for jobs of all users involved - $qb->innerJoin('a.user', 'u'); - $qb->andWhere($qb->expr()->in('u.userJob', ':jobs')) - ->setParameter('jobs', $jobs); - } - - if (0 < count($types)) { - $qb->andWhere($qb->expr()->in('a.activityType', ':types')) - ->setParameter('types', $types); - } - - if (null !== $dateFrom && null !== $dateTo) { - $qb->andWhere($qb->expr()->between( - 'a.date', - ':date_from', - ':date_to' - )) - ->setParameter('date_from', $dateFrom) - ->setParameter('date_to', $dateTo); - } elseif (null !== $dateFrom && null === $dateTo) { - $qb->andWhere($qb->expr()->gt('a.date', ':date_from')) - ->setParameter('date_from', $dateFrom); - } elseif (null === $dateFrom && null !== $dateTo) { - $qb->andWhere($qb->expr()->lt('a.date', ':date_to')) - ->setParameter('date_to', $dateTo); - } - - if (true === $onlyMe) { - $currentUser = $this->security->getUser(); - - $qb->andWhere($qb->expr()->eq('a.user', ':currentUser')) - ->setParameter('currentUser', $currentUser); - } - - return $qb; - } }