EventController.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Muzich\UserBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
  5. class EventController extends Controller
  6. {
  7. protected $event;
  8. public function infoBarAction()
  9. {
  10. $events = $this->getDoctrine()->getRepository('MuzichCoreBundle:Event')
  11. ->getEvents($this->getUserId())
  12. ;
  13. return $this->render('MuzichUserBundle:Info:bar.html.twig', array(
  14. 'events' => $events
  15. ));
  16. }
  17. public function viewElementsAction($event_id)
  18. {
  19. if (($response = $this->mustBeConnected()))
  20. {
  21. return $response;
  22. }
  23. if (!($event = $this->getDoctrine()->getRepository('MuzichCoreBundle:Event')
  24. ->findOneById($event_id)))
  25. {
  26. if ($this->getRequest()->isXmlHttpRequest())
  27. {
  28. return $this->jsonResponse(array(
  29. 'status' => 'error',
  30. 'errors' => array('NotFound')
  31. ));
  32. }
  33. return $this->redirect($this->generateUrl('index'));
  34. }
  35. if ($event->getUser()->getId() != $this->getUserId())
  36. {
  37. if ($this->getRequest()->isXmlHttpRequest())
  38. {
  39. return $this->jsonResponse(array(
  40. 'status' => 'error',
  41. 'errors' => array('NotAllowed')
  42. ));
  43. }
  44. throw $this->createNotFoundException('Ressource ajax uniquement.');
  45. }
  46. // A partir d'ici on a ce qu'il faut.
  47. // On modifie l'Element Searcher en lui donnat les ids correspondant a l'event
  48. $user = $this->getUser();
  49. $es = $this->getNewElementSearcher();
  50. $es->setNoTags();
  51. $es->setIds($event->getIds());
  52. $es->setIdsDisplay($event->getType());
  53. $this->setElementSearcherParams($es->getParams(), $user->getPersonalHash($event->getId()));
  54. $elements = $es->getElements($this->getDoctrine(), $this->getUserId());
  55. return $this->render('MuzichUserBundle:Event:elements.html.twig', array(
  56. 'elements' => $elements,
  57. 'last_element_id' => $elements[count($elements)-1]->getId(),
  58. 'event' => $event
  59. ));
  60. }
  61. public function userCanAccessToThisEvent($event_id)
  62. {
  63. if (!($this->event = $this->getDoctrine()->getRepository('MuzichCoreBundle:Event')
  64. ->findOneById($event_id)))
  65. {
  66. throw $this->createNotFoundException();
  67. }
  68. if ($this->event->getUser()->getId() != $this->getUserId())
  69. {
  70. throw $this->createNotFoundException();
  71. }
  72. }
  73. public function userUseCorrectToken($token)
  74. {
  75. if ($this->getUser()->getPersonalHash($this->event->getId()) != $token)
  76. {
  77. throw new AccessDeniedException();
  78. }
  79. }
  80. public function deleteAction($event_id, $token)
  81. {
  82. $this->userCanAccessToThisEvent($event_id);
  83. $this->userUseCorrectToken($token);
  84. $this->getEntityManager()->remove($this->event);
  85. $this->flush();
  86. return $this->redirect($this->generateUrl('home'));
  87. }
  88. }