diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php index 7fb8b3b59..138583ef2 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.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\SocialAction; use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Form\Type\Select2SocialActionType; use Chill\PersonBundle\Templating\Entity\SocialActionRender; @@ -52,7 +53,9 @@ class SocialActionFilter implements FilterInterface $clause = $qb->expr()->in('acpwsocialaction.id', ':socialactions'); $qb ->andWhere($clause) - ->setParameter('socialactions', $data['accepted_socialactions']); + ->setParameter('socialactions', + $this->addDescendantsActions($data['accepted_socialactions']) + ); } public function applyOn(): string @@ -69,14 +72,16 @@ class SocialActionFilter implements FilterInterface public function describeAction($data, $format = 'string'): array { - $socialactions = []; + $actions = []; - foreach ($data['accepted_socialactions'] as $sa) { - $socialactions[] = $this->actionRender->renderString($sa, []); + $socialactions = $data['accepted_socialactions']; + + foreach ($socialactions as $action) { + $actions[] = $this->renderAction($action); } return ['Filtered by socialactions: only %socialactions%', [ - '%socialactions%' => implode(', ou ', $socialactions), + '%socialactions%' => implode(', ou ', $actions), ]]; } @@ -84,4 +89,45 @@ class SocialActionFilter implements FilterInterface { return 'Filter by socialaction'; } + + private function addDescendantsActions($accepted_socialactions): array + { + $array = []; + + foreach ($accepted_socialactions as $action) { + /** @var SocialAction $action */ + $array[] = $action; + if (!$action->hasParent()) { + $array = array_merge($array, $action->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) + ); + } + + private function renderAction(SocialAction $action): string + { + $render_str = $this->actionRender->renderString($action, []); + + if (!$action->hasParent()) { + if (count($action->getDescendants()) > 0) { + return $render_str . " (et dérivés)"; + } + } + + return $render_str; + } } diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php index b53960cad..ab22f2972 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Form\Type; use Chill\PersonBundle\Entity\SocialWork\SocialAction; +use Chill\PersonBundle\Repository\SocialWork\SocialActionRepository; use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; @@ -21,9 +22,14 @@ class Select2SocialActionType extends AbstractType { private SocialActionRender $actionRender; - public function __construct(SocialActionRender $actionRender) - { + private SocialActionRepository $actionRepository; + + public function __construct( + SocialActionRender $actionRender, + SocialActionRepository $actionRepository + ) { $this->actionRender = $actionRender; + $this->actionRepository = $actionRepository; } public function configureOptions(OptionsResolver $resolver) @@ -31,6 +37,7 @@ class Select2SocialActionType extends AbstractType $resolver ->setDefaults([ 'class' => SocialAction::class, + 'choices' => $this->actionRepository->findActionsNotDesactivated(), 'choice_label' => function (SocialAction $sa) { return $this->actionRender->renderString($sa, []); }, diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php index 052432741..3b8715d7e 100644 --- a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php @@ -67,4 +67,21 @@ final class SocialActionRepository implements ObjectRepository { return SocialAction::class; } + + public function findActionsNotDesactivated(): array + { + return $this->buildQueryWithDesactivatedDateCriteria()->getQuery()->getResult(); + } + + private function buildQueryWithDesactivatedDateCriteria(): QueryBuilder + { + $qb = $this->repository->createQueryBuilder('sa'); + + $qb ->where('sa.desactivationDate is null') + ->orWhere('sa.desactivationDate > :now') + ->orderBy('sa.ordering', 'ASC') + ->setParameter('now', new \DateTime('now')); + + return $qb; + } }