chill-bundles/Controller/EventController.php
2019-05-02 19:27:31 +02:00

449 lines
14 KiB
PHP

<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2019, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
*
* 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 <http://www.gnu.org/licenses/>.
*/
namespace Chill\EventBundle\Controller;
use Chill\EventBundle\Entity\Participation;
use Chill\EventBundle\Form\Type\PickEventType;
use Chill\EventBundle\Repository\ParticipationRepository;
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;
/**
* Event controller.
*
*/
class EventController extends Controller
{
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var AuthorizationHelper
*/
protected $authorizationHelper;
/**
* @var FormFactoryInterface
*/
protected $formFactoryInterface;
/**
* EventController constructor.
*
* @param EventDispatcherInterface $eventDispatcher
* @param AuthorizationHelper $authorizationHelper
* @param FormFactoryInterface $formFactoryInterface
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
AuthorizationHelper $authorizationHelper,
FormFactoryInterface $formFactoryInterface
)
{
$this->eventDispatcher = $eventDispatcher;
$this->authorizationHelper = $authorizationHelper;
$this->formFactoryInterface = $formFactoryInterface;
}
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.
*
* @param $event_id
* @return \Symfony\Component\HttpFoundation\Response
*/
public function showAction($event_id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChillEventBundle:Event')->find($event_id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$this->denyAccessUnlessGranted('CHILL_EVENT_SEE_DETAILS', $entity,
"You are not allowed to see details on this event");
$addParticipationByPersonForm = $this->createAddParticipationByPersonForm($entity);
return $this->render('ChillEventBundle:Event:show.html.twig', array(
'event' => $entity,
'form_add_participation_by_person' => $addParticipationByPersonForm->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);
/**
* @var $paginatorFactory \Chill\MainBundle\Pagination\PaginatorFactory
*/
$paginatorFactory = $this->get('chill_main.paginator_factory');
$paginator = $paginatorFactory->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();
}
// pour aller plus loin
// TODO ?? inclure le type d'événement et la date dans les résultats de recherche (ex: on tape 'vis' --> il affiche les visites )
// TODO ?? exclure des résultats les événement déjà sélectionnés pour une personne. empêcher d'inscrire 2x une personne !
}