<?php
namespace App\Form\Type\Frontend;
use App\Repository\ExternalSaleRepository;
use App\Repository\TransmissionRepository;
use App\Repository\VehicleRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
abstract class BaseVehicleSearchType extends AbstractType
{
public const FORM_DEFAULT = 0;
public const FORM_SALE_RESULTS = 1;
public const FORM_ALERT_RESULTS = 2;
public const SEAT_COUNT_CHOICES = [
'frontend.search.form.seatCount.choices.choice1' => '2-2',
'frontend.search.form.seatCount.choices.choice2' => '5-5',
'frontend.search.form.seatCount.choices.choice3' => '6-99',
];
public const CO2_CHOICES = [
'frontend.search.form.co2.choices.choice1' => null,
'frontend.search.form.co2.choices.choice2' => '0-100',
'frontend.search.form.co2.choices.choice3' => '100-150',
'frontend.search.form.co2.choices.choice4' => '150-180',
];
public const EURO_EMISSION_STANDARD_CHOICES = [
'frontend.search.form.euroEmissionStandard.choices.choice1' => '0-4',
'frontend.search.form.euroEmissionStandard.choices.choice2' => '5',
'frontend.search.form.euroEmissionStandard.choices.choice3' => '6',
];
public const BOOLEAN_CHOICES = [
'frontend.search.form.co2.choices.choice1' => null,
'frontend.vehicle.information.doubleclefs.yes' => true,
'frontend.page.register_pro.form.manager.choices.non' => false,
];
public const SEGMENT_TRANSMISSION_CHOICES = [
'Automatique' => 'frontend.search.form.segmentTransmission.choices.choice1',
'Manuelle' => 'frontend.search.form.segmentTransmission.choices.choice2',
];
protected $externalSaleRepository;
protected $vehicleRepository;
protected $router;
protected $session;
protected $tokenStorage;
protected $transmissionRepository;
public function __construct(VehicleRepository $vehicleRepository, TransmissionRepository $transmissionRepository, ExternalSaleRepository $externalSaleRepository, RouterInterface $router, SessionInterface $session, TokenStorageInterface $tokenStorage)
{
$this->vehicleRepository = $vehicleRepository;
$this->externalSaleRepository = $externalSaleRepository;
$this->transmissionRepository = $transmissionRepository;
$this->router = $router;
$this->session = $session;
$this->tokenStorage = $tokenStorage;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$data = $this->session->get('form_values', []);
if (isset($data['seatCount']) && !is_array($data['seatCount'])) {
$data['seatCount'] = explode(',', (string) $data['seatCount']);
}
$builder
->add('maker', TextType::class, [
'required' => false,
'label' => 'form.vehicleSearch.brand.label',
'attr' => [
'class' => 'maker-input',
'data-field' => 'maker',
'placeholder' => 'frontend.search.form.vehicleSearch.brand.placeholder',
],
])
->add('makerList', ButtonType::class, [
'label' => 'frontend.search.form.vehicleSearch.brand.placeholder',
'attr' => [
'class' => 'popin-choices maker-list',
'data-name' => 'maker',
'data-url' => $this->router->generate('frontend_vehicle_maker', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
],
])
->add('modelGroup', TextType::class, [
'required' => false,
'label' => 'form.vehicleSearch.group.label',
'disabled' => true,
'attr' => [
'class' => 'group-input disabled-white',
'data-field' => 'modelGroup',
'placeholder' => 'frontend.search.form.vehicleSearch.group.placeholder',
],
])
->add('groupList', ButtonType::class, [
'label' => 'frontend.search.form.vehicleSearch.group.placeholder',
'attr' => [
'class' => 'popin-choices group-list',
'data-name' => 'group',
'data-url' => $this->router->generate('frontend_vehicle_model', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
],
])
->add('energy', TextType::class, [
'required' => false,
'label' => 'frontend.search.form.vehicleSearch.energy.label',
'attr' => [
'class' => 'energy-input disabled-white',
'data-field' => 'energy',
'placeholder' => 'frontend.search.form.vehicleSearch.energy.placeholder',
],
])
->add('energyList', ButtonType::class, [
'label' => 'frontend.search.form.vehicleSearch.energy.label',
'attr' => [
'class' => 'popin-choices energy-list',
'data-name' => 'energy',
'data-url' => $this->router->generate('frontend_vehicle_energy', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
],
])
->add('segmentSize', TextType::class, [
'required' => false,
'label' => 'frontend.search.form.vehicleSearch.segmenttaille.label',
'attr' => [
'class' => 'segmenttaille-input disabled-white',
'data-field' => 'segmentSize',
'placeholder' => 'frontend.search.form.vehicleSearch.segmenttaille.placeholder',
],
])
->add('segmenttailleList', ButtonType::class, [
'label' => 'frontend.search.form.vehicleSearch.segmenttaille.label',
'attr' => [
'class' => 'popin-choices segmenttaille-list',
'data-name' => 'segmenttaille',
'data-url' => $this->router->generate('frontend_vehicle_segmenttailles', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
],
])
->add('options', TextType::class, [
'required' => false,
'label' => 'frontend.search.form.vehicleSearch.options.label',
'disabled' => true,
'data' => $data['options'] ?? '',
'attr' => [
'class' => 'options-input disabled-white',
'data-field' => 'options',
'placeholder' => 'frontend.search.form.vehicleSearch.options.placeholder',
],
])
->add('optionsList', ButtonType::class, [
'label' => 'frontend.search.form.vehicleSearch.options.label',
'attr' => [
'class' => 'popin-choices options-list',
'data-name' => 'options',
'data-url' => $this->router->generate('frontend_vehicle_options', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
],
]);
}
public function finishView(FormView $view, FormInterface $form, array $options): void
{
foreach ($view->children as $child) {
if ('critair' == $child->vars['name']) {
foreach ($child->vars['choices'] as $choice) {
$choice->label = 'Crit\'Air '.$choice->label;
}
}
}
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'csrf_protection' => false,
'sale_field_type' => HiddenType::class,
'form_type' => self::FORM_DEFAULT,
]);
}
protected function getSegmentTransmissionChoices()
{
$choices = $this->transmissionRepository->getDistinctTransmissionQuery()->execute([], 'choice_type');
foreach ($choices as $key => $value) {
if (isset(static::SEGMENT_TRANSMISSION_CHOICES[$key])) {
$choices[static::SEGMENT_TRANSMISSION_CHOICES[$key]] = $value;
}
unset($choices[$key]);
}
return $choices;
}
protected function getCritairChoices($data)
{
return $this->vehicleRepository->getDistinctCritairForFutureSalesQuery($data)->execute([], 'choice_type');
}
}