phpSpreadsheet export participations list for an event

This commit is contained in:
Tchama 2019-07-17 17:30:46 +02:00
parent c26b4f1ae7
commit 59668f45a2
5 changed files with 220 additions and 9 deletions

View File

@ -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

View File

@ -46,4 +46,4 @@ chill_event__event_export_participations:
defaults: { _controller: "ChillEventBundle:Event:exportParticipations" }
requirements:
event_id: \d+
methods: [ GET ]
methods: [ GET, POST ]

View File

@ -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

View File

@ -1,3 +1,88 @@
{% for participation in event.participations %}
{{ dump(participation.person.firstname ~ ' ' ~ participation.person.lastname) }}
{% endfor %}
{% extends 'ChillEventBundle::layout.html.twig' %}
{% block title 'Event : %label%'|trans({ '%label%' : event.name } ) %}
{% block event_content -%}
<table>
{% for participation in event.participations %}
<tr><td>
{{ participation.person.id }}
</td><td>
{{ participation.person.firstname }}
</td><td>
{{ participation.person.lastname }}
</td><td>
{{ participation.person.email }}
</td></tr>
{% endfor %}
</table><br>
<style>
.input-group {
position: relative;
width: 100%;
display: -ms-flexbox; display: flex;
-ms-flex-wrap: wrap; flex-wrap: wrap;
-ms-flex-align: stretch; align-items: stretch;
}
.custom-select {
padding: .375rem 1.75rem .375rem .75rem;
background: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 4 5'%3e%3cpath fill='%23343a40' d='M2 0L0 2h4zm0 5L0 3h4z'/%3e%3c/svg%3e")
no-repeat right .75rem center/8px 10px;
border-radius: .25rem;
-webkit-appearance: none; -moz-appearance: none; appearance: none;
/*
display: inline-block;
width: 100%; height: calc(1.5em + .75rem + 2px);
font-size: 1rem; font-weight: 400; line-height: 1.5;
vertical-align: middle;
color: #495057; background-color: #fff;
border: 1px solid #ced4da;
*/
}
.input-group > .custom-select {
position: relative;
width: 1%;
margin-bottom: 0;
-ms-flex: 1 1 auto; flex: 1 1 auto;
}
.input-group > .custom-select:not(:last-child) {
border-top-right-radius: 0; border-bottom-right-radius: 0;
}
.input-group > .input-group-append > .btn {
border-top-left-radius: 0; border-bottom-left-radius: 0;
}
.input-group-append {
display: -ms-flexbox; display: flex; margin-left: -1px;
}
.input-group-append .btn {
position: relative; z-index: 2;
}
.btn {
display: inline-block;
text-align: center; vertical-align: middle;
padding: .375rem .75rem;
font-weight: 400; font-size: 1rem; line-height: 1.5;
color: #212529; background-color: transparent;
border: 1px solid transparent; border-radius: .25rem;
-webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none;
transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
.btn-outline-secondary {
color: #6c757d; border-color: #6c757d;
}
</style>
{{ form_start(form) }}
{{ form_errors(form) }}
<div class="input-group">
{{ form_widget(form.format) }}
<div class="input-group-append">
{{ form_widget(form.submit) }}
</div>
</div>
{{ form_end(form) }}
{% endblock %}

View File

@ -21,7 +21,8 @@
}
],
"require": {
"chill-project/person": "~1.5"
"chill-project/person": "~1.5",
"phpoffice/phpspreadsheet": "~1.8.2"
},
"require-dev": {
},