mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-03 21:34:59 +00:00
phpSpreadsheet export participations list for an event
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user