VideothequeController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace AppBundle\Controller;
  3. use AppBundle\Entity\Commentaire;
  4. use AppBundle\Entity\Genre;
  5. use AppBundle\Entity\Realisateur;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use AppBundle\Entity\Film;
  12. use AppBundle\Form\FilmType;
  13. class VideothequeController extends Controller
  14. {
  15. /**
  16. * @Route("/", name="racine_test")
  17. */
  18. public function indexAction()
  19. {
  20. return $this->redirectToRoute('videotheque_liste');
  21. }
  22. /**
  23. * @Route("/liste", name="videotheque_liste")
  24. */
  25. public function listeAction()
  26. {
  27. $em = $this->getDoctrine()->getManager();
  28. $repo = $em->getRepository('AppBundle:Film');
  29. $listeFilms = $repo->findAll();
  30. return $this->render('@App/videotheque/liste.html.twig', array(
  31. 'listeFilms' => $listeFilms
  32. ));
  33. }
  34. /**
  35. * @Route("/liste-by/{id}", name="videotheque_listepargenre")
  36. */
  37. public function listeParGenreAction(\AppBundle\Entity\Genre $genre)
  38. {
  39. $em = $this->getDoctrine()->getManager();
  40. $films = $em->getRepository('AppBundle:Film')->findFilmWithGenre(array($genre->getName()));
  41. return $this->render('@App/videotheque/liste.html.twig', array(
  42. 'listeFilms' => $films,
  43. 'titre' => 'Films par catégorie : '.$genre->getName()
  44. ));
  45. }
  46. /**
  47. * @Route("/liste-by_real/{id}", name="videotheque_listeparreal")
  48. */
  49. public function listeParRealisateurAction(Realisateur $realisateur)
  50. {
  51. $em = $this->getDoctrine()->getManager();
  52. $films = $em->getRepository('AppBundle:Film')->findFilmWithReal(array($realisateur->getNomComplet()));
  53. return $this->render('@App/videotheque/liste.html.twig', array(
  54. 'listeFilms' => $films,
  55. 'titre' => 'Films par réalisateur : '.$realisateur->getNomComplet()
  56. ));
  57. }
  58. /**
  59. * @Route("/ajouter", name="videotheque_ajouter")
  60. */
  61. public function ajouterAction(Request $request)
  62. {
  63. $film = new Film;
  64. $film->setAuthered($this->getUser());
  65. $form = $this->createForm(FilmType::class, $film);
  66. $em = $this->getDoctrine()->getManager();
  67. $form->handleRequest($request);
  68. if ($form->isSubmitted() && $form->isValid())
  69. {
  70. $uniciteCollections = $this->get('film.unicite_collections');
  71. $film = $uniciteCollections->assureUniciteCollections($film);
  72. $em->persist($film);
  73. $em->flush();
  74. $this->addFlash('success', 'Le film a été ajouté');
  75. return $this->redirectToRoute('videotheque_voirfilm', array('id'=>$film->getId()));
  76. }
  77. return $this->render('@App/videotheque/ajouter.html.twig', array(
  78. 'form' => $form->createView(),
  79. ));
  80. }
  81. /**
  82. * @Route("/modifier/{id}", name="videotheque_modifier")
  83. */
  84. public function modifierAction(Request $request, Film $film)
  85. {
  86. $form = $this->createForm(FilmType::class, $film);
  87. $form->handleRequest($request);
  88. $em = $this->getDoctrine()->getManager();
  89. if ($form->isSubmitted() && $form->isValid())
  90. {
  91. $uniciteCollections = $this->get('film.unicite_collections');
  92. $film = $uniciteCollections->assureUniciteCollections($film);
  93. $em->flush();
  94. $this->addFlash('success', 'Le film a été modifié');
  95. return $this->redirectToRoute('videotheque_voirfilm',array('id'=>$film->getId()));
  96. }
  97. return $this->render('@App/videotheque/modifier.html.twig', array(
  98. 'form' => $form->createView(),
  99. ));
  100. }
  101. /**
  102. * @Route("/supprimer/{id}", name="videotheque_supprimer")
  103. * @Security("has_role('ROLE_ADMIN')")
  104. */
  105. public function supprimerAction(Request $request, Film $film)
  106. {
  107. $form = $this->get('form.factory')->create();
  108. $form->handleRequest($request);
  109. if ($form->isSubmitted() && $form->isValid())
  110. {
  111. $em = $this->getDoctrine()->getManager();
  112. $commentaire = $em->getRepository('AppBundle:Commentaire')->findOneBy(array('film'=>$film));
  113. if ($commentaire != null)
  114. {
  115. $em->remove($commentaire);
  116. }
  117. $em->remove($film);
  118. $em->flush();
  119. $this->addFlash('success', 'Le film '.$film->getTitre().' a bien été supprimé.');
  120. return $this->redirectToRoute('videotheque_liste');
  121. }
  122. return $this->render('@App/videotheque/supprimer.html.twig', array(
  123. 'film' => $film,
  124. 'form' => $form->createView()
  125. ));
  126. }
  127. /**
  128. * @Route("/fichefilm/{id}", name="videotheque_voirfilm")
  129. */
  130. public function voirFilmAction(Request $request, \AppBundle\Entity\Film $film)
  131. {
  132. $em = $this->getDoctrine()->getManager();
  133. $repoComment = $em->getRepository('AppBundle:Commentaire');
  134. $commentaireUser = $repoComment->findOneBy(array('film'=>$film, 'user'=>$this->getUser()));
  135. if (is_null($commentaireUser))
  136. {
  137. $commentaireUser = new Commentaire();
  138. $commentaireUser->setUser($this->getUser());
  139. $commentaireUser->setFilm(($film));
  140. }
  141. $form = $this->createForm('AppBundle\Form\CommentaireType', $commentaireUser);
  142. $form->handleRequest($request);
  143. if ($form->isSubmitted() && $form->isValid())
  144. {
  145. $commentaireUser = $form->getData();
  146. $em->persist($commentaireUser);
  147. $em->flush();
  148. $this->addFlash('success', 'Le commentaire a été posté');
  149. }
  150. $commentaires = $repoComment->findBy(array('film'=>$film));
  151. return $this->render('@App/videotheque/voirfilm.html.twig', array(
  152. 'film' => $film,
  153. 'commentaires' => $commentaires,
  154. 'form' => $form->createView()
  155. ));
  156. }
  157. /**
  158. * @Route("/ajax_req_realisateurs", name="videotheque_ajax_realisateurs")
  159. */
  160. public function ajaxRealisateurs(Request $request)
  161. {
  162. $realisateurs = $this
  163. ->getDoctrine()
  164. ->getManager()
  165. ->getRepository('AppBundle:Realisateur')
  166. ->findNomsComplets();
  167. $liste = array();
  168. foreach ($realisateurs as $key=>$nom)
  169. {
  170. $liste[$key] = $this->get('serializer')->serialize($nom, 'json');
  171. }
  172. return new JsonResponse($liste);
  173. }
  174. /**
  175. * @Route("/ajax_req_genres", name="videotheque_ajax_genres")
  176. */
  177. public function ajaxGenres(Request $request)
  178. {
  179. $genres = $this
  180. ->getDoctrine()
  181. ->getManager()
  182. ->getRepository('AppBundle:Genre')
  183. ->findGenres();
  184. $liste = array();
  185. foreach ($genres as $key=>$nom)
  186. {
  187. $liste[$key] = $this->get('serializer')->serialize($nom, 'json');
  188. }
  189. return new JsonResponse($liste);
  190. }
  191. }