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.
This commit is contained in:
2025-04-23 09:11:50 +02:00
parent 0f6b10aa0a
commit 973450110b
6 changed files with 110 additions and 8 deletions

View File

@@ -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
{