From 08df1c4ac8ea3dc3f86b3d1025b7d293a2325bd7 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Fri, 3 Mar 2023 11:53:44 +0100 Subject: [PATCH] FEATURE [repository] add parameters for filter data - typing error still for userjob --- .../Controller/ActivityController.php | 35 +++++++++--- .../Repository/ActivityACLAwareRepository.php | 18 ++++-- .../ActivityACLAwareRepositoryInterface.php | 14 ++++- .../Repository/ActivityRepository.php | 55 +++++++++++++++---- 4 files changed, 100 insertions(+), 22 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php index 02ac23b2f..1b2e66ea4 100644 --- a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php +++ b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php @@ -312,18 +312,39 @@ final class ActivityController extends AbstractController $view = 'ChillActivityBundle:Activity:listPerson.html.twig'; } elseif ($accompanyingPeriod instanceof AccompanyingPeriod) { - $this->denyAccessUnlessGranted(ActivityVoter::SEE, $accompanyingPeriod); - $data = $form->getData(); + if ($form->isSubmitted() && $form->isValid()) { + $data = $form->getData(); + dump($data['types']->getValues()); + $activities = $this->activityACLAwareRepository + ->findByAccompanyingPeriod( + $accompanyingPeriod, + ActivityVoter::SEE, + 0, + $data['dateTo'], + $data['dateFrom'], + $data['jobs']->getValues(), + $data['types']->getValues(), + null, + ['date' => 'DESC', 'id' => 'DESC'] + ); + } $activities = $this->activityACLAwareRepository - ->findByAccompanyingPeriod($accompanyingPeriod, ActivityVoter::SEE, 0, null, ['date' => 'DESC', 'id' => 'DESC']); - - $view = 'ChillActivityBundle:Activity:listAccompanyingCourse.html.twig'; + ->findByAccompanyingPeriod( + $accompanyingPeriod, + ActivityVoter::SEE, + 0, + null, + null, + [], + [], + null, + ['date' => 'DESC', 'id' => 'DESC'] + ); } - - + $view = 'ChillActivityBundle:Activity:listAccompanyingCourse.html.twig'; return $this->render( $view, diff --git a/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepository.php b/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepository.php index fa36c8d07..b412b18bf 100644 --- a/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepository.php +++ b/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepository.php @@ -22,14 +22,15 @@ use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; +use DateTime; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\AbstractQuery; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Query\ResultSetMappingBuilder; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; + use Symfony\Component\Security\Core\Role\Role; use Symfony\Component\Security\Core\Security; - use function count; use function in_array; @@ -63,8 +64,17 @@ final class ActivityACLAwareRepository implements ActivityACLAwareRepositoryInte $this->security = $security; } - public function findByAccompanyingPeriod(AccompanyingPeriod $period, string $role, ?int $start = 0, ?int $limit = 1000, ?array $orderBy = []): array - { + public function findByAccompanyingPeriod( + AccompanyingPeriod $period, + string $role, + ?int $start = 0, + ?DateTime $before = null, + ?DateTime $after = null, + ?array $userJob = [], + ?array $activityTypes = [], + ?int $limit = 1000, + ?array $orderBy = [] + ): array { $user = $this->security->getUser(); $center = $this->centerResolverDispatcher->resolveCenter($period); @@ -76,7 +86,7 @@ final class ActivityACLAwareRepository implements ActivityACLAwareRepositoryInte ->getReachableCircles($user, $role, $center); return $this->em->getRepository(Activity::class) - ->findByAccompanyingPeriod($period, $scopes, true, $limit, $start, $orderBy); + ->findByAccompanyingPeriod($period, $scopes, true, $before, $after, $userJob, $activityTypes, $limit, $start, $orderBy); } public function findByAccompanyingPeriodSimplified(AccompanyingPeriod $period, ?int $limit = 1000): array diff --git a/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepositoryInterface.php b/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepositoryInterface.php index 8cdb83524..ebba45c37 100644 --- a/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepositoryInterface.php +++ b/src/Bundle/ChillActivityBundle/Repository/ActivityACLAwareRepositoryInterface.php @@ -11,15 +11,27 @@ declare(strict_types=1); namespace Chill\ActivityBundle\Repository; +use Chill\ActivityBundle\Entity\Activity; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; +use DateTime; interface ActivityACLAwareRepositoryInterface { /** * @return Activity[]|array */ - public function findByAccompanyingPeriod(AccompanyingPeriod $period, string $role, ?int $start = 0, ?int $limit = 1000, ?array $orderBy = []): array; + public function findByAccompanyingPeriod( + AccompanyingPeriod $period, + string $role, + ?int $start = 0, + ?DateTime $before = null, + ?DateTime $after = null, + ?array $userJob = [], + ?array $activityTypes = [], + ?int $limit = 1000, + ?array $orderBy = [] + ): array; /** * Return a list of activities, simplified as array (not object). diff --git a/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php b/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php index 3519cf6fd..5b28fe586 100644 --- a/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php +++ b/src/Bundle/ChillActivityBundle/Repository/ActivityRepository.php @@ -16,10 +16,12 @@ use Chill\ActivityBundle\Entity\ActivityType; use Chill\MainBundle\Entity\UserJob; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; +use DateTime; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\Security\Core\Security; +use function count; /** * @method Activity|null find($id, $lockMode = null, $lockVersion = null) @@ -42,8 +44,18 @@ class ActivityRepository extends ServiceEntityRepository * * @return Activity[] */ - public function findByAccompanyingPeriod(AccompanyingPeriod $period, array $scopes, ?bool $allowNullScope = false, ?int $limit = 100, ?int $offset = 0, array $orderBy = ['date' => 'desc']): array - { + public function findByAccompanyingPeriod( + AccompanyingPeriod $period, + array $scopes, + ?bool $allowNullScope = false, + ?DateTime $before = null, + ?DateTime $after = null, + ?array $userJob = [], + array $activityTypes = [], + ?int $limit = 100, + ?int $offset = 0, + array $orderBy = ['date' => 'desc'] + ): array { $qb = $this->createQueryBuilder('a'); $qb->select('a'); @@ -68,12 +80,41 @@ class ActivityRepository extends ServiceEntityRepository ) ->setParameter('period', $period); + // Add filter parameters to query if any + if ($before) { + $qb->andWhere( + $qb->expr()->lt('a.date', ':before') + ); + $qb->setParameter('before', $before); + } + + if ($after) { + $qb->andWhere( + $qb->expr()->lt('a.date', ':after') + ); + $qb->setParameter('after', $after); + } + + if (count($activityTypes) > 0) { + $qb->andWhere( + $qb->expr()->in('a.activityType', ':activityTypes') + ); + $qb->setParameter('activityTypes', $activityTypes); + } + + /*if (null !== $userJob) { + $qb->innerJoin() + }*/ + foreach ($orderBy as $k => $dir) { $qb->addOrderBy('a.' . $k, $dir); } $qb->setMaxResults($limit)->setFirstResult($offset); + dump($qb->getQuery()); + dump($qb->getQuery()->getResult()); + return $qb->getQuery()->getResult(); } @@ -106,15 +147,10 @@ class ActivityRepository extends ServiceEntityRepository } /** - * @param QueryBuilder $qb * @param array|UserJob $jobs * @param array|ActivityType $types - * @param \DateTime $dateFrom - * @param \DateTime $dateTo - * @param bool $onlyMe - * @return QueryBuilder */ - private function addQueryFilters(QueryBuilder $qb, array $jobs, array $types, \DateTime $dateFrom, \DateTime $dateTo, bool $onlyMe): QueryBuilder + private function addQueryFilters(QueryBuilder $qb, array $jobs, array $types, DateTime $dateFrom, DateTime $dateTo, bool $onlyMe): QueryBuilder { if (0 < count($jobs)) { $qb->join('a.user', 'u'); @@ -127,8 +163,7 @@ class ActivityRepository extends ServiceEntityRepository ->setParameter('types', $types); } - if (null !== $dateFrom && null !== $dateTo) - { + if (null !== $dateFrom && null !== $dateTo) { $qb->andWhere($qb->expr()->between( 'a.date', ':date_from',