<?php
namespace App\Form;
use App\Entity\Gibier;
use App\Repository\AnimalRepository;
use App\Services\Utils;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use App\Entity\HuntingMode;
use App\Entity\Weapon;
use App\Entity\Animal;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SearchFormType extends AbstractType
{
public function __construct(private Utils $utils)
{}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->setMethod('GET')
->add('region',ChoiceType::class,[
'mapped' => true,
'expanded' => true,
'multiple' => true,
'placeholder' => 'Régions',
'choice_loader' => new CallbackChoiceLoader(function() {
return $this->utils->getRegionsToChoices();
}),
'required' => false,
])
->add('huntingMode', EntityType::class, array(
'class' => HuntingMode::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.title', 'ASC');
},
'choice_label' => 'title',
'mapped' => true,
'expanded' => true,
'multiple' => true,
'required' => false,
))
->add('animals', EntityType::class, array(
'class' => Animal::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('a')
->orderBy('a.name', 'ASC');
},
'choice_label' => 'name',
'mapped' => true,
'expanded' => true,
'multiple' => true,
'required' => false,
))
->add('dates',TextType::class,[
'required' => false,
'empty_data' => null,
'mapped' => true,
'attr' => [
'class' => 'search_form_dates'
]
])
->add('departement',ChoiceType::class,[
'mapped' => true,
'expanded' => true,
'multiple' => true,
'placeholder' => 'Département',
'choice_loader' => new CallbackChoiceLoader(function() {
return $this->utils->getListToChocies();
}),
'required' => false,
])
->add('weapons', EntityType::class, array(
'class' => Weapon::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('a')
->orderBy('a.name', 'ASC');
},
'choice_label' => 'name',
'mapped' => true,
'expanded' => true,
'multiple' => true,
'required' => false,
))
->add('priceMin',MoneyType::class,[
'required' => false,
'attr' => [
'class' => 'price',
'placeholder' => ''
]
])
->add('priceMax',MoneyType::class,[
'required' => false,
'attr' => [
'class' => 'price',
'placeholder' => ''
]
])
->add('type',ChoiceType::class,[
'expanded' => true,
'multiple' => true,
'placeholder' => 'Type d\'offre',
'choices' => [
'Action' => 'ac',
'Bracelet' => 'br',
'Journée' => 'jr',
],
'required' => false,
])
->add('gibier', EntityType::class, array(
'class' => Gibier::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('a')
->orderBy('a.title', 'ASC');
},
'choice_label' => 'title',
'placeholder' => 'Type de gibier',
'mapped' => true,
'expanded' => true,
'multiple' => true ,
'required' => false,
))
->add('orderBy', HiddenType::class)
->add('orderSort', HiddenType::class)
->add('save', SubmitType::class, [
'label' => 'Appliquer'
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'isMobile' => false,
]);
}
}