From be37d9d73ab0434bca8b8738a3a9e18e6f6688eb Mon Sep 17 00:00:00 2001 From: Tchama Date: Thu, 18 Jul 2019 12:12:59 +0200 Subject: [PATCH] improve phpspreadsheet participations list export --- Controller/EventController.php | 97 ++++++++++--------- Resources/config/routing/event.yml | 6 -- .../Event/exportParticipations.html.twig | 88 ----------------- Resources/views/Event/show.html.twig | 91 +++++++++++++++-- 4 files changed, 132 insertions(+), 150 deletions(-) delete mode 100644 Resources/views/Event/exportParticipations.html.twig diff --git a/Controller/EventController.php b/Controller/EventController.php index 74959cc69..666fb459e 100644 --- a/Controller/EventController.php +++ b/Controller/EventController.php @@ -197,31 +197,37 @@ class EventController extends Controller 'form' => $form->createView(), )); } - + /** * Finds and displays a Event entity. * - * @param $event_id + * @paramConverter("event", options={"id" = "event_id"}) + * @param Event $event + * @param Request $request * @return \Symfony\Component\HttpFoundation\Response + * @throws \PhpOffice\PhpSpreadsheet\Exception */ - public function showAction($event_id) + public function showAction(Event $event, Request $request) { - $em = $this->getDoctrine()->getManager(); - - $entity = $em->getRepository('ChillEventBundle:Event')->find($event_id); - - if (!$entity) { + if (!$event) { throw $this->createNotFoundException('Unable to find Event entity.'); } - $this->denyAccessUnlessGranted('CHILL_EVENT_SEE_DETAILS', $entity, + $this->denyAccessUnlessGranted('CHILL_EVENT_SEE_DETAILS', $event, "You are not allowed to see details on this event"); - $addParticipationByPersonForm = $this->createAddParticipationByPersonForm($entity); - + $addParticipationByPersonForm = $this->createAddParticipationByPersonForm($event); + + $exportParticipationsList = $this->exportParticipationsList($event, $request); + + if ($exportParticipationsList['response']) { + return $exportParticipationsList['response']; + } + return $this->render('ChillEventBundle:Event:show.html.twig', array( - 'event' => $entity, - 'form_add_participation_by_person' => $addParticipationByPersonForm->createView() + 'event' => $event, + 'form_add_participation_by_person' => $addParticipationByPersonForm->createView(), + 'form_export' => $exportParticipationsList['form']->createView() )); } @@ -450,40 +456,23 @@ class EventController extends Controller /** - * @paramConverter("event", options={"id" = "event_id"}) * @param Event $event * @param Request $request - * @return \Symfony\Component\HttpFoundation\Response + * @return array * @throws \PhpOffice\PhpSpreadsheet\Exception */ - public function exportParticipationsAction(Event $event, Request $request) + protected function exportParticipationsList(Event $event, Request $request) { - $builder = $this->createFormBuilder() - ->add('format', ChoiceType::class, [ - 'choices' => [ - 'xlsx' => 'xlsx', - 'ods' => 'ods', - 'csv' => 'csv' - ], - 'label' => false, - 'placeholder' => 'Select a format', - 'attr' => [ 'class' => 'custom-select' ] - ]) - ->add('submit', SubmitType::class, [ - 'label' => 'Export', - 'attr' => [ 'class' => 'btn btn-outline-secondary' ] - ]); - - $form = $builder->getForm(); + $form = $this->createExportByFormatForm(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $form->getData(); $format = $data['format']; - $filename = 'export_event_participations.' .$format; + $filename = 'export_event'. $event->getId() .'_participations.' .$format; - $spreadsheet = $this->createSpreadsheet($event); + $spreadsheet = $this->createExportSpreadsheet($event); switch ($format) { case 'ods': @@ -499,10 +488,7 @@ class EventController extends Controller $writer = new Csv($spreadsheet); break; default: - return $this->render('ChillEventBundle:Event:exportParticipations.html.twig', [ - 'event' => $event, - 'form' => $form->createView(), - ]); + return [ 'form' => $form, 'response' => null ]; } $response = new StreamedResponse(); @@ -515,13 +501,32 @@ class EventController extends Controller $writer->save('php://output'); }); - return $response; + return [ 'form' => $form, 'response' => $response ]; } - return $this->render('ChillEventBundle:Event:exportParticipations.html.twig', [ - 'event' => $event, - 'form' => $form->createView(), - ]); + return [ 'form' => $form, 'response' => null ]; + } + + /** + * @return \Symfony\Component\Form\FormInterface + */ + protected function createExportByFormatForm() + { + $builder = $this->createFormBuilder() + ->add('format', ChoiceType::class, [ + 'choices' => [ + 'xlsx' => 'xlsx', + 'ods' => 'ods', + 'csv' => 'csv' + ], + 'label' => false, + 'placeholder' => 'Select a format' + ]) + ->add('submit', SubmitType::class, [ + 'label' => 'Export' + ]); + + return $builder->getForm(); } /** @@ -529,7 +534,7 @@ class EventController extends Controller * @return Spreadsheet * @throws \PhpOffice\PhpSpreadsheet\Exception */ - protected function createSpreadsheet(Event $event) + protected function createExportSpreadsheet(Event $event) { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); @@ -544,7 +549,7 @@ class EventController extends Controller 'A4' => 'Type', 'B4' => $event->getType()->getName()['fr'], // TODO 'A5' => 'Circle', - 'B5' => $event->getCircle()->getName()['fr'], // TODO + 'B5' => $event->getCircle()->getName()['fr'], // TODO 'A6' => 'Moderator', 'B6' => $event->getModerator(), ]; diff --git a/Resources/config/routing/event.yml b/Resources/config/routing/event.yml index f1c52e335..840a7cda8 100644 --- a/Resources/config/routing/event.yml +++ b/Resources/config/routing/event.yml @@ -41,9 +41,3 @@ chill_event__list_by_person: defaults: { _controller: "ChillEventBundle:Event:listByPerson" } methods: [ GET ] -chill_event__event_export_participations: - path: /{event_id}/export/participations - defaults: { _controller: "ChillEventBundle:Event:exportParticipations" } - requirements: - event_id: \d+ - methods: [ GET, POST ] diff --git a/Resources/views/Event/exportParticipations.html.twig b/Resources/views/Event/exportParticipations.html.twig deleted file mode 100644 index 4623d8baf..000000000 --- a/Resources/views/Event/exportParticipations.html.twig +++ /dev/null @@ -1,88 +0,0 @@ -{% extends 'ChillEventBundle::layout.html.twig' %} - -{% block title 'Event : %label%'|trans({ '%label%' : event.name } ) %} - -{% block event_content -%} - - - {% for participation in event.participations %} - - {% endfor %} -
- {{ participation.person.id }} - - {{ participation.person.firstname }} - - {{ participation.person.lastname }} - - {{ participation.person.email }} -

