From 973450110b58ae6c986539c3c5412ff6dee6a64e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 23 Apr 2025 09:11:50 +0200 Subject: [PATCH] Add duplicate and update options for saved exports Introduce functionality to duplicate saved exports and update options directly from export generations. Update translations, controllers, views, and entities to support the new features, providing better flexibility and user experience around saved export management. --- .../Controller/SavedExportController.php | 70 ++++++++++++++++++- .../Entity/ExportGeneration.php | 5 ++ .../views/ExportGeneration/wait.html.twig | 34 +++++++-- .../views/SavedExport/edit.html.twig | 2 +- .../views/SavedExport/index.html.twig | 1 + .../translations/messages.fr.yml | 6 ++ 6 files changed, 110 insertions(+), 8 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Controller/SavedExportController.php b/src/Bundle/ChillMainBundle/Controller/SavedExportController.php index b7e38f7f4..2a07f2ac7 100644 --- a/src/Bundle/ChillMainBundle/Controller/SavedExportController.php +++ b/src/Bundle/ChillMainBundle/Controller/SavedExportController.php @@ -31,6 +31,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; +use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Security\Core\Security; @@ -104,14 +105,50 @@ final readonly class SavedExportController ->setExportAlias($exportGeneration->getExportAlias()) ->setUser($user) ->setOptions($exportGeneration->getOptions()) - ->setTitle($this->translator->trans($this->exportManager->getExport($exportGeneration->getExportAlias())->getTitle())); + ->setTitle( + $request->query->has('title') ? + $request->query->get('title') : + $this->translator->trans($this->exportManager->getExport($exportGeneration->getExportAlias())->getTitle()) + ); + return $this->handleEdit($savedExport, $request); + } + + #[Route(path: '/exports/saved/duplicate-from-saved-export/{id}/new', name: 'chill_main_export_saved_duplicate')] + public function duplicate(SavedExport $previousSavedExport, Request $request): Response + { + if (!$this->security->isGranted(SavedExportVoter::GENERATE, $previousSavedExport)) { + throw new AccessDeniedHttpException("Not allowed to see this saved export"); + } + + $user = $this->security->getUser(); + if (!$user instanceof User) { + throw new AccessDeniedHttpException('only regular user can create a saved export'); + } + + $savedExport = new SavedExport(); + $savedExport + ->setExportAlias($previousSavedExport->getExportAlias()) + ->setUser($user) + ->setOptions($previousSavedExport->getOptions()) + ->setDescription($previousSavedExport->getDescription()) + ->setTitle( + $request->query->has('title') ? + $request->query->get('title') : + $previousSavedExport->getTitle() . ' (' . $this->translator->trans('saved_export.Duplicated') . ' ' . (new \DateTimeImmutable('now'))->format('d-m-Y H:i:s') . ')' + ); + + return $this->handleEdit($savedExport, $request); + + } + + private function handleEdit(SavedExport $savedExport, Request $request): Response + { $form = $this->formFactory->create(SavedExportType::class, $savedExport); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->entityManager->persist($savedExport); - $exportGeneration->setSavedExport($savedExport); $this->entityManager->flush(); if (($session = $request->getSession()) instanceof Session) { @@ -166,6 +203,35 @@ final readonly class SavedExportController ); } + #[Route(path: '/{_locale}/exports/saved/{savedExport}/edit-options/{exportGeneration}', name: 'chill_main_export_saved_options_edit')] + public function updateOptionsFromGeneration(SavedExport $savedExport, ExportGeneration $exportGeneration, Request $request): Response + { + if (!$this->security->isGranted(SavedExportVoter::EDIT, $savedExport)) { + throw new AccessDeniedHttpException("You are not allowed to access this saved export"); + } + + if (!$this->security->isGranted(ExportGenerationVoter::VIEW, $exportGeneration)) { + throw new AccessDeniedHttpException("You are not allowed to access this export generation"); + } + + if ($savedExport->getExportAlias() !== $exportGeneration->getExportAlias()) { + throw new UnprocessableEntityHttpException("export alias does not match"); + } + + $savedExport->setOptions($exportGeneration->getOptions()); + + $this->entityManager->flush(); + + $session = $request->getSession(); + if ($session instanceof Session) { + $session->getFlashBag()->add('success', new TranslatableMessage("saved_export.Options updated successfully")); + } + + return new RedirectResponse( + $this->urlGenerator->generate('chill_main_export_saved_edit', ['id' => $savedExport->getId()]), + ); + } + #[Route(path: '/{_locale}/exports/saved/my', name: 'chill_main_export_saved_list_my')] public function list(): Response { diff --git a/src/Bundle/ChillMainBundle/Entity/ExportGeneration.php b/src/Bundle/ChillMainBundle/Entity/ExportGeneration.php index de9be0e1c..ede2943bd 100644 --- a/src/Bundle/ChillMainBundle/Entity/ExportGeneration.php +++ b/src/Bundle/ChillMainBundle/Entity/ExportGeneration.php @@ -113,6 +113,11 @@ class ExportGeneration implements TrackCreationInterface return $this; } + public function isLinkedToSavedExport(): bool + { + return null !== $this->savedExport; + } + public static function fromSavedExport(SavedExport $savedExport, ?\DateTimeImmutable $deletedAt = null): self { $generation = new self($savedExport->getExportAlias(), $savedExport->getOptions(), $deletedAt, $savedExport); diff --git a/src/Bundle/ChillMainBundle/Resources/views/ExportGeneration/wait.html.twig b/src/Bundle/ChillMainBundle/Resources/views/ExportGeneration/wait.html.twig index 44b814a6b..352662609 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/ExportGeneration/wait.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/ExportGeneration/wait.html.twig @@ -23,10 +23,34 @@ {{ 'export.generation.Come back later'|trans|chill_return_path_label }} -
  • - - {{ 'Save'|trans }} - -
  • + {% if not exportGeneration.isLinkedToSavedExport %} +
  • + + {{ 'Save'|trans }} + +
  • + {% else %} +
  • + +
  • + {% endif %} {% endblock content %} diff --git a/src/Bundle/ChillMainBundle/Resources/views/SavedExport/edit.html.twig b/src/Bundle/ChillMainBundle/Resources/views/SavedExport/edit.html.twig index a8a737c1e..f1f741d1a 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/SavedExport/edit.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/SavedExport/edit.html.twig @@ -26,7 +26,7 @@