mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-23 23:25:00 +00:00
Partage d'export enregistré et génération asynchrone des exports
This commit is contained in:
@@ -11,35 +11,39 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\Entity\ExportGeneration;
|
||||
use Chill\MainBundle\Entity\SavedExport;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Export\DirectExportInterface;
|
||||
use Chill\MainBundle\Export\ExportConfigNormalizer;
|
||||
use Chill\MainBundle\Export\ExportConfigProcessor;
|
||||
use Chill\MainBundle\Export\ExportFormHelper;
|
||||
use Chill\MainBundle\Export\ExportInterface;
|
||||
use Chill\MainBundle\Export\ExportManager;
|
||||
use Chill\MainBundle\Form\SavedExportType;
|
||||
use Chill\MainBundle\Export\Messenger\ExportRequestGenerationMessage;
|
||||
use Chill\MainBundle\Form\Type\Export\ExportType;
|
||||
use Chill\MainBundle\Form\Type\Export\FormatterType;
|
||||
use Chill\MainBundle\Form\Type\Export\PickCenterType;
|
||||
use Chill\MainBundle\Redis\ChillRedis;
|
||||
use Chill\MainBundle\Repository\SavedExportRepositoryInterface;
|
||||
use Chill\MainBundle\Repository\SavedExportOrExportGenerationRepository;
|
||||
use Chill\MainBundle\Security\Authorization\ChillExportVoter;
|
||||
use Chill\MainBundle\Security\Authorization\SavedExportVoter;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Clock\ClockInterface;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
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\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* Class ExportController
|
||||
@@ -50,117 +54,23 @@ class ExportController extends AbstractController
|
||||
private readonly bool $filterStatsByCenters;
|
||||
|
||||
public function __construct(
|
||||
private readonly ChillRedis $redis,
|
||||
private readonly ExportManager $exportManager,
|
||||
private readonly FormFactoryInterface $formFactory,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly SessionInterface $session,
|
||||
private readonly TranslatorInterface $translator,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly ExportFormHelper $exportFormHelper,
|
||||
private readonly SavedExportRepositoryInterface $savedExportRepository,
|
||||
private readonly Security $security,
|
||||
ParameterBagInterface $parameterBag,
|
||||
private readonly MessageBusInterface $messageBus,
|
||||
private readonly ClockInterface $clock,
|
||||
private readonly ExportConfigNormalizer $exportConfigNormalizer,
|
||||
private readonly SavedExportOrExportGenerationRepository $savedExportOrExportGenerationRepository,
|
||||
private readonly ExportConfigProcessor $exportConfigProcessor,
|
||||
) {
|
||||
$this->filterStatsByCenters = $parameterBag->get('chill_main')['acl']['filter_stats_by_center'];
|
||||
}
|
||||
|
||||
#[Route(path: '/{_locale}/exports/download/{alias}', name: 'chill_main_export_download', methods: ['GET'])]
|
||||
public function downloadResultAction(Request $request, mixed $alias)
|
||||
{
|
||||
/** @var ExportManager $exportManager */
|
||||
$exportManager = $this->exportManager;
|
||||
$export = $exportManager->getExport($alias);
|
||||
$key = $request->query->get('key', null);
|
||||
$savedExport = $this->getSavedExportFromRequest($request);
|
||||
|
||||
[$dataCenters, $dataExport, $dataFormatter] = $this->rebuildData($key, $savedExport);
|
||||
|
||||
$formatterAlias = $exportManager->getFormatterAlias($dataExport['export']);
|
||||
|
||||
if (null !== $formatterAlias) {
|
||||
$formater = $exportManager->getFormatter($formatterAlias);
|
||||
} else {
|
||||
$formater = null;
|
||||
}
|
||||
|
||||
$viewVariables = [
|
||||
'alias' => $alias,
|
||||
'export' => $export,
|
||||
'export_group' => $this->getExportGroup($export),
|
||||
'saved_export' => $savedExport,
|
||||
];
|
||||
|
||||
if ($formater instanceof \Chill\MainBundle\Export\Formatter\CSVListFormatter) {
|
||||
// due to a bug in php, we add the mime type in the download view
|
||||
$viewVariables['mime_type'] = 'text/csv';
|
||||
}
|
||||
|
||||
return $this->render('@ChillMain/Export/download.html.twig', $viewVariables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a report.
|
||||
*
|
||||
* This action must work with GET queries.
|
||||
*
|
||||
* @param string $alias
|
||||
*/
|
||||
#[Route(path: '/{_locale}/exports/generate/{alias}', name: 'chill_main_export_generate', methods: ['GET'])]
|
||||
public function generateAction(Request $request, $alias): Response
|
||||
{
|
||||
/** @var ExportManager $exportManager */
|
||||
$exportManager = $this->exportManager;
|
||||
$key = $request->query->get('key', null);
|
||||
$savedExport = $this->getSavedExportFromRequest($request);
|
||||
|
||||
[$dataCenters, $dataExport, $dataFormatter] = $this->rebuildData($key, $savedExport);
|
||||
|
||||
return $exportManager->generate(
|
||||
$alias,
|
||||
$dataCenters['centers'],
|
||||
$dataExport['export'],
|
||||
null !== $dataFormatter ? $dataFormatter['formatter'] : []
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \RedisException
|
||||
*/
|
||||
#[Route(path: '/{_locale}/exports/generate-from-saved/{id}', name: 'chill_main_export_generate_from_saved')]
|
||||
public function generateFromSavedExport(SavedExport $savedExport): RedirectResponse
|
||||
{
|
||||
$this->denyAccessUnlessGranted(SavedExportVoter::GENERATE, $savedExport);
|
||||
|
||||
$key = md5(uniqid((string) random_int(0, mt_getrandmax()), false));
|
||||
|
||||
$this->redis->setEx($key, 3600, \serialize($savedExport->getOptions()));
|
||||
|
||||
return $this->redirectToRoute(
|
||||
'chill_main_export_download',
|
||||
[
|
||||
'alias' => $savedExport->getExportAlias(),
|
||||
'key' => $key, 'prevent_save' => true,
|
||||
'returnPath' => $this->generateUrl('chill_main_export_saved_list_my'),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the list of available exports.
|
||||
*/
|
||||
#[Route(path: '/{_locale}/exports/', name: 'chill_main_export_index')]
|
||||
public function indexAction(): Response
|
||||
{
|
||||
$exportManager = $this->exportManager;
|
||||
|
||||
$exports = $exportManager->getExportsGrouped(true);
|
||||
|
||||
return $this->render('@ChillMain/Export/layout.html.twig', [
|
||||
'grouped_exports' => $exports,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* handle the step to build a query for an export.
|
||||
*
|
||||
@@ -197,64 +107,6 @@ class ExportController extends AbstractController
|
||||
};
|
||||
}
|
||||
|
||||
#[Route(path: '/{_locale}/export/saved/update-from-key/{id}/{key}', name: 'chill_main_export_saved_edit_options_from_key')]
|
||||
public function editSavedExportOptionsFromKey(SavedExport $savedExport, string $key): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted('ROLE_USER');
|
||||
$user = $this->getUser();
|
||||
|
||||
if (!$user instanceof User) {
|
||||
throw new AccessDeniedHttpException();
|
||||
}
|
||||
|
||||
$data = $this->rebuildRawData($key);
|
||||
|
||||
$savedExport
|
||||
->setOptions($data);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('chill_main_export_saved_edit', ['id' => $savedExport->getId()]);
|
||||
}
|
||||
|
||||
#[Route(path: '/{_locale}/export/save-from-key/{alias}/{key}', name: 'chill_main_export_save_from_key')]
|
||||
public function saveFromKey(string $alias, string $key, Request $request): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted('ROLE_USER');
|
||||
$user = $this->getUser();
|
||||
|
||||
if (!$user instanceof User) {
|
||||
throw new AccessDeniedHttpException();
|
||||
}
|
||||
|
||||
$data = $this->rebuildRawData($key);
|
||||
|
||||
$savedExport = new SavedExport();
|
||||
$savedExport
|
||||
->setOptions($data)
|
||||
->setExportAlias($alias)
|
||||
->setUser($user);
|
||||
|
||||
$form = $this->createForm(SavedExportType::class, $savedExport);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->entityManager->persist($savedExport);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('chill_main_export_index');
|
||||
}
|
||||
|
||||
return $this->render(
|
||||
'@ChillMain/SavedExport/new.html.twig',
|
||||
[
|
||||
'form' => $form->createView(),
|
||||
'saved_export' => $savedExport,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* create a form to show on different steps.
|
||||
*
|
||||
@@ -262,19 +114,26 @@ class ExportController extends AbstractController
|
||||
*/
|
||||
protected function createCreateFormExport(string $alias, string $step, array $data, ?SavedExport $savedExport): FormInterface
|
||||
{
|
||||
/** @var ExportManager $exportManager */
|
||||
$exportManager = $this->exportManager;
|
||||
$isGenerate = str_starts_with($step, 'generate_');
|
||||
$canEditFull = $this->security->isGranted(ChillExportVoter::COMPOSE_EXPORT);
|
||||
|
||||
if (!$canEditFull && null === $savedExport) {
|
||||
throw new AccessDeniedHttpException('The user is not allowed to edit all filter, it should edit only SavedExport');
|
||||
}
|
||||
|
||||
$options = match ($step) {
|
||||
'export', 'generate_export' => [
|
||||
'export_alias' => $alias,
|
||||
'picked_centers' => $exportManager->getPickedCenters($data['centers'] ?? []),
|
||||
'picked_centers' => $this->filterStatsByCenters ? $this->exportFormHelper->getPickedCenters($data) : [],
|
||||
'can_edit_full' => $canEditFull,
|
||||
'allowed_filters' => $canEditFull ? null : $this->exportConfigProcessor->retrieveUsedFilters($savedExport->getOptions()['filters']),
|
||||
'allowed_aggregators' => $canEditFull ? null : $this->exportConfigProcessor->retrieveUsedAggregators($savedExport->getOptions()['aggregators']),
|
||||
],
|
||||
'formatter', 'generate_formatter' => [
|
||||
'export_alias' => $alias,
|
||||
'formatter_alias' => $exportManager->getFormatterAlias($data['export']),
|
||||
'aggregator_aliases' => $exportManager->getUsedAggregatorsAliases($data['export']),
|
||||
'aggregator_aliases' => $exportManager->getUsedAggregatorsAliases($data['export']['aggregators']),
|
||||
],
|
||||
default => [
|
||||
'export_alias' => $alias,
|
||||
@@ -283,14 +142,14 @@ class ExportController extends AbstractController
|
||||
|
||||
$defaultFormData = match ($savedExport) {
|
||||
null => $this->exportFormHelper->getDefaultData($step, $exportManager->getExport($alias), $options),
|
||||
default => $this->exportFormHelper->savedExportDataToFormData($savedExport, $step, $options),
|
||||
default => $this->exportFormHelper->savedExportDataToFormData($savedExport, $step),
|
||||
};
|
||||
|
||||
$builder = $this->formFactory
|
||||
->createNamedBuilder(
|
||||
'',
|
||||
FormType::class,
|
||||
$defaultFormData,
|
||||
'centers' === $step ? ['centers' => $defaultFormData] : $defaultFormData,
|
||||
[
|
||||
'method' => $isGenerate ? Request::METHOD_GET : Request::METHOD_POST,
|
||||
'csrf_protection' => !$isGenerate,
|
||||
@@ -429,13 +288,14 @@ class ExportController extends AbstractController
|
||||
* and redirect to the `generate` action.
|
||||
*
|
||||
* The data from previous steps is removed from session.
|
||||
*
|
||||
* @param string $alias
|
||||
*
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
private function forwardToGenerate(Request $request, DirectExportInterface|ExportInterface $export, $alias, ?SavedExport $savedExport)
|
||||
private function forwardToGenerate(Request $request, DirectExportInterface|ExportInterface $export, $alias, ?SavedExport $savedExport): Response
|
||||
{
|
||||
$user = $this->getUser();
|
||||
|
||||
if (!$user instanceof User) {
|
||||
throw new AccessDeniedHttpException('only regular users can generate export');
|
||||
}
|
||||
$dataCenters = $this->session->get('centers_step_raw', null);
|
||||
$dataFormatter = $this->session->get('formatter_step_raw', null);
|
||||
$dataExport = $this->session->get('export_step_raw', null);
|
||||
@@ -448,60 +308,82 @@ class ExportController extends AbstractController
|
||||
]);
|
||||
}
|
||||
|
||||
$parameters = [
|
||||
'formatter' => $dataFormatter ?? [],
|
||||
'export' => $dataExport ?? [],
|
||||
'centers' => $dataCenters ?? [],
|
||||
'alias' => $alias,
|
||||
];
|
||||
unset($parameters['_token']);
|
||||
$key = md5(uniqid((string) random_int(0, mt_getrandmax()), false));
|
||||
$dataToNormalize = $this->buildExportDataForNormalization(
|
||||
$alias,
|
||||
$dataCenters,
|
||||
$dataExport,
|
||||
$dataFormatter,
|
||||
$savedExport,
|
||||
);
|
||||
|
||||
$this->redis->setEx($key, 3600, \serialize($parameters));
|
||||
$deleteAt = $this->clock->now()->add(new \DateInterval('P6M'));
|
||||
$options = $this->exportConfigNormalizer->normalizeConfig($alias, $dataToNormalize);
|
||||
$exportGeneration = match (null === $savedExport) {
|
||||
true => new ExportGeneration($alias, $options, $deleteAt),
|
||||
false => ExportGeneration::fromSavedExport($savedExport, $deleteAt, $options),
|
||||
};
|
||||
|
||||
$this->entityManager->persist($exportGeneration);
|
||||
$this->entityManager->flush();
|
||||
$this->messageBus->dispatch(new ExportRequestGenerationMessage($exportGeneration, $user));
|
||||
|
||||
// remove data from session
|
||||
$this->session->remove('centers_step_raw');
|
||||
$this->session->remove('export_step_raw');
|
||||
$this->session->remove('export_step');
|
||||
$this->session->remove('formatter_step_raw');
|
||||
$this->session->remove('formatter_step');
|
||||
|
||||
return $this->redirectToRoute('chill_main_export_download', [
|
||||
'key' => $key,
|
||||
'alias' => $alias,
|
||||
'from_saved' => $savedExport?->getId(),
|
||||
]);
|
||||
return $this->redirectToRoute('chill_main_export-generation_wait', ['id' => $exportGeneration->getId()]);
|
||||
}
|
||||
|
||||
private function rebuildData($key, ?SavedExport $savedExport)
|
||||
/**
|
||||
* Build the export form data into a way suitable for normalization.
|
||||
*
|
||||
* @param string $alias the export alias
|
||||
* @param array $dataCenters Raw data from center step
|
||||
* @param array $dataExport Raw data from export step
|
||||
* @param array $dataFormatter Raw data from formatter step
|
||||
*/
|
||||
private function buildExportDataForNormalization(string $alias, ?array $dataCenters, array $dataExport, array $dataFormatter, ?SavedExport $savedExport): array
|
||||
{
|
||||
$rawData = $this->rebuildRawData($key);
|
||||
|
||||
$alias = $rawData['alias'];
|
||||
|
||||
if ($this->filterStatsByCenters) {
|
||||
$formCenters = $this->createCreateFormExport($alias, 'generate_centers', [], $savedExport);
|
||||
$formCenters->submit($rawData['centers']);
|
||||
$dataCenters = $formCenters->getData();
|
||||
$formCenters = $this->createCreateFormExport($alias, 'generate_centers', [], null);
|
||||
$formCenters->submit($dataCenters);
|
||||
$dataAsCollection = $formCenters->getData()['centers'];
|
||||
$centers = $dataAsCollection['centers'];
|
||||
$regroupments = $dataAsCollection['regroupments'] ?? [];
|
||||
$dataCenters = [
|
||||
'centers' => $centers instanceof Collection ? $centers->toArray() : $centers,
|
||||
'regroupments' => $regroupments instanceof Collection ? $regroupments->toArray() : $regroupments,
|
||||
];
|
||||
} else {
|
||||
$dataCenters = ['centers' => []];
|
||||
$dataCenters = ['centers' => [], 'regroupments' => []];
|
||||
}
|
||||
|
||||
$formExport = $this->createCreateFormExport($alias, 'generate_export', $dataCenters, $savedExport);
|
||||
$formExport->submit($rawData['export']);
|
||||
$formExport->submit($dataExport);
|
||||
$dataExport = $formExport->getData();
|
||||
|
||||
if (\count($rawData['formatter']) > 0) {
|
||||
if (\count($dataFormatter) > 0) {
|
||||
$formFormatter = $this->createCreateFormExport(
|
||||
$alias,
|
||||
'generate_formatter',
|
||||
$dataExport,
|
||||
$savedExport
|
||||
);
|
||||
$formFormatter->submit($rawData['formatter']);
|
||||
$formFormatter->submit($dataFormatter);
|
||||
$dataFormatter = $formFormatter->getData();
|
||||
}
|
||||
|
||||
return [$dataCenters, $dataExport, $dataFormatter ?? null];
|
||||
return [
|
||||
'centers' => ['centers' => $dataCenters['centers'], 'regroupments' => $dataCenters['regroupments']],
|
||||
'export' => $dataExport['export']['export'] ?? [],
|
||||
'filters' => $dataExport['export']['filters'] ?? [],
|
||||
'aggregators' => $dataExport['export']['aggregators'] ?? [],
|
||||
'pick_formatter' => $dataExport['export']['pick_formatter']['alias'],
|
||||
'formatter' => $dataFormatter['formatter'] ?? [],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -509,7 +391,7 @@ class ExportController extends AbstractController
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
private function selectCentersStep(Request $request, DirectExportInterface|ExportInterface $export, $alias, ?SavedExport $savedExport = null)
|
||||
private function selectCentersStep(Request $request, DirectExportInterface|ExportInterface $export, $alias, ExportGeneration|SavedExport|null $savedExport = null)
|
||||
{
|
||||
if (!$this->filterStatsByCenters) {
|
||||
return $this->redirectToRoute('chill_main_export_new', [
|
||||
@@ -522,7 +404,12 @@ class ExportController extends AbstractController
|
||||
/** @var ExportManager $exportManager */
|
||||
$exportManager = $this->exportManager;
|
||||
|
||||
$form = $this->createCreateFormExport($alias, 'centers', [], $savedExport);
|
||||
$form = $this->createCreateFormExport(
|
||||
$alias,
|
||||
'centers',
|
||||
$this->exportFormHelper->getDefaultData('centers', $export, []),
|
||||
$savedExport
|
||||
);
|
||||
|
||||
if (Request::METHOD_POST === $request->getMethod()) {
|
||||
$form->handleRequest($request);
|
||||
@@ -538,7 +425,7 @@ class ExportController extends AbstractController
|
||||
false === $exportManager->isGrantedForElement(
|
||||
$export,
|
||||
null,
|
||||
$exportManager->getPickedCenters($data['centers'])
|
||||
$this->exportFormHelper->getPickedCenters($data['centers']),
|
||||
)
|
||||
) {
|
||||
throw $this->createAccessDeniedException('you do not have access to this export for those centers');
|
||||
@@ -548,7 +435,7 @@ class ExportController extends AbstractController
|
||||
'centers_step_raw',
|
||||
$request->request->all()
|
||||
);
|
||||
$this->session->set('centers_step', $data);
|
||||
$this->session->set('centers_step', $data['centers']);
|
||||
|
||||
return $this->redirectToRoute('chill_main_export_new', [
|
||||
'step' => $this->getNextStep('centers', $export),
|
||||
@@ -632,43 +519,15 @@ class ExportController extends AbstractController
|
||||
}
|
||||
}
|
||||
|
||||
private function rebuildRawData(?string $key): array
|
||||
{
|
||||
if (null === $key) {
|
||||
throw $this->createNotFoundException('key does not exists');
|
||||
}
|
||||
|
||||
if (1 !== $this->redis->exists($key)) {
|
||||
$this->addFlash('error', $this->translator->trans('This report is not available any more'));
|
||||
|
||||
throw $this->createNotFoundException('key does not exists');
|
||||
}
|
||||
|
||||
$serialized = $this->redis->get($key);
|
||||
|
||||
if (false === $serialized) {
|
||||
throw new \LogicException('the key could not be reached from redis');
|
||||
}
|
||||
|
||||
$rawData = \unserialize($serialized);
|
||||
|
||||
$this->logger->notice('[export] choices for an export unserialized', [
|
||||
'key' => $key,
|
||||
'rawData' => json_encode($rawData, JSON_THROW_ON_ERROR),
|
||||
]);
|
||||
|
||||
return $rawData;
|
||||
}
|
||||
|
||||
private function getSavedExportFromRequest(Request $request): ?SavedExport
|
||||
private function getSavedExportFromRequest(Request $request): SavedExport|ExportGeneration|null
|
||||
{
|
||||
$savedExport = match ($savedExportId = $request->query->get('from_saved', '')) {
|
||||
'' => null,
|
||||
default => $this->savedExportRepository->find($savedExportId),
|
||||
default => $this->savedExportOrExportGenerationRepository->findById($savedExportId),
|
||||
};
|
||||
|
||||
if (null !== $savedExport && !$this->security->isGranted(SavedExportVoter::EDIT, $savedExport)) {
|
||||
throw new AccessDeniedHttpException('saved export edition not allowed');
|
||||
if (null !== $savedExport && !$this->security->isGranted(SavedExportVoter::GENERATE, $savedExport)) {
|
||||
throw new AccessDeniedHttpException('saved export generation not allowed');
|
||||
}
|
||||
|
||||
return $savedExport;
|
||||
|
Reference in New Issue
Block a user