From 91af01336a1b21acdcf06e972cda9c4f6a3c2aa2 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 19 Sep 2022 13:46:24 +0200 Subject: [PATCH] fix goalResult aggregator --- .../GoalResultAggregator.php | 78 +++++++++++++++---- .../ResultAggregator.php | 14 +--- 2 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Aggregator/SocialWorkAggregators/GoalResultAggregator.php b/src/Bundle/ChillPersonBundle/Export/Aggregator/SocialWorkAggregators/GoalResultAggregator.php index 86bfcd71a..aeced53d7 100644 --- a/src/Bundle/ChillPersonBundle/Export/Aggregator/SocialWorkAggregators/GoalResultAggregator.php +++ b/src/Bundle/ChillPersonBundle/Export/Aggregator/SocialWorkAggregators/GoalResultAggregator.php @@ -5,20 +5,26 @@ namespace Chill\PersonBundle\Export\Aggregator\SocialWorkAggregators; use Chill\MainBundle\Export\AggregatorInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Export\Declarations; +use Chill\PersonBundle\Repository\SocialWork\GoalRepository; +use Chill\PersonBundle\Repository\SocialWork\ResultRepository; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; class GoalResultAggregator implements AggregatorInterface { - //private ResultRepository $resultRepository; + private ResultRepository $resultRepository; + + private GoalRepository $goalRepository; private TranslatableStringHelper $translatableStringHelper; public function __construct( - //ResultRepository $resultRepository, + ResultRepository $resultRepository, + GoalRepository $goalRepository, TranslatableStringHelper $translatableStringHelper ) { - //$this->resultRepository = $resultRepository; + $this->resultRepository = $resultRepository; + $this->goalRepository = $goalRepository; $this->translatableStringHelper = $translatableStringHelper; } @@ -28,19 +34,35 @@ class GoalResultAggregator implements AggregatorInterface */ public function getLabels($key, array $values, $data) { - return function ($value): string { - if ('_header' === $value) { - return 'Goal and result Type'; + return function ($value) use ($key): string { + switch ($key) { + case 'goal_aggregator': + + if ('_header' === $value) { + return 'Goal Type'; + } + + $g = $this->goalRepository->find($value); + + return $this->translatableStringHelper->localize( + $g->getTitle() + ); + + case 'result_aggregator': + + if ('_header' === $value) { + return 'Result Type'; + } + + $r = $this->resultRepository->find($value); + + return $this->translatableStringHelper->localize( + $r->getTitle() + ); + + default: + throw new \LogicException(); } - - //$g = $this->resultRepository->find($value); - - return - $value - //$this->translatableStringHelper->localize( - // $g->getTitle() - //) - ; }; } @@ -49,7 +71,10 @@ class GoalResultAggregator implements AggregatorInterface */ public function getQueryKeys($data): array { - return ['goal_result_aggregator']; + return [ + 'goal_aggregator', + 'result_aggregator' + ]; } /** @@ -81,7 +106,26 @@ class GoalResultAggregator implements AggregatorInterface */ public function alterQuery(QueryBuilder $qb, $data) { - // TODO: Implement alterQuery() method. + if (!in_array('goal', $qb->getAllAliases(), true)) { + $qb->join('acpw.goals', 'goal'); + } + + if (!in_array('goalresult', $qb->getAllAliases(), true)) { + $qb->join('goal.results', 'goalresult'); + } + + $qb->addSelect('IDENTITY(goal.goal) as goal_aggregator'); + $qb->addSelect('goalresult.id as result_aggregator'); + + $groupBy = $qb->getDQLPart('groupBy'); + + if (!empty($groupBy)) { + $qb->addGroupBy('goal_aggregator'); + } else { + $qb->groupBy('goal_aggregator'); + } + + $qb->addGroupBy('result_aggregator'); } /** diff --git a/src/Bundle/ChillPersonBundle/Export/Aggregator/SocialWorkAggregators/ResultAggregator.php b/src/Bundle/ChillPersonBundle/Export/Aggregator/SocialWorkAggregators/ResultAggregator.php index 2bbd8445c..8f7ac0624 100644 --- a/src/Bundle/ChillPersonBundle/Export/Aggregator/SocialWorkAggregators/ResultAggregator.php +++ b/src/Bundle/ChillPersonBundle/Export/Aggregator/SocialWorkAggregators/ResultAggregator.php @@ -37,19 +37,11 @@ final class ResultAggregator implements AggregatorInterface public function alterQuery(QueryBuilder $qb, $data) { - if (!in_array('acpwresult', $qb->getAllAliases(), true)) { - $qb->join('acpw.results', 'acpwresult'); + if (!in_array('result', $qb->getAllAliases(), true)) { + $qb->join('acpw.results', 'result'); } - if (!in_array('goal', $qb->getAllAliases(), true)) { - $qb->join('acpw.goals', 'goal'); - } - - if (!in_array('goalresult', $qb->getAllAliases(), true)) { - $qb->join('goal.results', 'goalresult'); - } - - $qb->addSelect('acpwresult.id, IDENTITY(goal.results) as result_aggregator'); + $qb->addSelect('result.id as result_aggregator'); $groupBy = $qb->getDQLPart('groupBy');