- - - - - {{ form_start(form) }} - {{ form_errors(form) }} -
- {{ form_widget(form.format) }} -
- {{ form_widget(form.submit) }} -
-
- {{ form_end(form) }} - -{% endblock %} diff --git a/Resources/views/Event/show.html.twig b/Resources/views/Event/show.html.twig index 3e5ba1d55..af881fbcc 100644 --- a/Resources/views/Event/show.html.twig +++ b/Resources/views/Event/show.html.twig @@ -104,21 +104,92 @@ - -
- {{ form_start(form_add_participation_by_person) }} - {{ form_widget(form_add_participation_by_person.person_id, { 'attr' : { 'style' : 'width: 25em; display:inline-block; ' } } ) }} - {{ form_widget(form_add_participation_by_person.submit, { 'attr' : { 'class' : 'sc-button bt-create' } } ) }} - {{ form_rest(form_add_participation_by_person) }} - {{ form_end(form_add_participation_by_person) }} + + + + +
+
+ {{ form_start(form_add_participation_by_person) }} +
+ {{ form_widget(form_add_participation_by_person.person_id, { 'attr' : { + 'class' : 'custom-select', + 'style': 'min-width: 15em; max-width: 20em; display: inline-block;' + }} ) }} +
+ {{ form_widget(form_add_participation_by_person.submit, { 'attr' : { 'class' : 'sc-button bt-create' } } ) }} +
+
+ {{ form_rest(form_add_participation_by_person) }} + {{ form_end(form_add_participation_by_person) }} +
+ +
+ {{ form_start(form_export) }} +
+ {{ form_widget(form_export.format, { 'attr' : { 'class': 'custom-select' } }) }} +
+ {{ form_widget(form_export.submit, { 'attr' : { 'class': 'sc-button bt-save' } }) }} +
+ +
+ {{ form_rest(form_export) }} + {{ form_end(form_export) }} +
{{ chill_delegated_block('block_footer_show', { 'event': event }) }}
- + {% endblock %}