src/Form/Type/Frontend/VehicleSearchType.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type\Frontend;
  3. use Doctrine\DBAL\Types\Types;
  4. use Symfony\Component\Form\Extension\Core\Type\ButtonType;
  5. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  6. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  7. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\Form\FormView;
  12. use Symfony\Component\OptionsResolver\OptionsResolver;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. class VehicleSearchType extends BaseVehicleSearchType
  15. {
  16.     public function buildForm(FormBuilderInterface $builder, array $options): void
  17.     {
  18.         $data $this->session->get('form_values', []);
  19.         foreach (array_keys($data) as $key) {
  20.             if (preg_match('/(.+)(Min|Max)/'$key$matches)) {
  21.                 $data[$matches[1]][strtolower($matches[2])] = $data[$key];
  22.                 unset($data[$key]);
  23.             }
  24.         }
  25.         parent::buildForm($builder$options);
  26.         if (isset($data['category']) && !is_array($data['category'])) {
  27.             $data['category'] = explode(',', (string) $data['category']);
  28.         }
  29.         if (isset($data['segmentTransmission']) && !is_array($data['segmentTransmission'])) {
  30.             $data['segmentTransmission'] = explode(',', (string) $data['segmentTransmission']);
  31.         }
  32.         if (isset($data['highlight'])) {
  33.             settype($data['highlight'], Types::BOOLEAN);
  34.         }
  35.         if (isset($data['broken'])) {
  36.             settype($data['broken'], Types::BOOLEAN);
  37.         }
  38.         if (isset($data['seatCount']) && !is_array($data['seatCount'])) {
  39.             $data['seatCount'] = explode(',', (string) $data['seatCount']);
  40.         }
  41.         $isExternalSale false;
  42.         if (array_key_exists('sale'$data)) {
  43.             $isExternalSale $this->externalSaleRepository->idExists($data['sale']);
  44.         }
  45.         $builder
  46.             ->add('event'HiddenType::class, [
  47.                 'attr' => [
  48.                     'data-field' => 'event',
  49.                     'data-id' => $data['event'] ?? '',
  50.                 ],
  51.             ])
  52.             ->add('category'ChoiceType::class, [
  53.                 'required' => false,
  54.                 'choices' => $this->getCategoryChoices(),
  55.                 'multiple' => true,
  56.                 'expanded' => true,
  57.                 'data' => $data['category'] ?? [],
  58.                 'attr' => [
  59.                     'data-field' => 'category',
  60.                 ],
  61.             ])
  62.             ->add('budget'BudgetType::class, [
  63.                 'required' => false,
  64.                 'data' => $data['budget'] ?? [],
  65.             ])
  66.             ->add('cityLocation'TextType::class, [
  67.                 'required' => false,
  68.                 'label' => 'form.vehicleSearch.cityLocation.label',
  69.                 'data' => $data['cityLocation'] ?? '',
  70.                 'attr' => [
  71.                     'class' => 'cityLocation-input',
  72.                     'data-field' => 'cityLocation',
  73.                     'placeholder' => 'frontend.search.form.vehicleSearch.cityLocation.placeholder',
  74.                 ],
  75.             ])
  76.             ->add('cityLocationList'ButtonType::class, [
  77.                 'label' => 'frontend.search.form.vehicleSearch.cityLocation.placeholder',
  78.                 'attr' => [
  79.                     'class' => 'popin-choices cityLocation-list hide-info-text',
  80.                     'data-name' => 'cityLocation',
  81.                     'data-url' => $this->router->generate('frontend_vehicle_location', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
  82.                 ],
  83.             ])
  84.             ->add('segmentTransmission'ChoiceType::class, [
  85.                 'required' => false,
  86.                 'choices' => $this->getSegmentTransmissionChoices(),
  87.                 'multiple' => true,
  88.                 'expanded' => true,
  89.                 'data' => $data['segmentTransmission'] ?? [],
  90.                 'attr' => [
  91.                     'data-field' => 'segmentTransmission',
  92.                 ],
  93.             ])
  94.             ->add('estimated'CheckboxType::class, [
  95.                 'required' => false,
  96.                 'label' => 'frontend.search.form.vehicleSearch.estimated.label',
  97.                 'mapped' => false,
  98.                 'data' => isset($data['estimated']) ? (bool) $data['estimated'] : false,
  99.                 'attr' => [
  100.                     'data-field' => 'estimated',
  101.                 ],
  102.                 'disabled' => $isExternalSale,
  103.             ])
  104.             ->add('highlight'CheckboxType::class, [
  105.                 'required' => false,
  106.                 'label' => 'frontend.search.form.vehicleSearch.highlight.label',
  107.                 'mapped' => false,
  108.                 'data' => $data['highlight'] ?? false,
  109.                 'attr' => [
  110.                     'data-field' => 'highlight',
  111.                 ],
  112.                 'disabled' => $isExternalSale,
  113.             ])
  114.             ->add('fourwheeldrive'CheckboxType::class, [
  115.                 'required' => false,
  116.                 'label' => 'form.vehicleSearch.fourwheeldrive.label',
  117.                 'mapped' => false,
  118.                 'data' => isset($data['fourwheeldrive']) ? (bool) $data['fourwheeldrive'] : false,
  119.                 'attr' => [
  120.                     'data-field' => 'fourwheeldrive',
  121.                 ],
  122.                 'disabled' => $isExternalSale,
  123.             ])
  124.             ->add('broken'CheckboxType::class, [
  125.                 'required' => false,
  126.                 'label' => 'frontend.search.form.vehicleSearch.broken.label',
  127.                 'mapped' => false,
  128.                 'data' => $data['broken'] ?? false,
  129.                 'attr' => [
  130.                     'data-field' => 'broken',
  131.                 ],
  132.                 'disabled' => $isExternalSale,
  133.             ])
  134.             ->add('kilometers'KilometerSearchType::class, [
  135.                 'required' => false,
  136.                 'data' => $data['kilometers'] ?? [],
  137.             ])
  138.             ->add('millesime'YearSearchType::class, [
  139.                 'required' => false,
  140.                 'data' => $data['year'] ?? [],
  141.             ])
  142.             ->add('co2'TextType::class, [
  143.                 'required' => false,
  144.                 'label' => 'frontend.search.form.vehicleSearch.co2.label',
  145.                 'data' => $data['co2'] ?? '',
  146.                 'attr' => [
  147.                     'data-field' => 'co2',
  148.                     'placeholder' => 'frontend.search.form.vehicleSearch.co2.placeholder',
  149.                 ],
  150.             ])
  151.             ->add('critair'ChoiceType::class, [
  152.                 'required' => false,
  153.                 'multiple' => false,
  154.                 'expanded' => false,
  155.                 'choices' => $this->getCritairChoices(['sale' => $data['sale'] ?? null]),
  156.                 'placeholder' => 'Crit\'Air',
  157.                 'data' => $data['critair'] ?? '',
  158.                 'attr' => [
  159.                     'class' => 'color06',
  160.                     'data-field' => 'critair',
  161.                 ],
  162.                 'disabled' => $isExternalSale,
  163.             ])
  164.             ->add('seatCount'ChoiceType::class, [
  165.                 'label' => 'frontend.pdf_view.block.info.places',
  166.                 'required' => false,
  167.                 'multiple' => true,
  168.                 'expanded' => true,
  169.                 'choices' => self::SEAT_COUNT_CHOICES,
  170.                 'data' => $data['seatCount'] ?? [],
  171.                 'attr' => [
  172.                     'data-field' => 'seatCount',
  173.                 ],
  174.             ]);
  175.         if ($isExternalSale) {
  176.             $fieldId 'optionsList';
  177.             // Get options from original field
  178.             $optionsListOptions $builder->get($fieldId)->getOptions();
  179.             // Set the “disabled” attribute
  180.             $optionsListOptions['disabled'] = true;
  181.             $builder
  182.                 ->remove($fieldId)
  183.                 // Declare the field with attribute “disabled”
  184.                 ->add($fieldIdButtonType::class, $optionsListOptions)
  185.             ;
  186.         }
  187.     }
  188.     public function finishView(FormView $viewFormInterface $form, array $options): void
  189.     {
  190.         foreach ($view->children as $child) {
  191.             if ('critair' == $child->vars['name']) {
  192.                 foreach ($child->vars['choices'] as $choice) {
  193.                     $choice->label 'Crit\'Air '.$choice->label;
  194.                 }
  195.             }
  196.         }
  197.     }
  198.     public function configureOptions(OptionsResolver $resolver): void
  199.     {
  200.         $resolver->setDefaults([
  201.             'csrf_protection' => false,
  202.             'sale_field_type' => HiddenType::class,
  203.             'form_type' => self::FORM_DEFAULT,
  204.         ]);
  205.     }
  206.     public function getBlockPrefix()
  207.     {
  208.         return 'vp_auto_vehicle_search';
  209.     }
  210.     private function getCategoryChoices()
  211.     {
  212.         return $this->vehicleRepository->getDistinctCategoryForFutureSalesQuery()->execute([], 'choice_type');
  213.     }
  214. }