mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-25 03:26:12 +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 Psr\Log\LoggerInterface;
|
||||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||||
use Symfony\Component\Form\FormFactoryInterface;
|
use Symfony\Component\Form\FormFactoryInterface;
|
||||||
|
use Chill\MainBundle\Redis\ChillRedis;
|
||||||
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ExportController is the controller use for exporting data.
|
* ExportController is the controller use for exporting data.
|
||||||
@ -66,16 +68,32 @@ class ExportController extends Controller
|
|||||||
*/
|
*/
|
||||||
protected $formFactory;
|
protected $formFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var ChillRedis
|
||||||
|
*/
|
||||||
|
protected $redis;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var TranslatorInterface
|
||||||
|
*/
|
||||||
|
protected $translator;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
ChillRedis $chillRedis,
|
||||||
ExportManager $exportManager,
|
ExportManager $exportManager,
|
||||||
FormFactoryInterface $formFactory,
|
FormFactoryInterface $formFactory,
|
||||||
LoggerInterface $logger,
|
LoggerInterface $logger,
|
||||||
SessionInterface $session
|
SessionInterface $session,
|
||||||
|
TranslatorInterface $translator
|
||||||
) {
|
) {
|
||||||
|
$this->redis = $chillRedis;
|
||||||
$this->exportManager = $exportManager;
|
$this->exportManager = $exportManager;
|
||||||
$this->formFactory = $formFactory;
|
$this->formFactory = $formFactory;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->session = $session;
|
$this->session = $session;
|
||||||
|
$this->translator = $translator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -393,9 +411,8 @@ class ExportController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gather data stored in session from previous steps, and redirect
|
* Gather data stored in session from previous steps, store it inside redis
|
||||||
* to the `generate` action, compiling data from previous step in the URL
|
* and redirect to the `generate` action.
|
||||||
* (to obtain a GET HTTP query).
|
|
||||||
*
|
*
|
||||||
* The data from previous steps is removed from session.
|
* The data from previous steps is removed from session.
|
||||||
*
|
*
|
||||||
@ -416,22 +433,24 @@ class ExportController extends Controller
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$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
|
// remove data from session
|
||||||
$this->session->remove('export_step_raw');
|
$this->session->remove('export_step_raw');
|
||||||
$this->session->remove('export_step');
|
$this->session->remove('export_step');
|
||||||
$this->session->remove('formatter_step_raw');
|
$this->session->remove('formatter_step_raw');
|
||||||
$this->session->remove('formatter_step');
|
$this->session->remove('formatter_step');
|
||||||
|
|
||||||
$redirectParameters = array_merge(
|
return $this->redirectToRoute('chill_main_export_download', [ 'key' => $key, 'alias' => $alias ]);
|
||||||
$dataFormatter ?? [],
|
|
||||||
$dataExport ?? [],
|
|
||||||
$dataCenters ?? [],
|
|
||||||
array('alias' => $alias)
|
|
||||||
);
|
|
||||||
unset($redirectParameters['_token']);
|
|
||||||
|
|
||||||
return $this->redirectToRoute('chill_main_export_download',
|
|
||||||
$redirectParameters);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -447,44 +466,65 @@ class ExportController extends Controller
|
|||||||
{
|
{
|
||||||
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
|
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
|
||||||
$exportManager = $this->exportManager;
|
$exportManager = $this->exportManager;
|
||||||
$export = $exportManager->getExport($alias);
|
$key = $request->query->get('key', null);
|
||||||
|
|
||||||
$formCenters = $this->createCreateFormExport($alias, 'generate_centers');
|
list($dataCenters, $dataExport, $dataFormatter) = $this->rebuildData($key);
|
||||||
$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();
|
|
||||||
}
|
|
||||||
|
|
||||||
$r = $exportManager->generate(
|
$r = $exportManager->generate(
|
||||||
$alias,
|
$alias,
|
||||||
$dataCenters['centers'],
|
$dataCenters['centers'],
|
||||||
$dataExport['export'],
|
$dataExport['export'],
|
||||||
isset($dataFormatter) ? $dataFormatter['formatter'] : []
|
$dataFormatter !== NULL ? $dataFormatter['formatter'] : []
|
||||||
);
|
);
|
||||||
|
|
||||||
return $r;
|
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)
|
public function downloadResultAction(Request $request, $alias)
|
||||||
{
|
{
|
||||||
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
|
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
|
||||||
$exportManager = $this->exportManager;
|
$exportManager = $this->exportManager;
|
||||||
$formCenters = $this->createCreateFormExport($alias, 'generate_centers');
|
$key = $request->query->get('key', null);
|
||||||
$formCenters->handleRequest($request);
|
|
||||||
$dataCenters = $formCenters->getData();
|
|
||||||
|
|
||||||
$formExport = $this->createCreateFormExport($alias, 'generate_export', $dataCenters);
|
list($dataCenters, $dataExport, $dataFormatter) = $this->rebuildData($key);
|
||||||
$formExport->handleRequest($request);
|
|
||||||
$dataExport = $formExport->getData();
|
|
||||||
|
|
||||||
$formatterAlias = $exportManager->getFormatterAlias($dataExport['export']);
|
$formatterAlias = $exportManager->getFormatterAlias($dataExport['export']);
|
||||||
if ($formatterAlias !== null) {
|
if ($formatterAlias !== null) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user