From 999d4e2038ba44408488db30da81b727ccf8d24b Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 10 Oct 2022 15:25:07 +0200 Subject: [PATCH] issue649: adapt existing UserCurrentLocationType to use select2 with AdministrativeLocationFilter --- .../Form/UserCurrentLocationType.php | 26 +++++++++++ .../AdministrativeLocationFilter.php | 45 +++++++++++-------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php index 3f839d3ed..00c194420 100644 --- a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php +++ b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php @@ -17,6 +17,9 @@ use Chill\MainBundle\Templating\TranslatableStringHelper; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; +use Symfony\Component\OptionsResolver\OptionsResolver; class UserCurrentLocationType extends AbstractType { @@ -32,6 +35,7 @@ class UserCurrentLocationType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { + //dump($options); $builder ->add('currentLocation', EntityType::class, [ 'class' => Location::class, @@ -43,7 +47,29 @@ class UserCurrentLocationType extends AbstractType }, 'placeholder' => 'Pick a location', 'required' => false, + 'label' => $options['label'], + 'label_attr' => $options['label_attr'], + 'multiple' => $options['multiple'], 'attr' => ['class' => 'select2'], ]); } + + public function buildView(FormView $view, FormInterface $form, array $options) + { + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver + ->setDefault('label', 'Current location') + ->setDefault('label_attr', []) + ->setDefault('multiple', false) + ->setAllowedTypes('multiple', ['bool']) + ; + } + + public function getBlockPrefix() + { + return 'user_current_location_type'; + } } diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php index 4ded757f3..65f0c5b3b 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php @@ -11,22 +11,26 @@ declare(strict_types=1); namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; -use Chill\MainBundle\Entity\Location; +//use Chill\MainBundle\Entity\Location; use Chill\MainBundle\Export\FilterInterface; +use Chill\MainBundle\Form\UserCurrentLocationType; +//use Chill\MainBundle\Repository\LocationRepository; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Export\Declarations; -use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; -use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormBuilderInterface; class AdministrativeLocationFilter implements FilterInterface { + //private LocationRepository $locationRepository; + private TranslatableStringHelper $translatableStringHelper; public function __construct( + //LocationRepository $locationRepository, TranslatableStringHelper $translatableStringHelper ) { + //$this->locationRepository = $locationRepository; $this->translatableStringHelper = $translatableStringHelper; } @@ -37,17 +41,10 @@ class AdministrativeLocationFilter implements FilterInterface public function alterQuery(QueryBuilder $qb, $data) { - $where = $qb->getDQLPart('where'); $clause = $qb->expr()->in('acp.administrativeLocation', ':locations'); - - if ($where instanceof Andx) { - $where->add($clause); - } else { - $where = $qb->expr()->andX($clause); - } - - $qb->add('where', $where); - $qb->setParameter('locations', $data['accepted_locations']); + $qb + ->andWhere($clause) + ->setParameter('locations', $data['accepted_locations']); } public function applyOn(): string @@ -57,13 +54,23 @@ class AdministrativeLocationFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_locations', EntityType::class, [ - 'class' => Location::class, - 'choice_label' => function (Location $l) { - return $l->getName() . ' (' . $this->translatableStringHelper->localize($l->getLocationType()->getTitle()) . ')'; - }, + + //$builder->add('accepted_locations', EntityType::class, [ + // 'class' => Location::class, + // 'choices' => $this->locationRepository->findByPublicLocations(), + // 'choice_label' => function (Location $l) { + // return $l->getName() . ' (' . $this->translatableStringHelper->localize($l->getLocationType()->getTitle()) . ')'; + // }, + // 'multiple' => true, + // 'expanded' => true, + //]); + + $builder->add('accepted_locations', UserCurrentLocationType::class, [ + 'label' => 'Accepted locations', + 'label_attr' => [ + //'class' => 'd-none' + ], 'multiple' => true, - 'expanded' => true, ]); }