chill-bundles/Controller/ParticipationController.php

269 lines
9.6 KiB
PHP

<?php
/*
* Copyright (C) 2016 Champs-Libres <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 Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Chill\EventBundle\Entity\Participation;
use Chill\EventBundle\Form\ParticipationType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Chill\EventBundle\Security\Authorization\ParticipationVoter;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class ParticipationController extends Controller
{
/**
* Show a form to add a participation
*
* This function parse the person_id / persons_ids query argument
* and decide if it should process a single or multiple participation
*
* @param Request $request
*/
public function newAction(Request $request)
{
$single = $request->query->getInt('person_id', null);
$multiple = $request->query->get('persons_ids', null);
if ($single !== NULL AND $multiple !== NULL) {
// we are not allowed to have both person_id and persons_ids
return (new Response())
->setStatusCode(Response::HTTP_BAD_REQUEST)
->setContent("You are not allow to provide both 'person_id' and "
. "'persons_ids' simulaneously");
}
if ($single !== NULL) {
return $this->newSingleAction($request);
}
}
protected function newSingleAction(Request $request)
{
$participation = $this->handleRequest($request, new Participation());
$this->denyAccessUnlessGranted(ParticipationVoter::CREATE,
$participation, 'The user is not allowed to create this participation');
$form = $this->createCreateForm($participation);
return $this->render('ChillEventBundle:Participation:new.html.twig', array(
'form' => $form->createView(),
'participation' => $participation
));
}
public function createAction(Request $request)
{
$participation = $this->handleRequest($request, new Participation());
$this->denyAccessUnlessGranted(ParticipationVoter::CREATE,
$participation, 'The user is not allowed to create this participation');
$form = $this->createCreateForm($participation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($participation);
$em->flush();
$this->addFlash('success', $this->get('translator')->trans(
'The participation was created'
));
return $this->redirectToRoute('chill_event__event_show', array(
'event_id' => $participation->getEvent()->getId()
));
}
return $this->render('ChillEventBundle:Participation:new.html.twig', array(
'form' => $form->createView(),
'participation' => $participation
));
}
/**
*
* @param Request $request
* @param Participation $participation
* @return Participation
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if the event/person is not found
* @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException if the user does not have access to event/person
*/
protected function handleRequest(Request $request, Participation $participation)
{
$em = $this->getDoctrine()->getManager();
$event_id = $request->query->getInt('event_id', null);
if ($event_id !== NULL) {
$event = $em->getRepository('ChillEventBundle:Event')
->find($event_id);
if ($event === NULL) {
throw $this->createNotFoundException('The event with id '.$event_id.' is not found');
}
$this->denyAccessUnlessGranted('CHILL_EVENT_SEE', $event,
'The user is not allowed to see the event');
$participation->setEvent($event);
}
$person_id = $request->query->getInt('person_id', null);
if ($person_id !== NULL) {
$person = $em->getRepository('ChillPersonBundle:Person')
->find($person_id);
if ($person === NULL) {
throw $this->createNotFoundException('The person with id '.$person_id.' is not found');
}
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person,
'The user is not allowed to see the person');
$participation->setPerson($person);
}
return $participation;
}
/**
*
* @param Participation $participation
* @return \Symfony\Component\Form\FormInterface
*/
public function createCreateForm(Participation $participation)
{
$form = $this->createForm(ParticipationType::class, $participation, array(
'event_type' => $participation->getEvent()->getType(),
'action' => $this->generateUrl('chill_event_participation_create', array(
'event_id' => $participation->getEvent()->getId(),
'person_id' => $participation->getPerson()->getId()
))
));
$form->add('submit', SubmitType::class, array(
'label' => 'Create'
));
return $form;
}
/**
* show an edit form for the participation with the given id.
*
* @param int $participation_id
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if the participation is not found
* @throws \Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException if the user is not allowed to edit the participation
*/
public function editAction($participation_id)
{
/* @var $participation Participation */
$participation = $this->getDoctrine()->getManager()
->getRepository('ChillEventBundle:Participation')
->find($participation_id);
if ($participation === NULL) {
throw $this->createNotFoundException('The participation is not found');
}
$this->denyAccessUnlessGranted(ParticipationVoter::UPDATE, $participation,
'You are not allowed to edit this participation');
$form = $this->createEditForm($participation);
return $this->render('ChillEventBundle:Participation:edit.html.twig', array(
'form' => $form->createView(),
'participation' => $participation
));
}
public function updateAction($participation_id, Request $request)
{
/* @var $participation Participation */
$participation = $this->getDoctrine()->getManager()
->getRepository('ChillEventBundle:Participation')
->find($participation_id);
if ($participation === NULL) {
throw $this->createNotFoundException('The participation is not found');
}
$this->denyAccessUnlessGranted(ParticipationVoter::UPDATE, $participation,
'You are not allowed to edit this participation');
$form = $this->createEditForm($participation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->flush();
$this->addFlash('success', $this->get('translator')->trans(
'The participation was updated'
));
return $this->redirectToRoute('chill_event__event_show', array(
'event_id' => $participation->getEvent()->getId()
));
}
return $this->render('ChillEventBundle:Participation:edit.html.twig', array(
'form' => $form->createView(),
'participation' => $participation
));
}
/**
*
* @param Participation $participation
* @return \Symfony\Component\Form\FormInterface
*/
public function createEditForm(Participation $participation)
{
$form = $this->createForm(ParticipationType::class, $participation, array(
'event_type' => $participation->getEvent()->getType(),
'action' => $this->generateUrl('chill_event_participation_update', array(
'participation_id' => $participation->getId()
))
));
$form->add('submit', SubmitType::class, array(
'label' => 'Edit'
));
return $form;
}
}