[feature] allow to edit multiple participations at once

ref #7
This commit is contained in:
2016-04-12 22:40:11 +02:00
parent c1b9069138
commit 78c53fe7b0
6 changed files with 220 additions and 3 deletions

View File

@@ -526,4 +526,124 @@ class ParticipationController extends Controller
return $form;
}
/**
* show a form to edit multiple participation for the same event.
*
* @param int $event_id
*/
public function editMultipleAction($event_id)
{
$event = $this->getDoctrine()->getRepository('ChillEventBundle:Event')
->find($event_id);
if ($event === null) {
throw $this->createNotFoundException("The event with id $event_id is not found");
}
// check for ACL, on Event level and on Participation Level
$this->denyAccessUnlessGranted('CHILL_EVENT_SEE', $event, "You are not allowed "
. "to see this event");
foreach ($event->getParticipations() as $participation) {
$this->denyAccessUnlessGranted(ParticipationVoter::UPDATE, $participation,
"You are not allowed to update participation with id ".$participation->getId());
}
switch ($event->getParticipations()->count()) {
case 0:
// if there aren't any participation, redirect to the 'show' view with an add flash
$this->addFlash('warning', $this->get('translator')
->trans( "There are no participation to edit for this event"));
return $this->redirectToRoute('chill_event__event_show',
array('event_id' => $event->getId()));
case 1:
// redirect to the form for a single participation
return $this->redirectToRoute('chill_event_participation_edit', array(
'participation_id' => $event->getParticipations()->first()->getId()
));
}
$form = $this->createEditFormMultiple($event->getParticipations(), $event);
return $this->render('ChillEventBundle:Participation:edit-multiple.html.twig', array(
'event' => $event,
'participations' => $event->getParticipations(),
'form' => $form->createView()
));
}
public function updateMultipleAction($event_id, Request $request)
{
/* @var $event \Chill\EventBundle\Entity\Event */
$event = $this->getDoctrine()->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, "You are not allowed "
. "to see this event");
foreach ($event->getParticipations() as $participation) {
$this->denyAccessUnlessGranted(ParticipationVoter::UPDATE, $participation,
"You are not allowed to update participation with id ".$participation->getId());
}
$form = $this->createEditFormMultiple($event->getParticipations(), $event);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', $this->get('translator')->trans("The participations "
. "have been successfully updated."));
return $this->redirectToRoute('chill_event__event_show',
array('event_id' => $event->getId()));
}
return $this->render('ChillEventBundle:Participation:edit-multiple.html.twig', array(
'event' => $event,
'participations' => $event->getParticipations(),
'form' => $form->createView()
));
}
/**
*
* @param \Doctrine\Common\Collections\Collectionn $participations contains object of Participation type
* @param \Chill\EventBundle\Entity\Event $event
* @return \Symfony\Component\Form\FormInterface
*/
protected function createEditFormMultiple(
\Doctrine\Common\Collections\Collection $participations,
\Chill\EventBundle\Entity\Event $event
) {
$form = $this->createForm(\Symfony\Component\Form\Extension\Core\Type\FormType::class,
array('participations' => $participations), array(
'method' => 'POST',
'action' => $this->generateUrl('chill_event_participation_update_multiple', array(
'event_id' => $event->getId()
))
));
$form->add('participations', CollectionType::class, array(
'entry_type' => ParticipationType::class,
'entry_options' => array(
'event_type' => $event->getType()
),
)
);
$form->add('submit', SubmitType::class, array(
'label' => 'Update'
));
return $form;
}
}