VideothequeController.php 8.0 KB

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