diff --git a/src/Glpi/Controller/Form/Import/Step2PreviewController.php b/src/Glpi/Controller/Form/Import/Step2PreviewController.php index c817c680ce5..ab29a4031f8 100644 --- a/src/Glpi/Controller/Form/Import/Step2PreviewController.php +++ b/src/Glpi/Controller/Form/Import/Step2PreviewController.php @@ -39,6 +39,7 @@ use Glpi\Form\Export\Serializer\FormSerializer; use Glpi\Form\Form; use Session; +use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; @@ -54,10 +55,37 @@ public function __invoke(Request $request): Response } $json = $this->getJsonFormFromRequest($request); - $serializer = new FormSerializer(); - $mapper = new DatabaseMapper(Session::getActiveEntities()); + $replacements = $request->request->all()["replacements"] ?? []; + return $this->previewResponse($json, $replacements); + } + + #[Route("/Form/Import/Remove", name: "glpi_form_import_remove", methods: "POST")] + public function removeForm(Request $request): Response + { + if (!Form::canCreate()) { + throw new AccessDeniedHttpException(); + } + + $json = $this->getJsonFormFromRequest($request); + $form_name = $request->request->get('remove_form_name'); $replacements = $request->request->all()["replacements"] ?? []; + + $serializer = new FormSerializer(); + $json = $serializer->removeFormFromJson($json, $form_name); + + // If all forms have been removed, redirect to the first step + if (!$serializer->isFormsInJson($json)) { + return new RedirectResponse('/Form/Import'); + } + + return $this->previewResponse($json, $replacements); + } + + private function previewResponse(string $json, array $replacements): Response + { + $serializer = new FormSerializer(); + $mapper = new DatabaseMapper(Session::getActiveEntities()); foreach ($replacements as $itemtype => $replacements_for_itemtype) { foreach ($replacements_for_itemtype as $original_name => $items_id) { $mapper->addMappedItem($itemtype, $original_name, $items_id); diff --git a/src/Glpi/Form/Export/Serializer/FormSerializer.php b/src/Glpi/Form/Export/Serializer/FormSerializer.php index 81b3ffb8d9f..697d34bf971 100644 --- a/src/Glpi/Form/Export/Serializer/FormSerializer.php +++ b/src/Glpi/Form/Export/Serializer/FormSerializer.php @@ -61,6 +61,19 @@ public function getVersion(): int return 1; } + public function isFormsInJson(string $json): bool + { + $export_specification = $this->deserialize($json); + + // Validate version + if ($export_specification->version !== $this->getVersion()) { + throw new InvalidArgumentException("Unsupported version"); + } + + // Check if the forms list is empty + return !empty($export_specification->forms); + } + /** @param array