diff --git a/Controller/EventController.php b/Controller/EventController.php index 18cd6b955..74959cc69 100644 --- a/Controller/EventController.php +++ b/Controller/EventController.php @@ -24,7 +24,12 @@ namespace Chill\EventBundle\Controller; use Chill\EventBundle\Entity\Participation; use Chill\EventBundle\Form\Type\PickEventType; -use Chill\EventBundle\Repository\ParticipationRepository; +use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Writer\Csv; +use PhpOffice\PhpSpreadsheet\Writer\Ods; +use PhpOffice\PhpSpreadsheet\Writer\Xlsx; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\HttpFoundation\StreamedResponse; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Chill\EventBundle\Security\Authorization\EventVoter; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; @@ -447,18 +452,135 @@ class EventController extends Controller /** * @paramConverter("event", options={"id" = "event_id"}) * @param Event $event + * @param Request $request * @return \Symfony\Component\HttpFoundation\Response + * @throws \PhpOffice\PhpSpreadsheet\Exception */ - public function exportParticipationsAction(Event $event) + public function exportParticipationsAction(Event $event, Request $request) { - dump($event); + $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->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) + { + $data = $form->getData(); + $format = $data['format']; + $filename = 'export_event_participations.' .$format; + + $spreadsheet = $this->createSpreadsheet($event); + + switch ($format) { + case 'ods': + $contentType = 'application/vnd.oasis.opendocument.spreadsheet'; + $writer = new Ods($spreadsheet); + break; + case 'xlsx': + $contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; + $writer = new Xlsx($spreadsheet); + break; + case 'csv': + $contentType = 'text/csv'; + $writer = new Csv($spreadsheet); + break; + default: + return $this->render('ChillEventBundle:Event:exportParticipations.html.twig', [ + 'event' => $event, + 'form' => $form->createView(), + ]); + } + + $response = new StreamedResponse(); + $response->headers->set('Content-Type', $contentType); + $response->headers->set('Content-Disposition', 'attachment;filename="'.$filename.'"'); + $response->setPrivate(); + $response->headers->addCacheControlDirective('no-cache', true); + $response->headers->addCacheControlDirective('must-revalidate', true); + $response->setCallback(function() use ($writer) { + $writer->save('php://output'); + }); + + return $response; + } return $this->render('ChillEventBundle:Event:exportParticipations.html.twig', [ - 'event' => $event, + 'event' => $event, + 'form' => $form->createView(), ]); } + /** + * @param Event $event + * @return Spreadsheet + * @throws \PhpOffice\PhpSpreadsheet\Exception + */ + protected function createSpreadsheet(Event $event) + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $headerValues = [ + 'A1' => 'Event', + 'B1' => $event->getId(), + 'A2' => 'Date', + 'B2' => $event->getDate()->format('d-m-Y H:i'), + 'A3' => 'Name', + 'B3' => $event->getName(), + 'A4' => 'Type', + 'B4' => $event->getType()->getName()['fr'], // TODO + 'A5' => 'Circle', + 'B5' => $event->getCircle()->getName()['fr'], // TODO + 'A6' => 'Moderator', + 'B6' => $event->getModerator(), + ]; + foreach ($headerValues as $k => $value) { + $sheet->setCellValue($k, $value); + } + + $columnNames = [ 'id', 'firstname', 'lastname', 'email' ]; + $columnLetter = 'A'; + foreach ($columnNames as $columnName) { + $sheet->setCellValue($columnLetter.'8', $columnName); + $columnLetter++; + } + + $columnValues = []; + foreach ($event->getParticipations() as $participation) { + $columnValues[] = [ + $participation->getPerson()->getId(), + $participation->getPerson()->getFirstname(), + $participation->getPerson()->getLastname(), + $participation->getPerson()->getEmail() + ]; + } + + $i = 9; + foreach ($columnValues as $columnValue) { + $columnLetter = 'A'; + foreach ($columnValue as $value) { + $sheet->setCellValue($columnLetter.$i, $value); + $columnLetter++; + } + $i++; + } + + return $spreadsheet; + } // pour aller plus loin diff --git a/Resources/config/routing/event.yml b/Resources/config/routing/event.yml index 79c3da414..f1c52e335 100644 --- a/Resources/config/routing/event.yml +++ b/Resources/config/routing/event.yml @@ -46,4 +46,4 @@ chill_event__event_export_participations: defaults: { _controller: "ChillEventBundle:Event:exportParticipations" } requirements: event_id: \d+ - methods: [ GET ] + methods: [ GET, POST ] diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index 53a79c3e1..44e77e20d 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -71,3 +71,6 @@ Subscribe an event: Ajouter un événement Pick an event: Choisir un événement Pick a type of event: Choisir un type d'événement Pick a moderator: Choisir un animateur + +Select a format: Choisir un format +Export: Exporter \ No newline at end of file diff --git a/Resources/views/Event/exportParticipations.html.twig b/Resources/views/Event/exportParticipations.html.twig index 20bfed6dc..4623d8baf 100644 --- a/Resources/views/Event/exportParticipations.html.twig +++ b/Resources/views/Event/exportParticipations.html.twig @@ -1,3 +1,88 @@ -{% for participation in event.participations %} - {{ dump(participation.person.firstname ~ ' ' ~ participation.person.lastname) }} -{% endfor %} \ No newline at end of file +{% 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/composer.json b/composer.json index b98b9fd00..caa75b7ea 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ } ], "require": { - "chill-project/person": "~1.5" + "chill-project/person": "~1.5", + "phpoffice/phpspreadsheet": "~1.8.2" }, "require-dev": { },