chill-bundles/Controller/EventController.php
Marc Ducobu 2cb4ee8bb9 Merging
2016-03-23 18:22:17 +01:00

225 lines
6.0 KiB
PHP

<?php
namespace Chill\EventBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Chill\EventBundle\Entity\Event;
use Chill\EventBundle\Form\EventType;
/**
* Event controller.
*
*/
class EventController extends Controller
{
/**
* Lists all Event entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('ChillEventBundle:Event')->findAll();
return $this->render('ChillEventBundle:Event:index.html.twig', array(
'entities' => $entities,
));
}
/**
* Creates a new Event entity.
*
*/
public function createAction(Request $request)
{
$entity = new Event();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('event_show', array('id' => $entity->getId())));
}
return $this->render('ChillEventBundle:Event:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Creates a form to create a Event entity.
*
* @param Event $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Event $entity)
{
$form = $this->createForm(EventType::class, $entity, array(
'action' => $this->generateUrl('event_create'),
'method' => 'POST'
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new Event entity.
*
*/
public function newAction()
{
$entity = new Event();
$form = $this->createCreateForm($entity);
return $this->render('ChillEventBundle:Event:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Finds and displays a Event entity.
*
*/
public function showAction($event_id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChillEventBundle:Event')->find($event_id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$this->denyAccessUnlessGranted('CHILL_EVENT_SEE_DETAILS', $entity,
"You are not allowed to see details on this event");
return $this->render('ChillEventBundle:Event:show.html.twig', array(
'event' => $entity
));
}
/**
* Displays a form to edit an existing Event entity.
*
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChillEventBundle:Event')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return $this->render('ChillEventBundle:Event:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Creates a form to edit a Event entity.
*
* @param Event $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Event $entity)
{
$form = $this->createForm(new EventType(), $entity, array(
'action' => $this->generateUrl('event_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing Event entity.
*
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChillEventBundle:Event')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('event_edit', array('id' => $id)));
}
return $this->render('ChillEventBundle:Event:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a Event entity.
*
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ChillEventBundle:Event')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('event'));
}
/**
* Creates a form to delete a Event entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('event_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
}