mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-14 06:14:23 +00:00
The participationController accept a new parameter : `persons_ids`, which must receive a comma-separated list of person ids. A participation will be create for all those peoples. The `new` and `create` actions does not allow to receive both `person_id` and `persons_ids`. Tests are added. ref #6
453 lines
16 KiB
PHP
453 lines
16 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;
|
|
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @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)
|
|
{
|
|
// test the request is correct
|
|
try {
|
|
$this->testRequest($request);
|
|
} catch (\RuntimeException $ex) {
|
|
$this->get('logger')->warning($ex->getMessage());
|
|
|
|
return (new Response())
|
|
->setStatusCode(Response::HTTP_BAD_REQUEST)
|
|
->setContent($ex->getMessage());
|
|
}
|
|
|
|
// forward to other action
|
|
$single = $request->query->has('person_id');
|
|
$multiple = $request->query->has('persons_ids');
|
|
|
|
if ($single === true) {
|
|
return $this->newSingle($request);
|
|
}
|
|
|
|
if ($multiple === true) {
|
|
|
|
return $this->newMultiple($request);
|
|
}
|
|
|
|
// at this point, we miss the required fields. Throw an error
|
|
return (new Response())
|
|
->setStatusCode(Response::HTTP_BAD_REQUEST)
|
|
->setContent("You must provide either 'person_id' or "
|
|
. "'persons_ids' argument in query");
|
|
}
|
|
|
|
protected function testRequest($request)
|
|
{
|
|
$single = $request->query->has('person_id');
|
|
$multiple = $request->query->has('persons_ids');
|
|
|
|
if ($single === true AND $multiple === true) {
|
|
// we are not allowed to have both person_id and persons_ids
|
|
throw new \RuntimeException("You are not allow to provide both 'person_id' and "
|
|
. "'persons_ids' simulaneously");
|
|
}
|
|
|
|
if ($multiple === true) {
|
|
$persons_ids = $request->query->get('persons_ids');
|
|
|
|
if (!preg_match('/^([0-9]{1,},){1,}[0-9]{1,}$/', $persons_ids)) {
|
|
throw new \RuntimeException("The persons_ids value should "
|
|
. "contains int separated by ','");
|
|
}
|
|
}
|
|
|
|
// check for event_id - this could be removed later
|
|
if ($request->query->has('event_id') === FALSE) {
|
|
throw new \RuntimeException("You must provide an event_id");
|
|
}
|
|
|
|
}
|
|
|
|
protected function newSingle(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
|
|
));
|
|
}
|
|
|
|
protected function newMultiple(Request $request)
|
|
{
|
|
$participations = $this->handleRequest($request, new Participation());
|
|
|
|
foreach ($participations as $participation) {
|
|
$this->denyAccessUnlessGranted(ParticipationVoter::CREATE,
|
|
$participation, 'The user is not allowed to create this participation');
|
|
}
|
|
|
|
$form = $this->createCreateFormMultiple($participations);
|
|
|
|
return $this->render('ChillEventBundle:Participation:new-multiple.html.twig', array(
|
|
'form' => $form->createView(),
|
|
'participations' => $participations
|
|
));
|
|
}
|
|
|
|
public function createAction(Request $request)
|
|
{
|
|
// test the request is correct
|
|
try {
|
|
$this->testRequest($request);
|
|
} catch (\RuntimeException $ex) {
|
|
$this->get('logger')->warning($ex->getMessage());
|
|
|
|
return (new Response())
|
|
->setStatusCode(Response::HTTP_BAD_REQUEST)
|
|
->setContent($ex->getMessage());
|
|
}
|
|
|
|
// forward to other action
|
|
$single = $request->query->has('person_id');
|
|
$multiple = $request->query->has('persons_ids');
|
|
|
|
if ($single === true) {
|
|
return $this->createSingle($request);
|
|
}
|
|
|
|
if ($multiple === true) {
|
|
|
|
return $this->createMultiple($request);
|
|
}
|
|
|
|
// at this point, we miss the required fields. Throw an error
|
|
return (new Response())
|
|
->setStatusCode(Response::HTTP_BAD_REQUEST)
|
|
->setContent("You must provide either 'person_id' or "
|
|
. "'persons_ids' argument in query");
|
|
}
|
|
|
|
public function createSingle(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
|
|
));
|
|
}
|
|
|
|
public function createMultiple(Request $request)
|
|
{
|
|
$participations = $this->handleRequest($request, new Participation());
|
|
|
|
foreach($participations as $participation) {
|
|
$this->denyAccessUnlessGranted(ParticipationVoter::CREATE,
|
|
$participation, 'The user is not allowed to create this participation');
|
|
}
|
|
|
|
$form = $this->createCreateFormMultiple($participations);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
$data = $form->getData();
|
|
|
|
foreach($data['participations'] as $participation) {
|
|
$em->persist($participation);
|
|
}
|
|
|
|
$em->flush();
|
|
|
|
$this->addFlash('success', $this->get('translator')->trans(
|
|
'The participations were created'
|
|
));
|
|
|
|
return $this->redirectToRoute('chill_event__event_show', array(
|
|
'event_id' => $participations[0]->getEvent()->getId()
|
|
));
|
|
}
|
|
|
|
return $this->render('ChillEventBundle:Participation:new.html.twig', array(
|
|
'form' => $form->createView(),
|
|
'participation' => $participation
|
|
));
|
|
}
|
|
|
|
/**
|
|
*
|
|
* Handle the request to adapt $participation.
|
|
*
|
|
* If the request is multiple, the $participation object is cloned.
|
|
* Limitations: the $participation should not be persisted.
|
|
*
|
|
* @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();
|
|
if ($em->contains($participation)) {
|
|
throw new \LogicException("The participation object should not be managed by "
|
|
. "the object manager using the method ".__METHOD__);
|
|
}
|
|
|
|
$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);
|
|
}
|
|
|
|
// this script should be able to handle multiple, so we translate
|
|
// single person_id in an array
|
|
$persons_ids = $request->query->has('person_id') ?
|
|
array($request->query->getInt('person_id', null)):
|
|
explode(',', $request->query->get('persons_ids'))
|
|
;
|
|
$participations = array();
|
|
|
|
foreach($persons_ids as $person_id) {
|
|
|
|
// clone if we have to reuse the $participation
|
|
$participation = count($persons_ids) > 1 ? clone $participation : $participation;
|
|
|
|
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);
|
|
}
|
|
|
|
$participations[] = $participation;
|
|
}
|
|
|
|
return count($participations) > 1 ? $participations : $participations[0];
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @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;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param array $participations
|
|
* @return type
|
|
*/
|
|
public function createCreateFormMultiple(array $participations)
|
|
{
|
|
$form = $this->createForm(\Symfony\Component\Form\Extension\Core\Type\FormType::class,
|
|
array('participations' => $participations), array(
|
|
'action' => $this->generateUrl('chill_event_participation_create', array(
|
|
'event_id' => $participations[0]->getEvent()->getId(),
|
|
'persons_ids' => implode(',', array_map(
|
|
function(Participation $p) { return $p->getPerson()->getId(); },
|
|
$participations))
|
|
)
|
|
)));
|
|
$form->add('participations', CollectionType::class, array(
|
|
'entry_type' => ParticipationType::class,
|
|
'entry_options' => array(
|
|
'event_type' => $participations[0]->getEvent()->getType()
|
|
),
|
|
)
|
|
);
|
|
|
|
$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;
|
|
}
|
|
|
|
}
|