From 0d38d4df40b497758e9f58a4501e6755f2590ebf Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Wed, 20 Jul 2022 19:33:42 +0200 Subject: [PATCH] social issues filter: improve render with parents, add parents in where clause --- .../Export/Filter/SocialIssueFilter.php | 55 +++++++++++++++++-- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/SocialIssueFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/SocialIssueFilter.php index eac3544f8..f82873dcd 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/SocialIssueFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/SocialIssueFilter.php @@ -6,6 +6,7 @@ use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Export\Declarations; +use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; use Symfony\Bridge\Doctrine\Form\Type\EntityType; @@ -24,20 +25,27 @@ class SocialIssueFilter implements FilterInterface */ private TranslatableStringHelper $translatableStringHelper; + /** + * @var SocialIssueRender + */ + private SocialIssueRender $socialIssueRender; + public function __construct( TranslatorInterface $translator, - TranslatableStringHelper $translatableStringHelper + TranslatableStringHelper $translatableStringHelper, + SocialIssueRender $socialIssueRender ) { $this->translator = $translator; $this->translatableStringHelper = $translatableStringHelper; + $this->socialIssueRender = $socialIssueRender; } public function buildForm(FormBuilderInterface $builder) { $builder->add('accepted_socialissue', EntityType::class, [ 'class' => SocialIssue::class, - 'choice_label' => function(SocialIssue $s) { - return $this->translatableStringHelper->localize($s->getTitle()); + 'choice_label' => function ($socialIssue) { + return $this->socialIssueRender->renderString($socialIssue, []); }, 'multiple' => true, 'expanded' => true, @@ -57,7 +65,7 @@ class SocialIssueFilter implements FilterInterface if ('null' === $i) { $issues[] = $this->translator->trans('Not given'); } else { - $issues[] = $this->translatableStringHelper->localize($i->getTitle()); + $issues[] = $this->socialIssueRender->renderString($i, []); } } @@ -86,7 +94,44 @@ class SocialIssueFilter implements FilterInterface } $qb->add('where', $where); - $qb->setParameter('socialissues', $data['accepted_socialissue']); + $qb->setParameter('socialissues', + $this->addParentIssues($data['accepted_socialissue']) + ); + } + + /** + * "Le filtre retiendra les parcours qui comportent cette problématique, + * ou une problématique parente à celles choisies." + * + * Add parent of each socialissue selected, and remove duplicates + * + * @param $accepted_issues + * @return array + */ + private function addParentIssues($accepted_issues) + { + $array = []; + foreach ($accepted_issues as $i) + { + /** @var SocialIssue $i */ + if ($i->hasParent()) { + $array[] = $i->getParent(); + } + $array[] = $i; + } + return $this->removeDuplicate($array); + } + + private function removeDuplicate(array $array): array + { + $ids = array_map(function ($item) { + return $item->getId(); + }, $array); + + $unique_ids = array_unique($ids); dump($unique_ids); + + return array_values( + array_intersect_key($array, $unique_ids)); } public function applyOn()