<?php
namespace App\Form\Type\Frontend;
use App\Repository\SaleRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class VehicleFreeSearchType extends AbstractType
{
public function __construct(private readonly SaleRepository $saleRepository, private readonly SessionInterface $session)
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('freeSearch', TextType::class, [
'required' => false,
'mapped' => false,
'attr' => [
'class' => 'input-texte',
],
])
->add('maxKm', TextType::class, [
'required' => false,
'mapped' => false,
'attr' => [
'class' => 'input-texte',
'placeholder' => 'frontend.search.form.kilometersSearch.max',
],
])
->add('maxPrice', TextType::class, [
'required' => false,
'mapped' => false,
'attr' => [
'class' => 'input-texte',
'placeholder' => 'frontend.form.price-search.label.max',
],
])
->add('sale', ChoiceType::class, [
'required' => false,
'choices' => array_flip($this->getActiveSales($options['user'])),
'placeholder' => 'frontend.home_top.form.sale.placeholder',
'multiple' => false,
'expanded' => false,
])
->add('search', SubmitType::class, [
'label' => 'frontend.home_top.form.submit.label',
'attr' => [
],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'csrf_protection' => false,
'user' => null,
]);
}
public function getBlockPrefix()
{
return 'vp_auto_vehicle_free_search';
}
protected function getActiveSales($user)
{
return $this->saleRepository->getUserActiveChoices($user, $this->session->get('dimension', 'private'));
}
}