, * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ namespace Chill\EventBundle\Controller; use Chill\EventBundle\Entity\Participation; use Chill\EventBundle\Form\Type\PickEventType; use Chill\MainBundle\Pagination\PaginatorFactory; 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 Chill\EventBundle\Security\Authorization\EventVoter; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Privacy\PrivacyEvent; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Chill\PersonBundle\Form\Type\PickPersonType; use Chill\EventBundle\Entity\Event; use Chill\EventBundle\Form\EventType; use Symfony\Component\Security\Core\Role\Role; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Chill\MainBundle\Entity\Center; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Translation\TranslatorInterface; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; /** * Event controller. * */ class EventController extends Controller { /** * @var EventDispatcherInterface */ protected $eventDispatcher; /** * @var AuthorizationHelper */ protected $authorizationHelper; /** * @var FormFactoryInterface */ protected $formFactoryInterface; /** * @var TranslatorInterface */ protected $translator; /** * @var PaginatorFactory */ protected $paginator; /** * EventController constructor. * * @param EventDispatcherInterface $eventDispatcher * @param AuthorizationHelper $authorizationHelper * @param FormFactoryInterface $formFactoryInterface * @param TranslatorInterface $translator * @param PaginatorFactory $paginator */ 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; } public function mostRecentIndexAction() { return $this->redirectToRoute('chill_main_search', array( 'q' => '@event' )); } /** * 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', array( 'center_id' => $centers[0]->getId() )); } $form = $this->formFactoryInterface ->createNamedBuilder(null, FormType::class, null, array( 'csrf_protection' => false )) ->setMethod('GET') ->setAction( $this->generateUrl('chill_event__event_new')) ->add('center_id', EntityType::class, array( 'class' => Center::class, 'choices' => $centers, 'placeholder' => '', 'label' => 'To which centre should the event be associated ?' )) ->add('submit', SubmitType::class, array( 'label' => 'Next step' )) ->getForm(); return $this->render('ChillEventBundle:Event:newPickCenter.html.twig', array( 'form' => $form->createView() )); } /** * 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, array( 'method' => 'POST', 'center' => $entity->getCenter(), 'role' => new Role('CHILL_EVENT_CREATE') )); $form->add('submit', SubmitType::class, array('label' => 'Create')); return $form; } /** * Displays a form to create a new Event entity. * @param Center $center * @param Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function newAction(Center $center = null, Request $request) { if ($center === null) { $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', array('event_id' => $entity->getId()))); } return $this->render('ChillEventBundle:Event:new.html.twig', array( 'entity' => $entity, 'form' => $form->createView(), )); } /** * Finds and displays a Event entity. * * @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 $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', array( 'event' => $event, 'form_add_participation_by_person' => $addParticipationByPersonForm->createView(), 'form_export' => $exportParticipationsList['form']->createView() )); } /** * create a form to add a participation with a person * * @param Event $event * @return \Symfony\Component\Form\FormInterface */ protected function createAddParticipationByPersonForm(Event $event) { /* @var $builder \Symfony\Component\Form\FormBuilderInterface */ $builder = $this ->get('form.factory') ->createNamedBuilder( null, FormType::class, null, array( 'method' => 'GET', 'action' => $this->generateUrl('chill_event_participation_new'), 'csrf_protection' => false )) ; $builder->add('person_id', PickPersonType::class, array( 'role' => new Role('CHILL_EVENT_CREATE'), 'centers' => $event->getCenter() )); $builder->add('event_id', HiddenType::class, array( 'data' => $event->getId() )); $builder->add('submit', SubmitType::class, array( 'label' => 'Add a participation' )); return $builder->getForm(); } /** * 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', array( 'entity' => $entity, 'edit_form' => $editForm->createView(), )); } /** * Creates a form to edit a Event entity. * * @param Event $entity * @return \Symfony\Component\Form\FormInterface */ private function createEditForm(Event $entity) { $form = $this->createForm(EventType::class, $entity, array( 'action' => $this->generateUrl('chill_event__event_update', array('event_id' => $entity->getId())), 'method' => 'PUT', 'center' => $entity->getCenter(), 'role' => new Role('CHILL_EVENT_CREATE') )); $form->remove('center'); $form->add('submit', SubmitType::class, array('label' => 'Update')); return $form; } /** * Edits an existing Event entity. * * @param Request $request * @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', array('event_id' => $event_id))); } return $this->render('ChillEventBundle:Event:edit.html.twig', array( 'entity' => $entity, 'edit_form' => $editForm->createView(), )); } /** * List events subscriptions for a person * * @param $person_id * @return \Symfony\Component\HttpFoundation\Response * @throws \Doctrine\ORM\NonUniqueResultException */ public function listByPersonAction($person_id) { $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); if ($person === NULL) { 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, array( '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', array( 'participations' => $participations, 'person' => $person, 'paginator' => $paginator, 'form_add_event_participation_by_person' => $addEventParticipationByPersonForm->createView() )); } /** * create a form to add a participation with an event * * @param Person $person * @return \Symfony\Component\Form\FormInterface */ protected function createAddEventParticipationByPersonForm(Person $person) { /* @var $builder \Symfony\Component\Form\FormBuilderInterface */ $builder = $this ->get('form.factory') ->createNamedBuilder( null, FormType::class, null, array( 'method' => 'GET', 'action' => $this->generateUrl('chill_event_participation_new'), 'csrf_protection' => false )) ; $builder->add('event_id', PickEventType::class, array( 'role' => new Role('CHILL_EVENT_CREATE'), 'centers' => $person->getCenter() )); $builder->add('person_id', HiddenType::class, array( 'data' => $person->getId() )); $builder->add('return_path', HiddenType::class, array( 'data' => $this->generateUrl('chill_event__list_by_person', array( 'person_id' => $person->getId() )) )); $builder->add('submit', SubmitType::class, array( 'label' => 'Subscribe an event' )); return $builder->getForm(); } /** * @param Event $event * @param Request $request * @return array * @throws \PhpOffice\PhpSpreadsheet\Exception */ 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(function() use ($writer) { $writer->save('php://output'); }); return [ 'form' => $form, 'response' => $response ]; } 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(); } /** * @param Event $event * @return Spreadsheet * @throws \PhpOffice\PhpSpreadsheet\Exception */ 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; } /** * @param $event_id * @param Request $request * @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() ]); } /** * @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() ; } }