From 999d4e2038ba44408488db30da81b727ccf8d24b Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 10 Oct 2022 15:25:07 +0200 Subject: [PATCH 1/4] 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, ]); } From 087270829eb7ea8b571c81ea5cb95ff26c57283d Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 10 Oct 2022 15:26:40 +0200 Subject: [PATCH 2/4] cleaning previous --- .../Form/UserCurrentLocationType.php | 1 - .../AdministrativeLocationFilter.php | 15 --------------- 2 files changed, 16 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php index 00c194420..a99811bfa 100644 --- a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php +++ b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php @@ -35,7 +35,6 @@ class UserCurrentLocationType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { - //dump($options); $builder ->add('currentLocation', EntityType::class, [ 'class' => Location::class, diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php index 65f0c5b3b..b10a66944 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php @@ -11,10 +11,8 @@ declare(strict_types=1); namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; -//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\QueryBuilder; @@ -22,15 +20,12 @@ 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; } @@ -55,16 +50,6 @@ class AdministrativeLocationFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - //$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' => [ From 7276cb971f87dd060cbb82a2648844a5b96f1005 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 10 Oct 2022 15:59:16 +0200 Subject: [PATCH 3/4] issue649: new formType to use select2 with LocationTypeFilter --- .../Filter/ACPFilters/LocationTypeFilter.php | 11 ++-- .../Form/Type/Select2LocationTypeType.php | 51 +++++++++++++++++++ .../translations/messages.fr.yml | 1 + 3 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php index 92dc1b0eb..023882cf9 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php @@ -12,12 +12,11 @@ declare(strict_types=1); namespace Chill\ActivityBundle\Export\Filter\ACPFilters; use Chill\ActivityBundle\Export\Declarations; -use Chill\MainBundle\Entity\LocationType; use Chill\MainBundle\Export\FilterInterface; +use Chill\MainBundle\Form\Type\Select2LocationTypeType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; -use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormBuilderInterface; use function in_array; @@ -61,13 +60,9 @@ class LocationTypeFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_locationtype', EntityType::class, [ - 'class' => LocationType::class, - 'choice_label' => function (LocationType $type) { - return $this->translatableStringHelper->localize($type->getTitle()); - }, + $builder->add('accepted_locationtype', Select2LocationTypeType::class, [ 'multiple' => true, - 'expanded' => true, + //'label' => false, ]); } diff --git a/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php b/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php new file mode 100644 index 000000000..8fd5dbe91 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php @@ -0,0 +1,51 @@ +translatableStringHelper = $translatableStringHelper; + } + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder->add('locationtype', EntityType::class, [ + 'class' => LocationType::class, + 'choice_label' => function (LocationType $type) { + return $this->translatableStringHelper->localize($type->getTitle()); + }, + 'placeholder' => 'Pick a location type', + 'required' => false, + 'label' => $options['label'], + 'label_attr' => $options['label_attr'], + 'multiple' => $options['multiple'], + 'attr' => ['class' => 'select2'], + ]); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver + ->setDefault('label', 'Location type') + ->setDefault('label_attr', []) + ->setDefault('multiple', false) + ->setAllowedTypes('multiple', ['bool']) + ; + } + + public function getBlockPrefix() + { + return 'select2_location_type_type'; + } +} diff --git a/src/Bundle/ChillMainBundle/translations/messages.fr.yml b/src/Bundle/ChillMainBundle/translations/messages.fr.yml index 1e95e32d0..76dc27b39 100644 --- a/src/Bundle/ChillMainBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillMainBundle/translations/messages.fr.yml @@ -227,6 +227,7 @@ never: jamais Create a new location: Créer une nouvelle localisation Location list: Liste des localisations Location type: Type de localisation +Pick a location type: Choisir un type de localisation Phonenumber1: Numéro de téléphone Phonenumber2: Autre numéro de téléphone Location configuration: Configuration des localisations From 631111b0c76feeb168f6763fd6966b30c5ec2163 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 10 Oct 2022 16:08:23 +0200 Subject: [PATCH 4/4] rename UserCurrentLocationType to more generic Select2UserLocationType NB: used 2 times, check that all works --- src/Bundle/ChillMainBundle/Controller/UserController.php | 4 ++-- .../Select2UserLocationType.php} | 6 +++--- src/Bundle/ChillMainBundle/config/services/form.yaml | 2 +- .../AdministrativeLocationFilter.php | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) rename src/Bundle/ChillMainBundle/Form/{UserCurrentLocationType.php => Type/Select2UserLocationType.php} (94%) diff --git a/src/Bundle/ChillMainBundle/Controller/UserController.php b/src/Bundle/ChillMainBundle/Controller/UserController.php index 9d3941411..03d8d2692 100644 --- a/src/Bundle/ChillMainBundle/Controller/UserController.php +++ b/src/Bundle/ChillMainBundle/Controller/UserController.php @@ -15,7 +15,7 @@ use Chill\MainBundle\CRUD\Controller\CRUDController; use Chill\MainBundle\Entity\GroupCenter; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Form\Type\ComposedGroupCenterType; -use Chill\MainBundle\Form\UserCurrentLocationType; +use Chill\MainBundle\Form\Type\Select2UserLocationType; use Chill\MainBundle\Form\UserPasswordType; use Chill\MainBundle\Form\UserType; use Chill\MainBundle\Pagination\PaginatorInterface; @@ -234,7 +234,7 @@ class UserController extends CRUDController public function editCurrentLocationAction(Request $request) { $user = $this->getUser(); - $form = $this->createForm(UserCurrentLocationType::class, $user) + $form = $this->createForm(Select2UserLocationType::class, $user) ->add('submit', SubmitType::class, ['label' => 'Save']) ->handleRequest($request); diff --git a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php b/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php similarity index 94% rename from src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php rename to src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php index a99811bfa..e09b63bac 100644 --- a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php @@ -9,7 +9,7 @@ declare(strict_types=1); * the LICENSE file that was distributed with this source code. */ -namespace Chill\MainBundle\Form; +namespace Chill\MainBundle\Form\Type; use Chill\MainBundle\Entity\Location; use Chill\MainBundle\Repository\LocationRepository; @@ -21,7 +21,7 @@ use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; -class UserCurrentLocationType extends AbstractType +class Select2UserLocationType extends AbstractType { private LocationRepository $locationRepository; @@ -69,6 +69,6 @@ class UserCurrentLocationType extends AbstractType public function getBlockPrefix() { - return 'user_current_location_type'; + return 'select2_user_location_type'; } } diff --git a/src/Bundle/ChillMainBundle/config/services/form.yaml b/src/Bundle/ChillMainBundle/config/services/form.yaml index 0a757a8db..f047e8f39 100644 --- a/src/Bundle/ChillMainBundle/config/services/form.yaml +++ b/src/Bundle/ChillMainBundle/config/services/form.yaml @@ -132,7 +132,7 @@ services: autowire: true autoconfigure: true - Chill\MainBundle\Form\UserCurrentLocationType: + Chill\MainBundle\Form\Type\Select2UserLocationType: autowire: true autoconfigure: true diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php index b10a66944..9e54a2272 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php @@ -12,7 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; use Chill\MainBundle\Export\FilterInterface; -use Chill\MainBundle\Form\UserCurrentLocationType; +use Chill\MainBundle\Form\Type\Select2UserLocationType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Export\Declarations; use Doctrine\ORM\QueryBuilder; @@ -50,7 +50,7 @@ class AdministrativeLocationFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_locations', UserCurrentLocationType::class, [ + $builder->add('accepted_locations', Select2UserLocationType::class, [ 'label' => 'Accepted locations', 'label_attr' => [ //'class' => 'd-none'