VideothequeController.php 7.2 KB

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