diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/PersonFilters/GenderFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/PersonFilters/GenderFilter.php index bc90eb035..c52b7dd92 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/PersonFilters/GenderFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/PersonFilters/GenderFilter.php @@ -14,11 +14,13 @@ namespace Chill\PersonBundle\Export\Filter\PersonFilters; use Chill\MainBundle\Entity\Gender; use Chill\MainBundle\Export\ExportElementValidatedInterface; use Chill\MainBundle\Export\FilterInterface; +use Chill\MainBundle\Repository\GenderRepository; use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\PersonBundle\Export\Declarations; use Doctrine\ORM\Query\Expr; use Doctrine\ORM\QueryBuilder; use Symfony\Bridge\Doctrine\Form\Type\EntityType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Context\ExecutionContextInterface; use Symfony\Contracts\Translation\TranslatorInterface; @@ -27,7 +29,8 @@ class GenderFilter implements ExportElementValidatedInterface, FilterInterface { - public function __construct(private readonly TranslatorInterface $translator, private readonly TranslatableStringHelperInterface $translatableStringHelper) {} + // inject gender repository and find the active genders so that you can pass them to the ChoiceType (ordered by ordering) + public function __construct(private readonly TranslatorInterface $translator, private readonly TranslatableStringHelperInterface $translatableStringHelper, private readonly GenderRepository $genderRepository) {} public function addRole(): ?string { @@ -39,23 +42,17 @@ class GenderFilter implements $where = $qb->getDQLPart('where'); $isIn = $qb->expr()->in('person.gender', ':person_gender'); - if (!\in_array('null', $data['accepted_genders']->toArray(), true)) { + $acceptedGenders = $data['accepted_genders_entity']; + $nullIncluded = in_array(null, $acceptedGenders, true); + + if (!$nullIncluded) { $clause = $isIn; } else { $clause = $qb->expr()->orX($isIn, $qb->expr()->isNull('person.gender')); } - if ($where instanceof Expr\Andx) { - $where->add($clause); - } else { - $where = $qb->expr()->andX($clause); - } - - $qb->add('where', $where); - $qb->setParameter('person_gender', \array_filter( - $data['accepted_genders']->toArray(), - static fn ($el) => 'null' !== $el - )); + $qb->andWhere($clause); + $qb->setParameter('person_gender', array_filter($acceptedGenders, fn($gender) => $gender !== null)); } public function applyOn() @@ -65,11 +62,18 @@ class GenderFilter implements public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_genders', EntityType::class, [ - 'class' => Gender::class, - 'choice_label' => fn (Gender $g) => $this->translatableStringHelper->localize($g->getLabel()), + $genderChoices = $this->genderRepository->findByActiveOrdered(); + $choices = ['None' => null]; + + foreach ($genderChoices as $gender) { + $choices[$this->translatableStringHelper->localize($gender->getLabel())] = $gender->getId(); + } + + $builder->add('accepted_genders_entity', ChoiceType::class, [ + 'choices' => $choices, 'multiple' => true, 'expanded' => true, + 'placeholder' => 'Select gender', ]); } @@ -82,11 +86,11 @@ class GenderFilter implements { $genders = []; - foreach ($data['accepted_genders'] as $g) { - if ('null' === $g) { - $genders[] = $this->translator->trans('Not given'); + foreach ($data['accepted_genders_entity'] as $g) { + if (null === $g) { + $genders[] = $this->translator->trans('export.filter.person.gender.no_gender'); } else { - $genders[] = $this->translatableStringHelper->localize($g->getLabel()); + $genders[] = $this->translatableStringHelper->localize($this->genderRepository->find($g)->getLabel()); } } @@ -108,7 +112,7 @@ class GenderFilter implements public function validateForm($data, ExecutionContextInterface $context) { - if (!\is_iterable($data['accepted_genders']) || 0 === \count($data['accepted_genders'])) { + if (!\is_iterable($data['accepted_genders_entity']) || 0 === \count($data['accepted_genders_entity'])) { $context->buildViolation('You should select an option') ->addViolation(); } diff --git a/src/Bundle/ChillPersonBundle/Tests/Export/Filter/PersonFilters/GenderFilterTest.php b/src/Bundle/ChillPersonBundle/Tests/Export/Filter/PersonFilters/GenderFilterTest.php index 2d67987d4..142296521 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Export/Filter/PersonFilters/GenderFilterTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Export/Filter/PersonFilters/GenderFilterTest.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Tests\Export\Filter\PersonFilters; +use Chill\MainBundle\Entity\GenderEnum; use Chill\MainBundle\Test\Export\AbstractFilterTest; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Export\Filter\PersonFilters\GenderFilter; @@ -41,13 +42,13 @@ final class GenderFilterTest extends AbstractFilterTest { return [ [ - 'accepted_genders' => [Person::FEMALE_GENDER], + 'accepted_genders_entity' => [GenderEnum::FEMALE], ], [ - 'accepted_genders' => [Person::MALE_GENDER], + 'accepted_genders_entity' => [GenderEnum::MALE], ], [ - 'accepted_genders' => [Person::MALE_GENDER, Person::BOTH_GENDER], + 'accepted_genders_entity' => [GenderEnum::MALE, GenderEnum::NEUTRAL], ], ]; } diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index 4575d8403..73b0118ca 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -1189,6 +1189,8 @@ export: date_after: Après le date_before: Avant le title: Filtrer les usagers n'ayant été associés à aucun parcours + gender: + no_gender: genre non specifié course: not_having_address_reference: