From 8eba8dca24e6bab24083ac75a575af7da09ee719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 23 Jan 2016 01:19:06 +0100 Subject: [PATCH] add a step to pick center before export --- Controller/ExportController.php | 51 +++++++++- Form/Type/Export/PickCenterType.php | 94 +++++++++++++++++++ Resources/config/services.yml | 9 ++ .../views/Export/new_centers_step.html.twig | 35 +++++++ 4 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 Form/Type/Export/PickCenterType.php create mode 100644 Resources/views/Export/new_centers_step.html.twig diff --git a/Controller/ExportController.php b/Controller/ExportController.php index 87c87e820..193569fdd 100644 --- a/Controller/ExportController.php +++ b/Controller/ExportController.php @@ -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' : diff --git a/Form/Type/Export/PickCenterType.php b/Form/Type/Export/PickCenterType.php new file mode 100644 index 000000000..102290ada --- /dev/null +++ b/Form/Type/Export/PickCenterType.php @@ -0,0 +1,94 @@ + + * + * 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 . + */ + +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é + * @author Champs Libres + */ +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 + )); + + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 6a30eece0..e3c408584 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -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: diff --git a/Resources/views/Export/new_centers_step.html.twig b/Resources/views/Export/new_centers_step.html.twig new file mode 100644 index 000000000..d8304fd1c --- /dev/null +++ b/Resources/views/Export/new_centers_step.html.twig @@ -0,0 +1,35 @@ +{# + * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, + / + * + * 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 . +#} + +{% extends "ChillMainBundle::layoutWithVerticalMenu.html.twig" %} + +{% block title %}{{ export.title|trans }}{% endblock %} + +{% block layout_wvm_content %} + +

{{ export.title|trans }}

+ +

{{ export.description|trans }}

+ + {{ form_start(form) }} + + +

{{ form_widget(form.submit, { 'attr' : { 'class' : 'sc-button btn-action' } } ) }}

+ {{ form_end(form) }} + +{% endblock layout_wvm_content %}