issue649: adapt existing UserCurrentLocationType to use select2 with AdministrativeLocationFilter

This commit is contained in:
Mathieu Jaumotte 2022-10-10 15:25:07 +02:00
parent 01acfeb58f
commit 999d4e2038
2 changed files with 52 additions and 19 deletions

View File

@ -17,6 +17,9 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UserCurrentLocationType extends AbstractType class UserCurrentLocationType extends AbstractType
{ {
@ -32,6 +35,7 @@ class UserCurrentLocationType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
//dump($options);
$builder $builder
->add('currentLocation', EntityType::class, [ ->add('currentLocation', EntityType::class, [
'class' => Location::class, 'class' => Location::class,
@ -43,7 +47,29 @@ class UserCurrentLocationType extends AbstractType
}, },
'placeholder' => 'Pick a location', 'placeholder' => 'Pick a location',
'required' => false, 'required' => false,
'label' => $options['label'],
'label_attr' => $options['label_attr'],
'multiple' => $options['multiple'],
'attr' => ['class' => 'select2'], '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';
}
} }

View File

@ -11,22 +11,26 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Entity\Location; //use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\UserCurrentLocationType;
//use Chill\MainBundle\Repository\LocationRepository;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
class AdministrativeLocationFilter implements FilterInterface class AdministrativeLocationFilter implements FilterInterface
{ {
//private LocationRepository $locationRepository;
private TranslatableStringHelper $translatableStringHelper; private TranslatableStringHelper $translatableStringHelper;
public function __construct( public function __construct(
//LocationRepository $locationRepository,
TranslatableStringHelper $translatableStringHelper TranslatableStringHelper $translatableStringHelper
) { ) {
//$this->locationRepository = $locationRepository;
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
} }
@ -37,17 +41,10 @@ class AdministrativeLocationFilter implements FilterInterface
public function alterQuery(QueryBuilder $qb, $data) public function alterQuery(QueryBuilder $qb, $data)
{ {
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('acp.administrativeLocation', ':locations'); $clause = $qb->expr()->in('acp.administrativeLocation', ':locations');
$qb
if ($where instanceof Andx) { ->andWhere($clause)
$where->add($clause); ->setParameter('locations', $data['accepted_locations']);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('locations', $data['accepted_locations']);
} }
public function applyOn(): string public function applyOn(): string
@ -57,13 +54,23 @@ class AdministrativeLocationFilter implements FilterInterface
public function buildForm(FormBuilderInterface $builder) public function buildForm(FormBuilderInterface $builder)
{ {
$builder->add('accepted_locations', EntityType::class, [
'class' => Location::class, //$builder->add('accepted_locations', EntityType::class, [
'choice_label' => function (Location $l) { // 'class' => Location::class,
return $l->getName() . ' (' . $this->translatableStringHelper->localize($l->getLocationType()->getTitle()) . ')'; // '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, 'multiple' => true,
'expanded' => true,
]); ]);
} }