mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
handle requests and form to build an export
This commit is contained in:
parent
e1a9ad1612
commit
ce2119ee6f
@ -67,10 +67,13 @@ class ExportController extends Controller
|
|||||||
|
|
||||||
switch ($step) {
|
switch ($step) {
|
||||||
case 'export':
|
case 'export':
|
||||||
return $this->renderExportForm($alias);
|
return $this->exportFormStep($alias);
|
||||||
break;
|
break;
|
||||||
case 'formatter':
|
case 'formatter':
|
||||||
return $this->renderFormatterStep($request, $alias);
|
return $this->formatterFormStep($request, $alias);
|
||||||
|
break;
|
||||||
|
case 'generate':
|
||||||
|
return $this->forwardToGenerate($request, $alias);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw $this->createNotFoundException("The given step '$step' is invalid");
|
throw $this->createNotFoundException("The given step '$step' is invalid");
|
||||||
@ -83,13 +86,13 @@ class ExportController extends Controller
|
|||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @return \Symfony\Component\HttpFoundation\Response
|
* @return \Symfony\Component\HttpFoundation\Response
|
||||||
*/
|
*/
|
||||||
protected function renderExportForm($alias)
|
protected function exportFormStep($alias)
|
||||||
{
|
{
|
||||||
$exportManager = $this->get('chill.main.export_manager');
|
$exportManager = $this->get('chill.main.export_manager');
|
||||||
|
|
||||||
$export = $exportManager->getExport($alias);
|
$export = $exportManager->getExport($alias);
|
||||||
|
|
||||||
$form = $this->createCreateFormExport($alias);
|
$form = $this->createCreateFormExport($alias, 'export');
|
||||||
|
|
||||||
return $this->render('ChillMainBundle:Export:new.html.twig', array(
|
return $this->render('ChillMainBundle:Export:new.html.twig', array(
|
||||||
'form' => $form->createView(),
|
'form' => $form->createView(),
|
||||||
@ -103,93 +106,110 @@ class ExportController extends Controller
|
|||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @return \Symfony\Component\Form\Form
|
* @return \Symfony\Component\Form\Form
|
||||||
*/
|
*/
|
||||||
protected function createCreateFormExport($alias)
|
protected function createCreateFormExport($alias, $step, $data = array())
|
||||||
{
|
{
|
||||||
$builder = $this->get('form.factory')
|
$builder = $this->get('form.factory')
|
||||||
->createNamedBuilder(null, FormType::class, array(), array(
|
->createNamedBuilder(null, FormType::class, array(), array(
|
||||||
'method' => 'GET',
|
'method' => 'GET',
|
||||||
'csrf_protection' => false,
|
'csrf_protection' => false,
|
||||||
'action' => $this->generateUrl('chill_main_export_new', array(
|
'action' => $this->generateUrl($this->getNextRoute($step), array(
|
||||||
'alias' => $alias
|
'alias' => $alias
|
||||||
))
|
))
|
||||||
));
|
));
|
||||||
|
|
||||||
$builder->add('export', ExportType::class, array(
|
if ($step === 'export') {
|
||||||
'export_alias' => $alias,
|
$builder->add('export', ExportType::class, array(
|
||||||
|
'export_alias' => $alias,
|
||||||
));
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($step === 'formatter') {
|
||||||
|
$builder->add('formatter', FormatterType::class, array(
|
||||||
|
'formatter_alias' => $data['export']['pick_formatter']['alias'],
|
||||||
|
'export_alias' => $alias,
|
||||||
|
'aggregator_aliases' => array() //TODO
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
$builder->add('submit', 'submit', array(
|
$builder->add('submit', 'submit', array(
|
||||||
'label' => 'Generate'
|
'label' => 'Generate'
|
||||||
));
|
));
|
||||||
|
|
||||||
$builder->add('step', 'hidden', array(
|
$builder->add('step', 'hidden', array(
|
||||||
'data' => 'formatter'
|
'data' => $this->getNextStep($step)
|
||||||
));
|
));
|
||||||
|
|
||||||
return $builder->getForm();
|
return $builder->getForm();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderFormatterStep(Request $request, $alias)
|
private function getNextStep($step)
|
||||||
|
{
|
||||||
|
switch($step) {
|
||||||
|
case 'export': return 'formatter';
|
||||||
|
case 'formatter' : return 'generate';
|
||||||
|
default:
|
||||||
|
throw new \LogicException("the step $step is not defined.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getNextRoute($step)
|
||||||
|
{
|
||||||
|
switch($step) {
|
||||||
|
case 'generate':
|
||||||
|
return 'chill_main_export_generate';
|
||||||
|
default:
|
||||||
|
return 'chill_main_export_new';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function formatterFormStep(Request $request, $alias)
|
||||||
{
|
{
|
||||||
$export = $this->get('chill.main.export_manager')->getExport($alias);
|
$export = $this->get('chill.main.export_manager')->getExport($alias);
|
||||||
|
|
||||||
$exportForm = $this->createCreateFormExport($alias);
|
$exportForm = $this->createCreateFormExport($alias, 'export');
|
||||||
$exportForm->handleRequest($request);
|
$exportForm->handleRequest($request);
|
||||||
$data = $exportForm->getData();
|
|
||||||
|
|
||||||
$form = $this->createCreateFormFormatter($request,
|
if ($exportForm->isValid()) {
|
||||||
$alias, array(), $data['export']['formatter']['alias']);
|
$data = $exportForm->getData();
|
||||||
|
$this->get('session')->set('export_step', $data);
|
||||||
|
$this->get('session')->set('export_step_raw', $request->query->all());
|
||||||
|
|
||||||
|
$form = $this->createCreateFormExport($alias,
|
||||||
|
'formatter', $data);
|
||||||
|
|
||||||
return $this->render('ChillMainBundle:Export:new_formatter_step.html.twig',
|
return $this->render('ChillMainBundle:Export:new_formatter_step.html.twig',
|
||||||
array(
|
array(
|
||||||
'form' => $form->createView(),
|
'form' => $form->createView(),
|
||||||
'export' => $export
|
'export' => $export
|
||||||
));
|
));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw new \LogicException("The form contains invalid data. Currently"
|
||||||
|
. " we do not handle invalid data in forms");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function forwardToGenerate(Request $request, $alias)
|
||||||
*
|
|
||||||
* @param Request $request
|
|
||||||
* @param type $formatterAlias
|
|
||||||
* @return \Symfony\Component\Form\Form
|
|
||||||
*/
|
|
||||||
protected function createCreateFormFormatter(Request $request,
|
|
||||||
$exportAlias, $aggregatorAliases, $formatterAlias = null)
|
|
||||||
{
|
{
|
||||||
var_dump($request->query->all());
|
$data = $this->get('session')->get('export_step');
|
||||||
$builder = $this->get('form.factory')
|
|
||||||
->createNamedBuilder(null, FormType::class, array(), array(
|
|
||||||
'method' => 'GET',
|
|
||||||
'csrf_protection' => false,
|
|
||||||
'action' => $this->generateUrl('chill_main_export_generate', array(
|
|
||||||
'alias' => $exportAlias
|
|
||||||
))
|
|
||||||
));
|
|
||||||
|
|
||||||
$builder->add('formatter', FormatterType::class, array(
|
$form = $this->createCreateFormExport($alias,
|
||||||
'formatter_alias' => $formatterAlias,
|
'formatter', $data);
|
||||||
'export_alias' => $exportAlias,
|
$form->handleRequest($request);
|
||||||
'aggregator_aliases' => $aggregatorAliases
|
|
||||||
));
|
|
||||||
|
|
||||||
// re-add the export type under hidden fields
|
if ($form->isValid()) {
|
||||||
$builderExport = $builder->create('export', FormType::class, array());
|
$dataFormatter = $form->getData();
|
||||||
$data = $request->query->all();
|
$this->get('session')->set('formatter_step', $dataFormatter);
|
||||||
foreach($data['export'] as $key => $value) {
|
|
||||||
$this->recursiveAddHiddenFieldsToForm($builderExport, $key, $value);
|
|
||||||
}
|
}
|
||||||
$builder->add($builderExport);
|
|
||||||
|
|
||||||
//add the formatter alias
|
$redirectParameters = array_merge(
|
||||||
$builder->add('formatter', HiddenType::class, array(
|
$this->get('session')->get('export_step_raw'),
|
||||||
'data' => $formatterAlias
|
$request->query->all(),
|
||||||
));
|
array('alias' => $alias)
|
||||||
|
);
|
||||||
|
|
||||||
$builder->add('submit', 'submit', array(
|
return $this->redirect($this->generateUrl(
|
||||||
'label' => 'Generate'
|
'chill_main_export_generate', $redirectParameters));
|
||||||
));
|
|
||||||
|
|
||||||
return $builder->getForm();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,56 +217,15 @@ class ExportController extends Controller
|
|||||||
{
|
{
|
||||||
$exportManager = $this->get('chill.main.export_manager');
|
$exportManager = $this->get('chill.main.export_manager');
|
||||||
|
|
||||||
$form = $this->createCreateFormGenerate($request, $alias);
|
$formExport = $this->createCreateFormExport($alias, 'export');
|
||||||
$form->handleRequest($request);
|
$formExport->handleRequest($request);
|
||||||
$data = $form->getData();
|
$dataExport = $formExport->getData();
|
||||||
|
|
||||||
return $exportManager->generate($alias, $data['export']);
|
$formFormatter = $this->createCreateFormExport($alias, 'formatter',
|
||||||
}
|
$dataExport);
|
||||||
|
$formFormatter->handleRequest($request);
|
||||||
/**
|
$dataFormatter = $formFormatter->getData();
|
||||||
*
|
|
||||||
* @param Request $request
|
|
||||||
* @param string $alias
|
|
||||||
* @return \Symfony\Component\Form\Form
|
|
||||||
*/
|
|
||||||
public function createCreateFormGenerate(Request $request, $alias,
|
|
||||||
$aggregatorAliases, $formatterAlias)
|
|
||||||
{
|
|
||||||
$builder = $this->get('form.factory')
|
|
||||||
->createNamedBuilder(null, FormType::class, array(), array(
|
|
||||||
'method' => 'GET',
|
|
||||||
'csrf_protection' => false,
|
|
||||||
'action' => $this->generateUrl('chill_main_export_generate', array(
|
|
||||||
'alias' => $alias
|
|
||||||
))
|
|
||||||
));
|
|
||||||
|
|
||||||
$builder->add('formatter', FormatterType::class, array(
|
return $exportManager->generate($alias, $dataExport['export']);
|
||||||
'formatter_alias' => $formatterAlias,
|
|
||||||
'export_alias' => $exportAlias,
|
|
||||||
'aggregator_aliases' => $aggregatorAliases
|
|
||||||
));
|
|
||||||
|
|
||||||
$builder->add('export', ExportType::class, array(
|
|
||||||
'export_alias' => $alias,
|
|
||||||
));
|
|
||||||
|
|
||||||
return $builder->getForm();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function recursiveAddHiddenFieldsToForm(FormBuilderInterface $builder, $key, $data)
|
|
||||||
{
|
|
||||||
if (is_array($data)) {
|
|
||||||
foreach($data as $subKey => $value) {
|
|
||||||
$subBuilder = $builder->create($subKey, FormType::class);
|
|
||||||
$this->recursiveAddHiddenFieldsToForm($subBuilder, $subKey, $value);
|
|
||||||
$builder->add($subBuilder);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$builder->add($key, 'hidden', array(
|
|
||||||
'data' => $data
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -347,7 +347,7 @@ class ExportManager
|
|||||||
private function retrieveUsedAggregators($data)
|
private function retrieveUsedAggregators($data)
|
||||||
{
|
{
|
||||||
foreach($data as $alias => $aggregatorData) {
|
foreach($data as $alias => $aggregatorData) {
|
||||||
if ($aggregatorData['order']> 0){
|
if ($aggregatorData['enabled'] === true){
|
||||||
yield $alias => $this->getAggregator($alias);
|
yield $alias => $this->getAggregator($alias);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -384,13 +384,14 @@ class ExportManager
|
|||||||
|
|
||||||
private function handleAggregators(ExportInterface $export, QueryBuilder $qb, $data)
|
private function handleAggregators(ExportInterface $export, QueryBuilder $qb, $data)
|
||||||
{
|
{
|
||||||
$aggregators = $this->getAggregatorsApplyingOn($export->supportsModifiers());
|
//$aggregators = $this->getAggregatorsApplyingOn($export->supportsModifiers());
|
||||||
|
$aggregators = $this->retrieveUsedAggregators($data);
|
||||||
|
|
||||||
foreach ($aggregators as $alias => $aggregator) {
|
foreach ($aggregators as $alias => $aggregator) {
|
||||||
$formData = $data[$alias];
|
$formData = $data[$alias];
|
||||||
if ($formData['order'] >= 0) {
|
//if ($formData['order'] >= 0) {
|
||||||
$aggregator->alterQuery($qb, $formData['form']);
|
$aggregator->alterQuery($qb, $formData['form']);
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ class ExportType extends AbstractType
|
|||||||
|
|
||||||
$builder->add($aggregatorBuilder);
|
$builder->add($aggregatorBuilder);
|
||||||
|
|
||||||
$builder->add('formatter', PickFormatterType::class, array(
|
$builder->add('pick_formatter', PickFormatterType::class, array(
|
||||||
'export_alias' => $options['export_alias']
|
'export_alias' => $options['export_alias']
|
||||||
));
|
));
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
<h2>{{ 'Formatter'| trans }}</h2>
|
<h2>{{ 'Formatter'| trans }}</h2>
|
||||||
{{ form_row(form.children.export.children.formatter.children.alias) }}
|
{{ form_row(form.children.export.children.pick_formatter.children.alias) }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p>{{ form_widget(form.submit, { 'attr' : { 'class' : 'sc-button btn-action' } } ) }}</p>
|
<p>{{ form_widget(form.submit, { 'attr' : { 'class' : 'sc-button btn-action' } } ) }}</p>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user