From ccea6dd95cceef084b4c215024042ac02397f41b Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Sun, 16 Oct 2022 19:22:19 +0200 Subject: [PATCH] issue641: add descendants to selected in alterQuery --- .../SocialIssueFilter.php | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php index a64dda2b8..aa689165f 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; +use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Form\Type\Select2SocialIssueType; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; @@ -56,7 +57,9 @@ class SocialIssueFilter implements FilterInterface $clause = $qb->expr()->in('acpsocialissue.id', ':socialissues'); $qb ->andWhere($clause) - ->setParameter('socialissues', $data['accepted_socialissues']); + ->setParameter('socialissues', + $this->addDescendantsIssues($data['accepted_socialissues']) + ); } public function applyOn() @@ -71,17 +74,18 @@ class SocialIssueFilter implements FilterInterface ]); } - public function describeAction($data, $format = 'string') + public function describeAction($data, $format = 'string'): array { $issues = []; $socialissues = $data['accepted_socialissues']; - foreach ($socialissues as $i) { - if ('null' === $i) { + foreach ($socialissues as $si) { + /** @var SocialIssue $si */ + if (null === $si) { $issues[] = $this->translator->trans('Not given'); } else { - $issues[] = $this->socialIssueRender->renderString($i, []); + $issues[] = $this->socialIssueRender->renderString($si, []); } } @@ -91,8 +95,36 @@ class SocialIssueFilter implements FilterInterface ], ]; } - public function getTitle() + public function getTitle(): string { return 'Filter by social issue'; } + + private function addDescendantsIssues($accepted_socialissues): array + { + $array = []; + + foreach ($accepted_socialissues as $si) { + /** @var SocialIssue $si */ + $array[] = $si; + if (!$si->hasParent()) { + $array = array_merge($array, $si->getDescendants()->toArray()); + } + } + + return $this->removeDuplicate($array); + } + + private function removeDuplicate(array $array): array + { + $ids = array_map(static function ($item) { + return $item->getId(); + }, $array); + + $unique_ids = array_unique($ids); + + return array_values( + array_intersect_key($array, $unique_ids) + ); + } }