From b73c4f3d4d7d7be93c95525b7bcfdd6d6f1593a0 Mon Sep 17 00:00:00 2001 From: mmarchois Date: Wed, 18 Dec 2024 08:54:24 +0100 Subject: [PATCH] =?UTF-8?q?Explo:=20am=C3=A9lioration=20des=20perfs=20Upda?= =?UTF-8?q?teMeasureController?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/packages/framework.yaml | 2 + .../Command/Location/SaveLocationCommand.php | 5 + .../Form/Regulation/LocationFormType.php | 144 ++++++++++++------ 3 files changed, 104 insertions(+), 47 deletions(-) diff --git a/config/packages/framework.yaml b/config/packages/framework.yaml index d764c55ea..c7c6c6023 100644 --- a/config/packages/framework.yaml +++ b/config/packages/framework.yaml @@ -18,6 +18,8 @@ framework: #fragments: true php_errors: log: true + validation: + cache: validator.mapping.cache.adapter http_client: scoped_clients: diff --git a/src/Application/Regulation/Command/Location/SaveLocationCommand.php b/src/Application/Regulation/Command/Location/SaveLocationCommand.php index 0da7b714f..39589f2cf 100644 --- a/src/Application/Regulation/Command/Location/SaveLocationCommand.php +++ b/src/Application/Regulation/Command/Location/SaveLocationCommand.php @@ -115,4 +115,9 @@ public function getRoadDeleteCommand(): ?CommandInterface return null; } + + public function getRoadType(): string + { + return $this->roadType; + } } diff --git a/src/Infrastructure/Form/Regulation/LocationFormType.php b/src/Infrastructure/Form/Regulation/LocationFormType.php index b35a7fbab..e5a9b42e5 100644 --- a/src/Infrastructure/Form/Regulation/LocationFormType.php +++ b/src/Infrastructure/Form/Regulation/LocationFormType.php @@ -12,97 +12,147 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormInterface; use Symfony\Component\OptionsResolver\OptionsResolver; final class LocationFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { + // Ajout du champ roadType qui contrôle l'affichage des autres formulaires + $builder->add('roadType', ChoiceType::class, [ + 'choices' => $this->getRoadTypeChoices($options['permissions']), + 'label' => 'regulation.location.type', + 'label_attr' => [ + 'class' => 'required', + ], + ]); + + // Ajout de tous les sous-formulaires avec mapped => false par défaut + $this->addAllSubForms($builder, $options); + + // Gestion des événements PRE_SET_DATA et PRE_SUBMIT + $this->addFormEventListeners($builder, $options); + } + + private function addAllSubForms(FormBuilderInterface $builder, array $options): void + { + // Ajout des formulaires de routes numérotées $builder - ->add( - 'roadType', - ChoiceType::class, - options: $this->getRoadTypeOptions(), - ) - ->add(RoadTypeEnum::DEPARTMENTAL_ROAD->value, NumberedRoadFormType::class, [ - 'roadType' => RoadTypeEnum::DEPARTMENTAL_ROAD->value, - 'administrators' => $options['administrators'][RoadTypeEnum::DEPARTMENTAL_ROAD->value], + ->add('departmentalRoad', NumberedRoadFormType::class, [ + 'mapped' => false, 'label' => false, + 'roadType' => RoadTypeEnum::DEPARTMENTAL_ROAD->value, + 'administrators' => $options['administrators'][RoadTypeEnum::DEPARTMENTAL_ROAD->value] ?? [], ]) - ->add(RoadTypeEnum::NATIONAL_ROAD->value, NumberedRoadFormType::class, [ - 'roadType' => RoadTypeEnum::NATIONAL_ROAD->value, - 'administrators' => $options['administrators'][RoadTypeEnum::NATIONAL_ROAD->value], + ->add('nationalRoad', NumberedRoadFormType::class, [ + 'mapped' => false, 'label' => false, + 'roadType' => RoadTypeEnum::NATIONAL_ROAD->value, + 'administrators' => $options['administrators'][RoadTypeEnum::NATIONAL_ROAD->value] ?? [], ]) ->add('namedStreet', NamedStreetFormType::class, [ + 'mapped' => false, 'label' => false, ]) ->add('rawGeoJSON', RawGeoJSONFormType::class, [ + 'mapped' => false, 'label' => false, - ]) - ; + ]); + } - $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) { + private function addFormEventListeners(FormBuilderInterface $builder, array $options): void + { + // PRE_SET_DATA pour le chargement initial + $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void { $form = $event->getForm(); - $command = $event->getData(); - - $isRawGeoJSON = $command?->roadType === RoadTypeEnum::RAW_GEOJSON->value; - $canUseRawGeoJSON = \in_array(CanUseRawGeoJSON::PERMISSION_NAME, $options['permissions']); - - if ($isRawGeoJSON || $canUseRawGeoJSON) { - // Replace field with new options - $form->add( - 'roadType', - ChoiceType::class, - options: $this->getRoadTypeOptions( - includeRawGeoJSONOption: true, - ), - ); + $data = $event->getData(); + + if (!$data) { + return; } + + $this->updateFormMappingBasedOnRoadType($form, $data->getRoadType()); + }); + + // PRE_SUBMIT pour la soumission du formulaire + $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void { + $form = $event->getForm(); + $data = $event->getData(); + + if (!isset($data['roadType'])) { + return; + } + + $this->updateFormMappingBasedOnRoadType($form, $data['roadType']); }); } - private function getRoadTypeOptions(bool $includeRawGeoJSONOption = false): array + private function updateFormMappingBasedOnRoadType(FormInterface $form, string $roadType): void + { + // Réinitialiser tous les mapped à false + foreach (['departmentalRoad', 'nationalRoad', 'namedStreet', 'rawGeoJSON'] as $field) { + if ($form->has($field)) { + $config = $form->get($field)->getConfig(); + $form->add($field, \get_class($config->getType()->getInnerType()), [ + 'mapped' => false, + 'label' => false, + ] + $config->getOptions()); + } + } + + // Activer le mapping uniquement pour le formulaire correspondant au type de route + $fieldMap = [ + RoadTypeEnum::DEPARTMENTAL_ROAD->value => 'departmentalRoad', + RoadTypeEnum::NATIONAL_ROAD->value => 'nationalRoad', + 'lane' => 'namedStreet', + RoadTypeEnum::RAW_GEOJSON->value => 'rawGeoJSON', + ]; + + if (isset($fieldMap[$roadType]) && $form->has($fieldMap[$roadType])) { + $field = $fieldMap[$roadType]; + $config = $form->get($field)->getConfig(); + $form->add($field, \get_class($config->getType()->getInnerType()), [ + 'mapped' => true, + 'label' => false, + ] + $config->getOptions()); + } + } + + private function getRoadTypeChoices(array $permissions): array { $choices = []; - $choiceAttr = []; + $choices['regulation.location.type.placeholder'] = ''; foreach (RoadTypeEnum::cases() as $case) { $label = \sprintf('regulation.location.road.type.%s', $case->value); - if ($case->value === RoadTypeEnum::RAW_GEOJSON->value && !$includeRawGeoJSONOption) { - $choiceAttr[$label] = [ - 'hidden' => '', - 'disabled' => 'disabled', // For Safari (it does not support