fix the possibility to add multiple participation

ref #6
This commit is contained in:
2016-04-11 23:59:14 +02:00
parent fe8994d775
commit c1b9069138
6 changed files with 225 additions and 18 deletions

View File

@@ -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()
),
)
);