add a step to pick center before export

This commit is contained in:
Julien Fastré 2016-01-23 01:19:06 +01:00
parent fa246b37b6
commit 8eba8dca24
4 changed files with 186 additions and 3 deletions

View File

@ -27,6 +27,7 @@ use Symfony\Component\HttpFoundation\Request;
use Chill\MainBundle\Form\Type\Export\ExportType;
use Chill\MainBundle\Form\Type\Export\FormatterType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Chill\MainBundle\Form\Type\Export\PickCenterType;
/**
@ -71,9 +72,11 @@ class ExportController extends Controller
*/
public function newAction(Request $request, $alias)
{
$step = $request->query->getAlpha('step', 'export');
$step = $request->query->getAlpha('step', 'centers');
switch ($step) {
case 'centers':
return $this->selectCentersStep($request, $alias);
case 'export':
return $this->exportFormStep($request, $alias);
break;
@ -88,6 +91,40 @@ class ExportController extends Controller
}
}
public function selectCentersStep(Request $request, $alias)
{
$exportManager = $this->get('chill.main.export_manager');
$form = $this->createCreateFormExport($alias, 'centers');
$export = $exportManager->getExport($alias);
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$this->get('logger')->debug('form centers is valid', array(
'location' => __METHOD__));
$data = $form->getData();
$this->get('session')->set('centers_step_raw',
$request->request->all());
$this->get('session')->set('centers_step', $data);
return $this->redirectToRoute('chill_main_export_new', array(
'step' => $this->getNextStep('centers'),
'alias' => $alias
));
}
}
return $this->render('ChillMainBundle:Export:new_centers_step.html.twig',
array(
'form' => $form->createView(),
'export' => $export
));
}
/**
* Render the export form
*
@ -157,6 +194,12 @@ class ExportController extends Controller
'csrf_protection' => $isGenerate ? false : true,
));
if ($step === 'centers') {
$builder->add('centers', PickCenterType::class, array(
'export_alias' => $alias
));
}
if ($step === 'export' or $step === 'generate_export') {
$builder->add('export', ExportType::class, array(
'export_alias' => $alias,
@ -195,11 +238,13 @@ class ExportController extends Controller
private function getNextStep($step, $reverse = false)
{
switch($step) {
case 'export':
case 'centers':
if ($reverse !== false) {
throw new \LogicException("there is no step before 'export'");
}
return $reverse ? 'export' : 'formatter';
return 'export';
case 'export':
return $reverse ? 'centers' : 'formatter';
case 'formatter' :
return $reverse ? 'export' : 'generate';
case 'generate' :

View File

@ -0,0 +1,94 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Form\Type\Export;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use Chill\MainBundle\Entity\Center;
/**
* Pick centers amongst available centers for the user
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* @author Champs Libres <info@champs-libres.coop>
*/
class PickCenterType extends AbstractType
{
/**
*
* @var \Symfony\Component\Security\Core\User\UserInterface
*/
protected $user;
/**
*
* @var ExportManager
*/
protected $exportManager;
/**
*
* @var AuthorizationHelper
*/
protected $authorizationHelper;
public function __construct(TokenStorageInterface $tokenStorage,
ExportManager $exportManager, AuthorizationHelper $authorizationHelper)
{
$this->exportManager = $exportManager;
$this->user = $tokenStorage->getToken()->getUser();
$this->authorizationHelper = $authorizationHelper;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('export_alias')
;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$export = $this->exportManager->getExport($options['export_alias']);
$centers = $this->authorizationHelper->getReachableCenters($this->user,
$export->requiredRole());
$builder->add('c', EntityType::class, array(
'class' => 'ChillMainBundle:Center',
'query_builder' => function(EntityRepository $er) use ($centers) {
$qb = $er->createQueryBuilder('c');
$ids = array_map(function(Center $el) { return $el->getId(); },
$centers);
return $qb->where($qb->expr()->in('c.id', $ids));
},
'multiple' => true,
'expanded' => false,
'choice_label' => function(Center $c) { return $c->getName(); },
'data' => $centers
));
}
}

View File

@ -169,6 +169,15 @@ services:
tags:
- { name: form.type }
chill.main.form.pick_centers_type:
class: Chill\MainBundle\Form\Type\Export\PickCenterType
arguments:
- "@security.token_storage"
- "@chill.main.export_manager"
- "@chill.main.security.authorization.helper"
tags:
- { name: form.type }
chill.main.form.formatter_type:
class: Chill\MainBundle\Form\Type\Export\FormatterType
arguments:

View File

@ -0,0 +1,35 @@
{#
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
<info@champs-libres.coop> / <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
#}
{% extends "ChillMainBundle::layoutWithVerticalMenu.html.twig" %}
{% block title %}{{ export.title|trans }}{% endblock %}
{% block layout_wvm_content %}
<h1>{{ export.title|trans }}</h1>
<p>{{ export.description|trans }}</p>
{{ form_start(form) }}
<p>{{ form_widget(form.submit, { 'attr' : { 'class' : 'sc-button btn-action' } } ) }}</p>
{{ form_end(form) }}
{% endblock layout_wvm_content %}