src/Form/Type/Frontend/BaseVehicleSearchType.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type\Frontend;
  3. use App\Repository\ExternalSaleRepository;
  4. use App\Repository\TransmissionRepository;
  5. use App\Repository\VehicleRepository;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\Extension\Core\Type\ButtonType;
  8. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\Form\FormView;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Component\Routing\RouterInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. abstract class BaseVehicleSearchType extends AbstractType
  19. {
  20.     public const FORM_DEFAULT 0;
  21.     public const FORM_SALE_RESULTS 1;
  22.     public const FORM_ALERT_RESULTS 2;
  23.     public const SEAT_COUNT_CHOICES = [
  24.         'frontend.search.form.seatCount.choices.choice1' => '2-2',
  25.         'frontend.search.form.seatCount.choices.choice2' => '5-5',
  26.         'frontend.search.form.seatCount.choices.choice3' => '6-99',
  27.     ];
  28.     public const CO2_CHOICES = [
  29.         'frontend.search.form.co2.choices.choice1' => null,
  30.         'frontend.search.form.co2.choices.choice2' => '0-100',
  31.         'frontend.search.form.co2.choices.choice3' => '100-150',
  32.         'frontend.search.form.co2.choices.choice4' => '150-180',
  33.     ];
  34.     public const EURO_EMISSION_STANDARD_CHOICES = [
  35.         'frontend.search.form.euroEmissionStandard.choices.choice1' => '0-4',
  36.         'frontend.search.form.euroEmissionStandard.choices.choice2' => '5',
  37.         'frontend.search.form.euroEmissionStandard.choices.choice3' => '6',
  38.     ];
  39.     public const BOOLEAN_CHOICES = [
  40.         'frontend.search.form.co2.choices.choice1' => null,
  41.         'frontend.vehicle.information.doubleclefs.yes' => true,
  42.         'frontend.page.register_pro.form.manager.choices.non' => false,
  43.     ];
  44.     public const SEGMENT_TRANSMISSION_CHOICES = [
  45.         'Automatique' => 'frontend.search.form.segmentTransmission.choices.choice1',
  46.         'Manuelle' => 'frontend.search.form.segmentTransmission.choices.choice2',
  47.     ];
  48.     protected $externalSaleRepository;
  49.     protected $vehicleRepository;
  50.     protected $router;
  51.     protected $session;
  52.     protected $tokenStorage;
  53.     protected $transmissionRepository;
  54.     public function __construct(VehicleRepository $vehicleRepositoryTransmissionRepository $transmissionRepositoryExternalSaleRepository $externalSaleRepositoryRouterInterface $routerSessionInterface $sessionTokenStorageInterface $tokenStorage)
  55.     {
  56.         $this->vehicleRepository $vehicleRepository;
  57.         $this->externalSaleRepository $externalSaleRepository;
  58.         $this->transmissionRepository $transmissionRepository;
  59.         $this->router $router;
  60.         $this->session $session;
  61.         $this->tokenStorage $tokenStorage;
  62.     }
  63.     public function buildForm(FormBuilderInterface $builder, array $options): void
  64.     {
  65.         $data $this->session->get('form_values', []);
  66.         if (isset($data['seatCount']) && !is_array($data['seatCount'])) {
  67.             $data['seatCount'] = explode(',', (string) $data['seatCount']);
  68.         }
  69.         $builder
  70.             ->add('maker'TextType::class, [
  71.                 'required' => false,
  72.                 'label' => 'form.vehicleSearch.brand.label',
  73.                 'attr' => [
  74.                     'class' => 'maker-input',
  75.                     'data-field' => 'maker',
  76.                     'placeholder' => 'frontend.search.form.vehicleSearch.brand.placeholder',
  77.                 ],
  78.             ])
  79.             ->add('makerList'ButtonType::class, [
  80.                 'label' => 'frontend.search.form.vehicleSearch.brand.placeholder',
  81.                 'attr' => [
  82.                     'class' => 'popin-choices maker-list',
  83.                     'data-name' => 'maker',
  84.                     'data-url' => $this->router->generate('frontend_vehicle_maker', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
  85.                 ],
  86.             ])
  87.             ->add('modelGroup'TextType::class, [
  88.                 'required' => false,
  89.                 'label' => 'form.vehicleSearch.group.label',
  90.                 'disabled' => true,
  91.                 'attr' => [
  92.                     'class' => 'group-input disabled-white',
  93.                     'data-field' => 'modelGroup',
  94.                     'placeholder' => 'frontend.search.form.vehicleSearch.group.placeholder',
  95.                 ],
  96.             ])
  97.             ->add('groupList'ButtonType::class, [
  98.                 'label' => 'frontend.search.form.vehicleSearch.group.placeholder',
  99.                 'attr' => [
  100.                     'class' => 'popin-choices group-list',
  101.                     'data-name' => 'group',
  102.                     'data-url' => $this->router->generate('frontend_vehicle_model', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
  103.                 ],
  104.             ])
  105.             ->add('energy'TextType::class, [
  106.                 'required' => false,
  107.                 'label' => 'frontend.search.form.vehicleSearch.energy.label',
  108.                 'attr' => [
  109.                     'class' => 'energy-input disabled-white',
  110.                     'data-field' => 'energy',
  111.                     'placeholder' => 'frontend.search.form.vehicleSearch.energy.placeholder',
  112.                 ],
  113.             ])
  114.             ->add('energyList'ButtonType::class, [
  115.                 'label' => 'frontend.search.form.vehicleSearch.energy.label',
  116.                 'attr' => [
  117.                     'class' => 'popin-choices energy-list',
  118.                     'data-name' => 'energy',
  119.                     'data-url' => $this->router->generate('frontend_vehicle_energy', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
  120.                 ],
  121.             ])
  122.             ->add('segmentSize'TextType::class, [
  123.                 'required' => false,
  124.                 'label' => 'frontend.search.form.vehicleSearch.segmenttaille.label',
  125.                 'attr' => [
  126.                     'class' => 'segmenttaille-input disabled-white',
  127.                     'data-field' => 'segmentSize',
  128.                     'placeholder' => 'frontend.search.form.vehicleSearch.segmenttaille.placeholder',
  129.                 ],
  130.             ])
  131.             ->add('segmenttailleList'ButtonType::class, [
  132.                 'label' => 'frontend.search.form.vehicleSearch.segmenttaille.label',
  133.                 'attr' => [
  134.                     'class' => 'popin-choices segmenttaille-list',
  135.                     'data-name' => 'segmenttaille',
  136.                     'data-url' => $this->router->generate('frontend_vehicle_segmenttailles', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
  137.                 ],
  138.             ])
  139.             ->add('options'TextType::class, [
  140.                 'required' => false,
  141.                 'label' => 'frontend.search.form.vehicleSearch.options.label',
  142.                 'disabled' => true,
  143.                 'data' => $data['options'] ?? '',
  144.                 'attr' => [
  145.                     'class' => 'options-input disabled-white',
  146.                     'data-field' => 'options',
  147.                     'placeholder' => 'frontend.search.form.vehicleSearch.options.placeholder',
  148.                 ],
  149.             ])
  150.             ->add('optionsList'ButtonType::class, [
  151.                 'label' => 'frontend.search.form.vehicleSearch.options.label',
  152.                 'attr' => [
  153.                     'class' => 'popin-choices options-list',
  154.                     'data-name' => 'options',
  155.                     'data-url' => $this->router->generate('frontend_vehicle_options', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
  156.                 ],
  157.             ]);
  158.     }
  159.     public function finishView(FormView $viewFormInterface $form, array $options): void
  160.     {
  161.         foreach ($view->children as $child) {
  162.             if ('critair' == $child->vars['name']) {
  163.                 foreach ($child->vars['choices'] as $choice) {
  164.                     $choice->label 'Crit\'Air '.$choice->label;
  165.                 }
  166.             }
  167.         }
  168.     }
  169.     public function configureOptions(OptionsResolver $resolver): void
  170.     {
  171.         $resolver->setDefaults([
  172.             'csrf_protection' => false,
  173.             'sale_field_type' => HiddenType::class,
  174.             'form_type' => self::FORM_DEFAULT,
  175.         ]);
  176.     }
  177.     protected function getSegmentTransmissionChoices()
  178.     {
  179.         $choices $this->transmissionRepository->getDistinctTransmissionQuery()->execute([], 'choice_type');
  180.         foreach ($choices as $key => $value) {
  181.             if (isset(static::SEGMENT_TRANSMISSION_CHOICES[$key])) {
  182.                 $choices[static::SEGMENT_TRANSMISSION_CHOICES[$key]] = $value;
  183.             }
  184.             unset($choices[$key]);
  185.         }
  186.         return $choices;
  187.     }
  188.     protected function getCritairChoices($data)
  189.     {
  190.         return $this->vehicleRepository->getDistinctCritairForFutureSalesQuery($data)->execute([], 'choice_type');
  191.     }
  192. }