chill-bundles/Controller/ParticipationController.php

152 lines
5.3 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 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
{
public function newAction(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('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;
}
}