Merge branch '649_select2_forms' into 111_exports_suite

This commit is contained in:
Mathieu Jaumotte 2022-10-10 16:14:09 +02:00
commit 5fd4e322d6
7 changed files with 96 additions and 32 deletions

View File

@ -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,
]);
}

View File

@ -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);

View File

@ -0,0 +1,51 @@
<?php
namespace Chill\MainBundle\Form\Type;
use Chill\MainBundle\Entity\LocationType;
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\OptionsResolver\OptionsResolver;
class Select2LocationTypeType extends AbstractType
{
private TranslatableStringHelper $translatableStringHelper;
public function __construct(TranslatableStringHelper $translatableStringHelper)
{
$this->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';
}
}

View File

@ -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;
@ -17,8 +17,11 @@ 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
class Select2UserLocationType extends AbstractType
{
private LocationRepository $locationRepository;
@ -43,7 +46,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 'select2_user_location_type';
}
}

View File

@ -132,7 +132,7 @@ services:
autowire: true
autoconfigure: true
Chill\MainBundle\Form\UserCurrentLocationType:
Chill\MainBundle\Form\Type\Select2UserLocationType:
autowire: true
autoconfigure: true

View File

@ -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

View File

@ -11,17 +11,16 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\Select2UserLocationType;
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 TranslatableStringHelper $translatableStringHelper;
public function __construct(
@ -37,17 +36,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 +49,13 @@ 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', Select2UserLocationType::class, [
'label' => 'Accepted locations',
'label_attr' => [
//'class' => 'd-none'
],
'multiple' => true,
'expanded' => true,
]);
}