mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Introduced a new route and controller method to create saved exports directly from an export generation. Updated the Twig template to include a "Save" button, enabling users to utilize this new feature seamlessly. This enhances export management and provides a more convenient user experience.
197 lines
7.2 KiB
PHP
197 lines
7.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\Controller;
|
|
|
|
use Chill\MainBundle\Entity\ExportGeneration;
|
|
use Chill\MainBundle\Entity\SavedExport;
|
|
use Chill\MainBundle\Entity\User;
|
|
use Chill\MainBundle\Export\ExportInterface;
|
|
use Chill\MainBundle\Export\ExportManager;
|
|
use Chill\MainBundle\Export\GroupedExportInterface;
|
|
use Chill\MainBundle\Form\SavedExportType;
|
|
use Chill\MainBundle\Repository\SavedExportRepositoryInterface;
|
|
use Chill\MainBundle\Security\Authorization\ExportGenerationVoter;
|
|
use Chill\MainBundle\Security\Authorization\SavedExportVoter;
|
|
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
use Symfony\Component\Form\FormFactoryInterface;
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
use Symfony\Component\Security\Core\Security;
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
final readonly class SavedExportController
|
|
{
|
|
public function __construct(
|
|
private \Twig\Environment $templating,
|
|
private EntityManagerInterface $entityManager,
|
|
private ExportManager $exportManager,
|
|
private FormFactoryInterface $formFactory,
|
|
private SavedExportRepositoryInterface $savedExportRepository,
|
|
private Security $security,
|
|
private SessionInterface $session,
|
|
private TranslatorInterface $translator,
|
|
private UrlGeneratorInterface $urlGenerator,
|
|
private TranslatableStringHelperInterface $translatableStringHelper,
|
|
) {}
|
|
|
|
#[Route(path: '/{_locale}/exports/saved/{id}/delete', name: 'chill_main_export_saved_delete')]
|
|
public function delete(SavedExport $savedExport, Request $request): Response
|
|
{
|
|
if (!$this->security->isGranted(SavedExportVoter::DELETE, $savedExport)) {
|
|
throw new AccessDeniedHttpException();
|
|
}
|
|
|
|
$form = $this->formFactory->create();
|
|
$form->add('submit', SubmitType::class, ['label' => 'Delete']);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$this->entityManager->remove($savedExport);
|
|
$this->entityManager->flush();
|
|
|
|
$this->session->getFlashBag()->add('success', $this->translator->trans('saved_export.Export is deleted'));
|
|
|
|
return new RedirectResponse(
|
|
$this->urlGenerator->generate('chill_main_export_saved_list_my')
|
|
);
|
|
}
|
|
|
|
return new Response(
|
|
$this->templating->render(
|
|
'@ChillMain/SavedExport/delete.html.twig',
|
|
[
|
|
'saved_export' => $savedExport,
|
|
'delete_form' => $form->createView(),
|
|
]
|
|
)
|
|
);
|
|
}
|
|
|
|
#[Route(path: '/exports/saved/create-from-export-generation/{id}/new', name: 'chill_main_export_saved_create_from_export_generation')]
|
|
public function createFromExportGeneration(ExportGeneration $exportGeneration, Request $request): Response
|
|
{
|
|
if (!$this->security->isGranted(ExportGenerationVoter::VIEW, $exportGeneration)) {
|
|
throw new AccessDeniedHttpException();
|
|
}
|
|
|
|
$user = $this->security->getUser();
|
|
if (!$user instanceof User) {
|
|
throw new AccessDeniedHttpException('only regular user can create a saved export');
|
|
}
|
|
|
|
$savedExport = new SavedExport();
|
|
$savedExport
|
|
->setExportAlias($exportGeneration->getExportAlias())
|
|
->setUser($user)
|
|
->setOptions($exportGeneration->getOptions())
|
|
->setTitle($this->translator->trans($this->exportManager->getExport($exportGeneration->getExportAlias())->getTitle()));
|
|
|
|
$form = $this->formFactory->create(SavedExportType::class, $savedExport);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$this->entityManager->persist($savedExport);
|
|
$this->entityManager->flush();
|
|
|
|
$this->session->getFlashBag()->add('success', $this->translator->trans('saved_export.Saved export is saved!'));
|
|
|
|
return new RedirectResponse(
|
|
$this->urlGenerator->generate('chill_main_export_saved_list_my'),
|
|
);
|
|
}
|
|
|
|
return new Response(
|
|
$this->templating->render(
|
|
'@ChillMain/SavedExport/edit.html.twig',
|
|
[
|
|
'form' => $form->createView(),
|
|
],
|
|
)
|
|
);
|
|
}
|
|
|
|
#[Route(path: '/{_locale}/exports/saved/{id}/edit', name: 'chill_main_export_saved_edit')]
|
|
public function edit(SavedExport $savedExport, Request $request): Response
|
|
{
|
|
if (!$this->security->isGranted(SavedExportVoter::EDIT, $savedExport)) {
|
|
throw new AccessDeniedHttpException();
|
|
}
|
|
|
|
$form = $this->formFactory->create(SavedExportType::class, $savedExport);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$this->entityManager->flush();
|
|
|
|
$this->session->getFlashBag()->add('success', $this->translator->trans('saved_export.Saved export is saved!'));
|
|
|
|
return new RedirectResponse(
|
|
$this->urlGenerator->generate('chill_main_export_saved_list_my')
|
|
);
|
|
}
|
|
|
|
return new Response(
|
|
$this->templating->render(
|
|
'@ChillMain/SavedExport/edit.html.twig',
|
|
[
|
|
'form' => $form->createView(),
|
|
]
|
|
)
|
|
);
|
|
}
|
|
|
|
#[Route(path: '/{_locale}/exports/saved/my', name: 'chill_main_export_saved_list_my')]
|
|
public function list(): Response
|
|
{
|
|
$user = $this->security->getUser();
|
|
|
|
if (!$this->security->isGranted('ROLE_USER') || !$user instanceof User) {
|
|
throw new AccessDeniedHttpException();
|
|
}
|
|
|
|
$exports = $this->savedExportRepository->findByUser($user, ['title' => 'ASC']);
|
|
|
|
// group by center
|
|
/** @var array<string, array{saved: SavedExport, export: ExportInterface}> $exportsGrouped */
|
|
$exportsGrouped = [];
|
|
|
|
foreach ($exports as $savedExport) {
|
|
$export = $this->exportManager->getExport($savedExport->getExportAlias());
|
|
|
|
$exportsGrouped[
|
|
$export instanceof GroupedExportInterface
|
|
? $this->translator->trans($export->getGroup()) : '_'
|
|
][] = ['saved' => $savedExport, 'export' => $export];
|
|
}
|
|
|
|
ksort($exportsGrouped);
|
|
|
|
return new Response(
|
|
$this->templating->render(
|
|
'@ChillMain/SavedExport/index.html.twig',
|
|
[
|
|
'grouped_exports' => $exportsGrouped,
|
|
'total' => \count($exports),
|
|
]
|
|
)
|
|
);
|
|
}
|
|
}
|