mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-24 11:06:14 +00:00
store export parameters in redis key (issue #33)
This commit is contained in:
parent
1ea90c2d5f
commit
915b556125
@ -33,6 +33,8 @@ use Chill\MainBundle\Export\ExportManager;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Chill\MainBundle\Redis\ChillRedis;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* ExportController is the controller use for exporting data.
|
||||
@ -66,16 +68,32 @@ class ExportController extends Controller
|
||||
*/
|
||||
protected $formFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ChillRedis
|
||||
*/
|
||||
protected $redis;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
public function __construct(
|
||||
ChillRedis $chillRedis,
|
||||
ExportManager $exportManager,
|
||||
FormFactoryInterface $formFactory,
|
||||
LoggerInterface $logger,
|
||||
SessionInterface $session
|
||||
SessionInterface $session,
|
||||
TranslatorInterface $translator
|
||||
) {
|
||||
$this->redis = $chillRedis;
|
||||
$this->exportManager = $exportManager;
|
||||
$this->formFactory = $formFactory;
|
||||
$this->logger = $logger;
|
||||
$this->session = $session;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
|
||||
@ -393,9 +411,8 @@ class ExportController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Gather data stored in session from previous steps, and redirect
|
||||
* to the `generate` action, compiling data from previous step in the URL
|
||||
* (to obtain a GET HTTP query).
|
||||
* Gather data stored in session from previous steps, store it inside redis
|
||||
* and redirect to the `generate` action.
|
||||
*
|
||||
* The data from previous steps is removed from session.
|
||||
*
|
||||
@ -415,6 +432,17 @@ class ExportController extends Controller
|
||||
'alias' => $alias, 'step' => $this->getNextStep('generate', $export, true)
|
||||
));
|
||||
}
|
||||
|
||||
$parameters = [
|
||||
'formatter' => $dataFormatter ?? [],
|
||||
'export' => $dataExport ?? [],
|
||||
'centers' => $dataCenters ?? [],
|
||||
'alias' => $alias
|
||||
];
|
||||
unset($parameters['_token']);
|
||||
$key = md5(uniqid(rand(), false));
|
||||
|
||||
$this->redis->setEx($key, 3600, \serialize($parameters));
|
||||
|
||||
// remove data from session
|
||||
$this->session->remove('export_step_raw');
|
||||
@ -422,16 +450,7 @@ class ExportController extends Controller
|
||||
$this->session->remove('formatter_step_raw');
|
||||
$this->session->remove('formatter_step');
|
||||
|
||||
$redirectParameters = array_merge(
|
||||
$dataFormatter ?? [],
|
||||
$dataExport ?? [],
|
||||
$dataCenters ?? [],
|
||||
array('alias' => $alias)
|
||||
);
|
||||
unset($redirectParameters['_token']);
|
||||
|
||||
return $this->redirectToRoute('chill_main_export_download',
|
||||
$redirectParameters);
|
||||
return $this->redirectToRoute('chill_main_export_download', [ 'key' => $key, 'alias' => $alias ]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -447,44 +466,65 @@ class ExportController extends Controller
|
||||
{
|
||||
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
|
||||
$exportManager = $this->exportManager;
|
||||
$export = $exportManager->getExport($alias);
|
||||
|
||||
$formCenters = $this->createCreateFormExport($alias, 'generate_centers');
|
||||
$formCenters->handleRequest($request);
|
||||
$dataCenters = $formCenters->getData();
|
||||
|
||||
$formExport = $this->createCreateFormExport($alias, 'generate_export', $dataCenters);
|
||||
$formExport->handleRequest($request);
|
||||
$dataExport = $formExport->getData();
|
||||
|
||||
if ($export instanceof \Chill\MainBundle\Export\ExportInterface) {
|
||||
$formFormatter = $this->createCreateFormExport($alias, 'generate_formatter',
|
||||
$dataExport);
|
||||
$formFormatter->handleRequest($request);
|
||||
$dataFormatter = $formFormatter->getData();
|
||||
}
|
||||
$key = $request->query->get('key', null);
|
||||
|
||||
list($dataCenters, $dataExport, $dataFormatter) = $this->rebuildData($key);
|
||||
|
||||
$r = $exportManager->generate(
|
||||
$alias,
|
||||
$dataCenters['centers'],
|
||||
$dataExport['export'],
|
||||
isset($dataFormatter) ? $dataFormatter['formatter'] : []
|
||||
$dataFormatter !== NULL ? $dataFormatter['formatter'] : []
|
||||
);
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
protected function rebuildData($key)
|
||||
{
|
||||
if ($key === NULL) {
|
||||
throw $this->createHttpNotFoundException("key does not exists");
|
||||
}
|
||||
|
||||
if ($this->redis->exists($key) !== 1) {
|
||||
$this->addFlash('error', $this->translator->trans("This report is not available any more"));
|
||||
throw $this->createHttpNotFoundException("key does not exists");
|
||||
}
|
||||
|
||||
$serialized = $this->redis->get($key);
|
||||
|
||||
if ($serialized === false) {
|
||||
throw new \LogicException("the key could not be reached from redis");
|
||||
}
|
||||
|
||||
$rawData = \unserialize($serialized);
|
||||
$alias = $rawData['alias'];
|
||||
|
||||
$formCenters = $this->createCreateFormExport($alias, 'generate_centers');
|
||||
$formCenters->submit($rawData['centers']);
|
||||
$dataCenters = $formCenters->getData();
|
||||
|
||||
$formExport = $this->createCreateFormExport($alias, 'generate_export', $dataCenters);
|
||||
$formExport->submit($rawData['export']);
|
||||
$dataExport = $formExport->getData();
|
||||
|
||||
if (count($rawData['formatter']) > 0) {
|
||||
$formFormatter = $this->createCreateFormExport($alias, 'generate_formatter',
|
||||
$dataExport);
|
||||
$formFormatter->submit($rawData['formatter']);
|
||||
$dataFormatter = $formFormatter->getData();
|
||||
}
|
||||
|
||||
return [$dataCenters, $dataExport, $dataFormatter ?? null];
|
||||
}
|
||||
|
||||
public function downloadResultAction(Request $request, $alias)
|
||||
{
|
||||
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
|
||||
$exportManager = $this->exportManager;
|
||||
$formCenters = $this->createCreateFormExport($alias, 'generate_centers');
|
||||
$formCenters->handleRequest($request);
|
||||
$dataCenters = $formCenters->getData();
|
||||
|
||||
$formExport = $this->createCreateFormExport($alias, 'generate_export', $dataCenters);
|
||||
$formExport->handleRequest($request);
|
||||
$dataExport = $formExport->getData();
|
||||
$key = $request->query->get('key', null);
|
||||
|
||||
list($dataCenters, $dataExport, $dataFormatter) = $this->rebuildData($key);
|
||||
|
||||
$formatterAlias = $exportManager->getFormatterAlias($dataExport['export']);
|
||||
if ($formatterAlias !== null) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user