From c17036fcdaf2259bfc80435a513614c5559c4c27 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Wed, 17 Aug 2022 15:27:37 +0200 Subject: [PATCH] exports: fix activitytype filter/aggregator appliedTo activity_person and too activity_acl --- .../Aggregator/ActivityTypeAggregator.php | 18 +++++++---- .../Export/LinkedToACP/CountActivity.php | 25 +++++++--------- .../Export/LinkedToPerson/CountActivity.php | 13 ++++---- .../Export/Filter/ActivityTypeFilter.php | 30 ++++++++----------- .../translations/messages.fr.yml | 2 +- .../translations/messages.nl.yaml | 2 +- 6 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Export/Aggregator/ActivityTypeAggregator.php b/src/Bundle/ChillActivityBundle/Export/Aggregator/ActivityTypeAggregator.php index 3d73e0648..cedd2df8c 100644 --- a/src/Bundle/ChillActivityBundle/Export/Aggregator/ActivityTypeAggregator.php +++ b/src/Bundle/ChillActivityBundle/Export/Aggregator/ActivityTypeAggregator.php @@ -45,16 +45,24 @@ class ActivityTypeAggregator implements AggregatorInterface public function alterQuery(QueryBuilder $qb, $data) { - // add select element - $qb->addSelect(sprintf('IDENTITY(activity.type) AS %s', self::KEY)); + if (!in_array('type', $qb->getAllAliases())) { + $qb->join('activity.activityType', 'type'); + } - // add the "group by" part - $qb->addGroupBy(self::KEY); + $qb->addSelect(sprintf('IDENTITY(activity.activityType) AS %s', self::KEY)); + + $groupby = $qb->getDQLPart('groupBy'); + + if (!empty($groupBy)) { + $qb->addGroupBy(self::KEY); + } else { + $qb->groupBy(self::KEY); + } } public function applyOn(): string { - return Declarations::ACTIVITY_PERSON; + return Declarations::ACTIVITY; } public function buildForm(FormBuilderInterface $builder) diff --git a/src/Bundle/ChillActivityBundle/Export/Export/LinkedToACP/CountActivity.php b/src/Bundle/ChillActivityBundle/Export/Export/LinkedToACP/CountActivity.php index 2cbd7ddd8..55c0a1974 100644 --- a/src/Bundle/ChillActivityBundle/Export/Export/LinkedToACP/CountActivity.php +++ b/src/Bundle/ChillActivityBundle/Export/Export/LinkedToACP/CountActivity.php @@ -11,13 +11,15 @@ declare(strict_types=1); namespace Chill\ActivityBundle\Export\Export\LinkedToACP; +use Chill\ActivityBundle\Entity\Activity; use Chill\ActivityBundle\Export\Declarations; -use Chill\ActivityBundle\Repository\ActivityRepository; use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter; use Chill\MainBundle\Export\ExportInterface; use Chill\MainBundle\Export\FormatterInterface; use Chill\MainBundle\Export\GroupedExportInterface; use Chill\PersonBundle\Export\Declarations as PersonDeclarations; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Query; use LogicException; use Symfony\Component\Form\FormBuilderInterface; @@ -25,12 +27,12 @@ use Symfony\Component\Security\Core\Role\Role; class CountActivity implements ExportInterface, GroupedExportInterface { - protected ActivityRepository $activityRepository; + protected EntityRepository $repository; public function __construct( - ActivityRepository $activityRepository + EntityManagerInterface $em ) { - $this->activityRepository = $activityRepository; + $this->repository = $em->getRepository(Activity::class); } public function buildForm(FormBuilderInterface $builder) @@ -78,17 +80,12 @@ class CountActivity implements ExportInterface, GroupedExportInterface public function initiateQuery(array $requiredModifiers, array $acl, array $data = []) { - $centers = array_map(static fn ($el) => $el['center'], $acl); - - $qb = $this - ->activityRepository - ->createQueryBuilder('activity') - ->select('COUNT(activity.id) as export_count_activity'); - - $qb->andWhere( - $qb->expr()->isNotNull('activity.accompanyingPeriod') - ); + $qb = $this->repository->createQueryBuilder('activity') + ->join('activity.accompanyingPeriod', 'acp') + ; + $qb->select('COUNT(activity.id) as export_count_activity'); + return $qb; } diff --git a/src/Bundle/ChillActivityBundle/Export/Export/LinkedToPerson/CountActivity.php b/src/Bundle/ChillActivityBundle/Export/Export/LinkedToPerson/CountActivity.php index 96c79297e..75d3122c3 100644 --- a/src/Bundle/ChillActivityBundle/Export/Export/LinkedToPerson/CountActivity.php +++ b/src/Bundle/ChillActivityBundle/Export/Export/LinkedToPerson/CountActivity.php @@ -80,15 +80,16 @@ class CountActivity implements ExportInterface, GroupedExportInterface { $centers = array_map(static fn ($el) => $el['center'], $acl); - $qb = $this - ->activityRepository - ->createQueryBuilder('activity') - ->select('COUNT(activity.id) as export_count_activity') - ->join('activity.person', 'person'); + $qb = $this->activityRepository->createQueryBuilder('activity') + ->join('activity.person', 'person') + ; + + $qb->select('COUNT(activity.id) as export_count_activity'); $qb ->where($qb->expr()->in('person.center', ':centers')) - ->setParameter('centers', $centers); + ->setParameter('centers', $centers) + ; return $qb; } diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ActivityTypeFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ActivityTypeFilter.php index bfd80ecc7..665515cb1 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ActivityTypeFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ActivityTypeFilter.php @@ -64,37 +64,31 @@ class ActivityTypeFilter implements ExportElementValidatedInterface, FilterInter public function applyOn(): string { - return Declarations::ACTIVITY_PERSON; + return Declarations::ACTIVITY; } public function buildForm(FormBuilderInterface $builder) { - $builder->add( - 'types', - EntityType::class, - [ - 'class' => ActivityType::class, - 'choice_label' => fn (ActivityType $type) => $this->translatableStringHelper->localize($type->getName()), - 'multiple' => true, - 'expanded' => false, - ] - ); + $builder->add('types', EntityType::class, [ + 'class' => ActivityType::class, + 'choice_label' => fn (ActivityType $type) => $this->translatableStringHelper->localize($type->getName()), + 'group_by' => fn (ActivityType $type) => $this->translatableStringHelper->localize($type->getCategory()->getName()), + 'multiple' => true, + 'expanded' => false, + ]); } public function describeAction($data, $format = 'string') { // collect all the reasons'name used in this filter in one array $reasonsNames = array_map( - fn (ActivityType $t): string => '"' . $this->translatableStringHelper->localize($t->getName()) . '"', + fn (ActivityType $t): string => $this->translatableStringHelper->localize($t->getName()), $this->activityTypeRepository->findBy(['id' => $data['types']->toArray()]) ); - return [ - 'Filtered by activity type: only %list%', - [ - '%list%' => implode(', ', $reasonsNames), - ], - ]; + return ['Filtered by activity type: only %list%', [ + '%list%' => implode(', ou ', $reasonsNames), + ]]; } public function getTitle() diff --git a/src/Bundle/ChillActivityBundle/translations/messages.fr.yml b/src/Bundle/ChillActivityBundle/translations/messages.fr.yml index 82c821f51..7eacba1ce 100644 --- a/src/Bundle/ChillActivityBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillActivityBundle/translations/messages.fr.yml @@ -230,7 +230,7 @@ Average activities linked to an accompanying period visit duration by various pa #filters Filter by reason: Filtrer par sujet d'activité 'Filtered by reasons: only %list%': 'Filtré par sujet: seulement %list%' -'Filtered by activity type: only %list%': "Filtré par type d'activité: seulement %list%" +'Filtered by activity type: only %list%': "Filtré par type d'activité: uniquement %list%" Filtered by date activity: Filtrer par date d'activité Activities after this date: Activités après cette date Activities before this date: Activités avant cette date diff --git a/src/Bundle/ChillActivityBundle/translations/messages.nl.yaml b/src/Bundle/ChillActivityBundle/translations/messages.nl.yaml index d24eacf0a..0403e55ee 100644 --- a/src/Bundle/ChillActivityBundle/translations/messages.nl.yaml +++ b/src/Bundle/ChillActivityBundle/translations/messages.nl.yaml @@ -195,7 +195,7 @@ Number of activities: Nombre d'activités #filters Filter by reason: Filtrer par sujet d'activité 'Filtered by reasons: only %list%': 'Filtré par sujet: seulement %list%' -'Filtered by activity type: only %list%': "Filtré par type d'activity: seulement %list%" +'Filtered by activity type: only %list%': "Filtré par type d'activity: uniquement %list%" Filtered by date activity: Filtrer par date d'activité Activities after this date: Activités après cette date Activities before this date: Activités avant cette date