678 lines
21 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
/**
* Chill is a software for social workers.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\EventBundle\Controller;
use Chill\EventBundle\Entity\Event;
use Chill\EventBundle\Entity\Participation;
use Chill\EventBundle\Form\EventType;
use Chill\EventBundle\Form\Type\PickEventType;
use Chill\EventBundle\Security\Authorization\EventVoter;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Form\Type\PickPersonType;
use Chill\PersonBundle\Privacy\PrivacyEvent;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Csv;
use PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Translation\TranslatorInterface;
use function count;
/**
* Class EventController.
*/
class EventController extends AbstractController
{
/**
* @var AuthorizationHelper
*/
protected $authorizationHelper;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var FormFactoryInterface
*/
protected $formFactoryInterface;
/**
* @var PaginatorFactory
*/
protected $paginator;
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* EventController constructor.
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
AuthorizationHelper $authorizationHelper,
FormFactoryInterface $formFactoryInterface,
TranslatorInterface $translator,
PaginatorFactory $paginator
) {
$this->eventDispatcher = $eventDispatcher;
$this->authorizationHelper = $authorizationHelper;
$this->formFactoryInterface = $formFactoryInterface;
$this->translator = $translator;
$this->paginator = $paginator;
}
/**
* @param $event_id
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function deleteAction($event_id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$event = $em->getRepository('ChillEventBundle:Event')->findOneBy([
'id' => $event_id,
]);
if (!$event) {
throw $this->createNotFoundException('Unable to find this event.');
}
/** @var array $participations */
$participations = $event->getParticipations();
$form = $this->createDeleteForm($event_id);
if ($request->getMethod() === Request::METHOD_DELETE) {
$form->handleRequest($request);
if ($form->isValid()) {
foreach ($participations as $participation) {
$em->remove($participation);
}
$em->remove($event);
$em->flush();
$this->addFlash(
'success',
$this->get('translator')
->trans('The event has been sucessfully removed')
);
return $this->redirectToRoute('chill_main_search', [
'name' => 'event_regular',
'q' => '@event',
]);
}
}
return $this->render('ChillEventBundle:Event:confirm_delete.html.twig', [
'event_id' => $event->getId(),
'delete_form' => $form->createView(),
]);
}
/**
* Displays a form to edit an existing Event entity.
*
* @param $event_id
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function editAction($event_id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChillEventBundle:Event')->find($event_id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$editForm = $this->createEditForm($entity);
return $this->render('ChillEventBundle:Event:edit.html.twig', [
'entity' => $entity,
'edit_form' => $editForm->createView(),
]);
}
/**
* List events subscriptions for a person.
*
* @param $person_id
*
* @throws \Doctrine\ORM\NonUniqueResultException
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function listByPersonAction($person_id)
{
$em = $this->getDoctrine()->getManager();
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
if (null === $person) {
throw $this->createNotFoundException('Person not found');
}
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
$reachablesCircles = $this->authorizationHelper->getReachableCircles(
$this->getUser(),
new Role(EventVoter::SEE),
$person->getCenter()
);
$total = $em->getRepository('ChillEventBundle:Participation')->countByPerson($person_id);
$paginator = $this->paginator->create($total);
$participations = $em->getRepository('ChillEventBundle:Participation')->findByPersonInCircle(
$person_id,
$reachablesCircles,
$paginator->getCurrentPage()->getFirstItemNumber(),
$paginator->getItemsPerPage()
);
$privacyEvent = new PrivacyEvent($person, [
'element_class' => Participation::class,
'action' => 'list',
]);
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $privacyEvent);
$addEventParticipationByPersonForm = $this->createAddEventParticipationByPersonForm($person);
return $this->render('ChillEventBundle:Event:listByPerson.html.twig', [
'participations' => $participations,
'person' => $person,
'paginator' => $paginator,
'form_add_event_participation_by_person' => $addEventParticipationByPersonForm->createView(),
]);
}
public function mostRecentIndexAction()
{
return $this->redirectToRoute('chill_main_search', [
'q' => '@event',
]);
}
/**
* Displays a form to create a new Event entity.
*
* @param Center $center
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function newAction(?Center $center, Request $request)
{
if (null === $center) {
$center_id = $request->query->get('center_id');
$center = $this->getDoctrine()->getRepository(Center::class)->find($center_id);
}
$entity = new Event();
$entity->setCenter($center);
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
$this->addFlash('success', $this->get('translator')
->trans('The event was created'));
return $this->redirect($this->generateUrl('chill_event__event_show', ['event_id' => $entity->getId()]));
}
return $this->render('ChillEventBundle:Event:new.html.twig', [
'entity' => $entity,
'form' => $form->createView(),
]);
}
/**
* First step of new Event form.
*/
public function newPickCenterAction()
{
$role = new Role('CHILL_EVENT_CREATE');
/**
* @var Center $centers
*/
$centers = $this->authorizationHelper->getReachableCenters($this->getUser(), $role);
if (count($centers) === 1) {
return $this->redirectToRoute('chill_event__event_new', [
'center_id' => $centers[0]->getId(),
]);
}
$form = $this->formFactoryInterface
->createNamedBuilder(null, FormType::class, null, [
'csrf_protection' => false,
])
->setMethod('GET')
->setAction(
$this->generateUrl('chill_event__event_new')
)
->add('center_id', EntityType::class, [
'class' => Center::class,
'choices' => $centers,
'placeholder' => '',
'label' => 'To which centre should the event be associated ?',
])
->add('submit', SubmitType::class, [
'label' => 'Next step',
])
->getForm();
return $this->render('ChillEventBundle:Event:newPickCenter.html.twig', [
'form' => $form->createView(),
]);
}
/**
* Finds and displays a Event entity.
*
* @ParamConverter("event", options={"id": "event_id"})
*
* @throws \PhpOffice\PhpSpreadsheet\Exception
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function showAction(Event $event, Request $request)
{
if (!$event) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$this->denyAccessUnlessGranted(
'CHILL_EVENT_SEE_DETAILS',
$event,
'You are not allowed to see details on this event'
);
$addParticipationByPersonForm = $this->createAddParticipationByPersonForm($event);
$exportParticipationsList = $this->exportParticipationsList($event, $request);
if ($exportParticipationsList['response']) {
return $exportParticipationsList['response'];
}
return $this->render('ChillEventBundle:Event:show.html.twig', [
'event' => $event,
'form_add_participation_by_person' => $addParticipationByPersonForm->createView(),
'form_export' => $exportParticipationsList['form']->createView(),
]);
}
/**
* Edits an existing Event entity.
*
* @param $event_id
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function updateAction(Request $request, $event_id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChillEventBundle:Event')->find($event_id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
$this->addFlash('success', $this->get('translator')
->trans('The event was updated'));
return $this->redirect($this->generateUrl('chill_event__event_edit', ['event_id' => $event_id]));
}
return $this->render('ChillEventBundle:Event:edit.html.twig', [
'entity' => $entity,
'edit_form' => $editForm->createView(),
]);
}
/**
* create a form to add a participation with an event.
*
* @return \Symfony\Component\Form\FormInterface
*/
protected function createAddEventParticipationByPersonForm(Person $person)
{
/** @var \Symfony\Component\Form\FormBuilderInterface $builder */
$builder = $this
->get('form.factory')
->createNamedBuilder(
null,
FormType::class,
null,
[
'method' => 'GET',
'action' => $this->generateUrl('chill_event_participation_new'),
'csrf_protection' => false,
]
);
$builder->add('event_id', PickEventType::class, [
'role' => new Role('CHILL_EVENT_CREATE'),
'centers' => $person->getCenter(),
]);
$builder->add('person_id', HiddenType::class, [
'data' => $person->getId(),
]);
$builder->add('return_path', HiddenType::class, [
'data' => $this->generateUrl('chill_event__list_by_person', [
'person_id' => $person->getId(),
]),
]);
$builder->add(
'submit',
SubmitType::class,
[
'label' => 'Subscribe an event',
]
);
return $builder->getForm();
}
/**
* create a form to add a participation with a person.
*
* @return \Symfony\Component\Form\FormInterface
*/
protected function createAddParticipationByPersonForm(Event $event)
{
/** @var \Symfony\Component\Form\FormBuilderInterface $builder */
$builder = $this
->get('form.factory')
->createNamedBuilder(
null,
FormType::class,
null,
[
'method' => 'GET',
'action' => $this->generateUrl('chill_event_participation_new'),
'csrf_protection' => false,
]
);
$builder->add('person_id', PickPersonType::class, [
'role' => new Role('CHILL_EVENT_CREATE'),
'centers' => $event->getCenter(),
]);
$builder->add('event_id', HiddenType::class, [
'data' => $event->getId(),
]);
$builder->add(
'submit',
SubmitType::class,
[
'label' => 'Add a participation',
]
);
return $builder->getForm();
}
/**
* @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();
}
/**
* @throws \PhpOffice\PhpSpreadsheet\Exception
*
* @return Spreadsheet
*/
protected function createExportSpreadsheet(Event $event)
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$trans = $this->translator->getLocale();
$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()[$trans],
'A5' => 'Circle',
'B5' => $event->getCircle()->getName()[$trans],
'A6' => 'Moderator',
'B6' => $event->getModerator() ? $event->getModerator()->getUsernameCanonical() : null,
];
foreach ($headerValues as $k => $value) {
$sheet->setCellValue($k, $value);
}
$columnNames = ['id', 'firstname', 'lastname', 'role', 'status', 'email', 'phone', 'mobile'];
$columnLetter = 'A';
foreach ($columnNames as $columnName) {
$sheet->setCellValue($columnLetter . '8', $columnName);
++$columnLetter;
}
$columnValues = [];
foreach ($event->getParticipations() as $participation) {
/** @var Participation $participation */
$columnValues[] = [
$participation->getPerson()->getId(),
$participation->getPerson()->getFirstname(),
$participation->getPerson()->getLastname(),
$participation->getRole()->getName()[$trans],
$participation->getStatus()->getName()[$trans],
$participation->getPerson()->getEmail(),
$participation->getPerson()->getPhoneNumber(),
$participation->getPerson()->getMobileNumber(),
];
}
$i = 9;
foreach ($columnValues as $columnValue) {
$columnLetter = 'A';
foreach ($columnValue as $value) {
$sheet->setCellValue($columnLetter . $i, $value);
++$columnLetter;
}
++$i;
}
return $spreadsheet;
}
/**
* @throws \PhpOffice\PhpSpreadsheet\Exception
*
* @return array
*/
protected function exportParticipationsList(Event $event, Request $request)
{
$form = $this->createExportByFormatForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$format = $data['format'];
$filename = 'export_event' . $event->getId() . '_participations.' . $format;
$spreadsheet = $this->createExportSpreadsheet($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 ['form' => $form, 'response' => null];
}
$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(static function () use ($writer) {
$writer->save('php://output');
});
return ['form' => $form, 'response' => $response];
}
return ['form' => $form, 'response' => null];
}
/**
* Creates a form to create a Event entity.
*
* @param Event $entity The entity
*
* @return \Symfony\Component\Form\FormInterface
*/
private function createCreateForm(Event $entity)
{
$form = $this->createForm(EventType::class, $entity, [
'method' => 'POST',
'center' => $entity->getCenter(),
'role' => new Role('CHILL_EVENT_CREATE'),
]);
$form->add('submit', SubmitType::class, ['label' => 'Create']);
return $form;
}
/**
* @param $event_id
*
* @return \Symfony\Component\Form\FormInterface
*/
private function createDeleteForm($event_id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('chill_event__event_delete', [
'event_id' => $event_id,
]))
->setMethod('DELETE')
->add('submit', SubmitType::class, ['label' => 'Delete'])
->getForm();
}
/**
* Creates a form to edit a Event entity.
*
* @return \Symfony\Component\Form\FormInterface
*/
private function createEditForm(Event $entity)
{
$form = $this->createForm(EventType::class, $entity, [
'action' => $this->generateUrl('chill_event__event_update', ['event_id' => $entity->getId()]),
'method' => 'PUT',
'center' => $entity->getCenter(),
'role' => new Role('CHILL_EVENT_CREATE'),
]);
$form->remove('center');
$form->add('submit', SubmitType::class, ['label' => 'Update']);
return $form;
}
}