mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-29 05:26:13 +00:00
Customize genderFilter to include a NULL choice + add translation and adjust test
This commit is contained in:
parent
30ebd00693
commit
0d2e0b4e91
@ -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();
|
||||
}
|
||||
|
@ -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],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user