<?php
namespace App\Form\Type\Frontend;
use Doctrine\DBAL\Types\Types;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
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\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class VehicleSearchType extends BaseVehicleSearchType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$data = $this->session->get('form_values', []);
foreach (array_keys($data) as $key) {
if (preg_match('/(.+)(Min|Max)/', $key, $matches)) {
$data[$matches[1]][strtolower($matches[2])] = $data[$key];
unset($data[$key]);
}
}
parent::buildForm($builder, $options);
if (isset($data['category']) && !is_array($data['category'])) {
$data['category'] = explode(',', (string) $data['category']);
}
if (isset($data['segmentTransmission']) && !is_array($data['segmentTransmission'])) {
$data['segmentTransmission'] = explode(',', (string) $data['segmentTransmission']);
}
if (isset($data['highlight'])) {
settype($data['highlight'], Types::BOOLEAN);
}
if (isset($data['broken'])) {
settype($data['broken'], Types::BOOLEAN);
}
if (isset($data['seatCount']) && !is_array($data['seatCount'])) {
$data['seatCount'] = explode(',', (string) $data['seatCount']);
}
$isExternalSale = false;
if (array_key_exists('sale', $data)) {
$isExternalSale = $this->externalSaleRepository->idExists($data['sale']);
}
$builder
->add('event', HiddenType::class, [
'attr' => [
'data-field' => 'event',
'data-id' => $data['event'] ?? '',
],
])
->add('category', ChoiceType::class, [
'required' => false,
'choices' => $this->getCategoryChoices(),
'multiple' => true,
'expanded' => true,
'data' => $data['category'] ?? [],
'attr' => [
'data-field' => 'category',
],
])
->add('budget', BudgetType::class, [
'required' => false,
'data' => $data['budget'] ?? [],
])
->add('cityLocation', TextType::class, [
'required' => false,
'label' => 'form.vehicleSearch.cityLocation.label',
'data' => $data['cityLocation'] ?? '',
'attr' => [
'class' => 'cityLocation-input',
'data-field' => 'cityLocation',
'placeholder' => 'frontend.search.form.vehicleSearch.cityLocation.placeholder',
],
])
->add('cityLocationList', ButtonType::class, [
'label' => 'frontend.search.form.vehicleSearch.cityLocation.placeholder',
'attr' => [
'class' => 'popin-choices cityLocation-list hide-info-text',
'data-name' => 'cityLocation',
'data-url' => $this->router->generate('frontend_vehicle_location', ['formType' => $options['form_type']], UrlGeneratorInterface::ABSOLUTE_URL),
],
])
->add('segmentTransmission', ChoiceType::class, [
'required' => false,
'choices' => $this->getSegmentTransmissionChoices(),
'multiple' => true,
'expanded' => true,
'data' => $data['segmentTransmission'] ?? [],
'attr' => [
'data-field' => 'segmentTransmission',
],
])
->add('estimated', CheckboxType::class, [
'required' => false,
'label' => 'frontend.search.form.vehicleSearch.estimated.label',
'mapped' => false,
'data' => isset($data['estimated']) ? (bool) $data['estimated'] : false,
'attr' => [
'data-field' => 'estimated',
],
'disabled' => $isExternalSale,
])
->add('highlight', CheckboxType::class, [
'required' => false,
'label' => 'frontend.search.form.vehicleSearch.highlight.label',
'mapped' => false,
'data' => $data['highlight'] ?? false,
'attr' => [
'data-field' => 'highlight',
],
'disabled' => $isExternalSale,
])
->add('fourwheeldrive', CheckboxType::class, [
'required' => false,
'label' => 'form.vehicleSearch.fourwheeldrive.label',
'mapped' => false,
'data' => isset($data['fourwheeldrive']) ? (bool) $data['fourwheeldrive'] : false,
'attr' => [
'data-field' => 'fourwheeldrive',
],
'disabled' => $isExternalSale,
])
->add('broken', CheckboxType::class, [
'required' => false,
'label' => 'frontend.search.form.vehicleSearch.broken.label',
'mapped' => false,
'data' => $data['broken'] ?? false,
'attr' => [
'data-field' => 'broken',
],
'disabled' => $isExternalSale,
])
->add('kilometers', KilometerSearchType::class, [
'required' => false,
'data' => $data['kilometers'] ?? [],
])
->add('millesime', YearSearchType::class, [
'required' => false,
'data' => $data['year'] ?? [],
])
->add('co2', TextType::class, [
'required' => false,
'label' => 'frontend.search.form.vehicleSearch.co2.label',
'data' => $data['co2'] ?? '',
'attr' => [
'data-field' => 'co2',
'placeholder' => 'frontend.search.form.vehicleSearch.co2.placeholder',
],
])
->add('critair', ChoiceType::class, [
'required' => false,
'multiple' => false,
'expanded' => false,
'choices' => $this->getCritairChoices(['sale' => $data['sale'] ?? null]),
'placeholder' => 'Crit\'Air',
'data' => $data['critair'] ?? '',
'attr' => [
'class' => 'color06',
'data-field' => 'critair',
],
'disabled' => $isExternalSale,
])
->add('seatCount', ChoiceType::class, [
'label' => 'frontend.pdf_view.block.info.places',
'required' => false,
'multiple' => true,
'expanded' => true,
'choices' => self::SEAT_COUNT_CHOICES,
'data' => $data['seatCount'] ?? [],
'attr' => [
'data-field' => 'seatCount',
],
]);
if ($isExternalSale) {
$fieldId = 'optionsList';
// Get options from original field
$optionsListOptions = $builder->get($fieldId)->getOptions();
// Set the “disabled” attribute
$optionsListOptions['disabled'] = true;
$builder
->remove($fieldId)
// Declare the field with attribute “disabled”
->add($fieldId, ButtonType::class, $optionsListOptions)
;
}
}
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,
]);
}
public function getBlockPrefix()
{
return 'vp_auto_vehicle_search';
}
private function getCategoryChoices()
{
return $this->vehicleRepository->getDistinctCategoryForFutureSalesQuery()->execute([], 'choice_type');
}
}