diff --git a/Controller/ParticipationController.php b/Controller/ParticipationController.php index 007a7825d..e2eaa3fa2 100644 --- a/Controller/ParticipationController.php +++ b/Controller/ParticipationController.php @@ -39,7 +39,8 @@ 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 + * and decide if it should process a single or multiple participation. Depending + * on this, the appropriate layout and form. * * @param Request $request */ @@ -76,7 +77,18 @@ class ParticipationController extends Controller . "'persons_ids' argument in query"); } - protected function testRequest($request) + /** + * + * Test that the query parameters are valid : + * + * - an `event_id` is existing ; + * - `person_id` and `persons_ids` are **not** both present ; + * - `persons_id` is correct (contains only numbers and a ','. + * + * @param Request $request + * @throws \RuntimeException if an error is detected + */ + protected function testRequest(Request $request) { $single = $request->query->has('person_id'); $multiple = $request->query->has('persons_ids'); @@ -103,6 +115,12 @@ class ParticipationController extends Controller } + /** + * Show a form with single participation. + * + * @param Request $request + * @return Response + */ protected function newSingle(Request $request) { $participation = $this->handleRequest($request, new Participation()); @@ -114,25 +132,84 @@ class ParticipationController extends Controller return $this->render('ChillEventBundle:Participation:new.html.twig', array( 'form' => $form->createView(), - 'participation' => $participation + 'participation' => $participation, + 'ignored_participations' => array() // this is required, see self::newMultiple )); } + /** + * Show a form with multiple participation. + * + * If a person is already participating on the event (if a participation with + * the same person is associated with the event), the participation is ignored. + * + * If all but one participation is ignored, the page show the same response + * than the newSingle function. + * + * If all participations must be ignored, an error is shown and the method redirects + * to the event 'show' view with an appropriate flash message. + * + * @param Request $request + * @return Response + */ protected function newMultiple(Request $request) { $participations = $this->handleRequest($request, new Participation()); - foreach ($participations as $participation) { + foreach ($participations as $i => $participation) { + // check for authorization $this->denyAccessUnlessGranted(ParticipationVoter::CREATE, $participation, 'The user is not allowed to create this participation'); + + // check that the user is not already in the event (computing only once) + /* @var $peopleParticipating \Doctrine\Common\Collections\ArrayCollection */ + $peopleParticipating = isset($peopleParticipating) ? $peopleParticipating : + $participation->getEvent()->getParticipations()->map( + function(Participation $p) { return $p->getPerson()->getId(); } + ); + + if ($peopleParticipating->contains($participation->getPerson()->getId())) { + $ignoredParticipations[] = $participation + ->getEvent()->getParticipations()->filter( + function (Participation $p) use ($participation) { + return $p->getPerson()->getId() === $participation->getPerson()->getId(); + } + )->first(); + } else { + $newParticipations[] = $participation; + } } - $form = $this->createCreateFormMultiple($participations); + // this is where the function redirect depending on valid participation - return $this->render('ChillEventBundle:Participation:new-multiple.html.twig', array( - 'form' => $form->createView(), - 'participations' => $participations - )); + if (!isset($newParticipations)) { + // if we do not have nay participants, redirect to event view + $this->addFlash('error', 'Any of the requested people may be added ' + . 'on the event: they are maybe already participating.'); + + return $this->redirectToRoute('chill_event_participation_new', array( + 'event_id' => $request->query->getInt('event_id', 0) + )); + } elseif (count($newParticipations) > 1) { + // if we have multiple participations, show a form with multiple participations + $form = $this->createCreateFormMultiple($newParticipations); + + return $this->render('ChillEventBundle:Participation:new-multiple.html.twig', array( + 'form' => $form->createView(), + 'participations' => $newParticipations, + 'ignored_participations' => isset($ignoredParticipations) ? $ignoredParticipations : array() + )); + } else { + // if we have only one participation, show the same form than for single participation + $form = $this->createCreateForm($participation); + + return $this->render('ChillEventBundle:Participation:new.html.twig', array( + 'form' => $form->createView(), + 'participation' => $participation, + 'ignored_participations' => isset($ignoredParticipations) ? $ignoredParticipations : array() + )); + + } } public function createAction(Request $request) @@ -338,7 +415,7 @@ class ParticipationController extends Controller $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(), + 'event_id' => current($participations)->getEvent()->getId(), 'persons_ids' => implode(',', array_map( function(Participation $p) { return $p->getPerson()->getId(); }, $participations)) @@ -347,7 +424,7 @@ class ParticipationController extends Controller $form->add('participations', CollectionType::class, array( 'entry_type' => ParticipationType::class, 'entry_options' => array( - 'event_type' => $participations[0]->getEvent()->getType() + 'event_type' => current($participations)->getEvent()->getType() ), ) ); diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index 67e992d7e..3d1d9c4ca 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -32,6 +32,8 @@ Back to the event: Retour à l'événement The participation was created: La participation a été créée The participation was updated: La participation a été mise à jour Participation Edit: Modifier une participation +'Any of the requested people may be added on the event: they are maybe already participating.': 'Aucune des personnes suggérées ne peut être ajoutée à l''événement: elles sont peut-être déjà inscrites comme participantes.' +'The following people have been ignored because they are already participating on the event': '{1} La personne suivante a été ignorée parce qu''elle participe déjà à l''événement | ]1,Inf] Les personnes suivantes ont été ignorées parce qu''elles participent déjà à l''événement' #search Event search: Recherche d'événements diff --git a/Resources/views/Participation/_ignored_participations.html.twig b/Resources/views/Participation/_ignored_participations.html.twig new file mode 100644 index 000000000..0d54dd624 --- /dev/null +++ b/Resources/views/Participation/_ignored_participations.html.twig @@ -0,0 +1,10 @@ +{% import 'ChillPersonBundle:Person:macro.html.twig' as person_macro %} + +{% if ignored_participations|length > 0 %} +
{% transchoice ignored_participations|length %}The following people have been ignored because they are already participating on the event{% endtranschoice %} :
+