VideothequeController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Commentaire;
  4. use App\Entity\Realisateur;
  5. use App\Service\UniciteCollections;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use App\Entity\Film;
  12. use App\Form\FilmType;
  13. class VideothequeController extends AbstractController
  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('App:Film');
  29. $listeFilms = $repo->findTous();
  30. return $this->render('videotheque/liste.html.twig', array(
  31. 'listeFilms' => $listeFilms
  32. ));
  33. }
  34. /**
  35. * @Route("/liste-by/{id}", name="videotheque_listepargenre")
  36. */
  37. public function listeParGenreAction(\App\Entity\Genre $genre)
  38. {
  39. $em = $this->getDoctrine()->getManager();
  40. $films = $em->getRepository('App:Film')->findFilmWithGenre(array($genre->getName()));
  41. return $this->render('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('App:Film')->findFilmWithReal(array($realisateur->getNomComplet()));
  53. return $this->render('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, UniciteCollections $uniciteCollections)
  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. $film = $uniciteCollections->assureUniciteCollections($film);
  71. $em->persist($film);
  72. $em->flush();
  73. $this->addFlash('success', 'Le film a été ajouté');
  74. return $this->redirectToRoute('videotheque_voirfilm', array('id'=>$film->getId()));
  75. }
  76. return $this->render('videotheque/ajouter.html.twig', array(
  77. 'form' => $form->createView(),
  78. ));
  79. }
  80. /**
  81. * @Route("/modifier/{id}", name="videotheque_modifier")
  82. */
  83. public function modifierAction(Request $request, Film $film, UniciteCollections $uniciteCollections)
  84. {
  85. $form = $this->createForm(FilmType::class, $film);
  86. $form->handleRequest($request);
  87. $em = $this->getDoctrine()->getManager();
  88. if ($form->isSubmitted() && $form->isValid())
  89. {
  90. $film = $uniciteCollections->assureUniciteCollections($film);
  91. $em->flush();
  92. $this->addFlash('success', 'Le film a été modifié');
  93. return $this->redirectToRoute('videotheque_voirfilm',array('id'=>$film->getId()));
  94. }
  95. return $this->render('videotheque/modifier.html.twig', array(
  96. 'form' => $form->createView(),
  97. ));
  98. }
  99. /**
  100. * @Route("/supprimer/{id}", name="videotheque_supprimer")
  101. * @Security("has_role('ROLE_ADMIN')")
  102. */
  103. public function supprimerAction(Request $request, Film $film)
  104. {
  105. $form = $this->get('form.factory')->create();
  106. $form->handleRequest($request);
  107. if ($form->isSubmitted() && $form->isValid())
  108. {
  109. $em = $this->getDoctrine()->getManager();
  110. $commentaire = $em->getRepository('App:Commentaire')->findOneBy(array('film'=>$film));
  111. if ($commentaire != null)
  112. {
  113. $em->remove($commentaire);
  114. }
  115. $em->remove($film);
  116. $em->flush();
  117. $this->addFlash('success', 'Le film '.$film->getTitre().' a bien été supprimé.');
  118. return $this->redirectToRoute('videotheque_liste');
  119. }
  120. return $this->render('videotheque/supprimer.html.twig', array(
  121. 'film' => $film,
  122. 'form' => $form->createView()
  123. ));
  124. }
  125. /**
  126. * @Route("/fichefilm/{id}", name="videotheque_voirfilm")
  127. */
  128. public function voirFilmAction(Request $request, \App\Entity\Film $film)
  129. {
  130. $em = $this->getDoctrine()->getManager();
  131. $repoComment = $em->getRepository('App:Commentaire');
  132. $commentaireUser = $repoComment->findOneBy(array('film'=>$film, 'user'=>$this->getUser()));
  133. if (is_null($commentaireUser))
  134. {
  135. $commentaireUser = new Commentaire();
  136. $commentaireUser->setUser($this->getUser());
  137. $commentaireUser->setFilm(($film));
  138. }
  139. $form = $this->createForm('App\Form\CommentaireType', $commentaireUser);
  140. $form->handleRequest($request);
  141. if ($form->isSubmitted() && $form->isValid())
  142. {
  143. if ($commentaireUser->getContenu() =="" && $commentaireUser->getNote() == 0) {
  144. if (!is_null($commentaireUser->getId())) {
  145. $em->remove($commentaireUser);
  146. }
  147. } else {
  148. $em->persist($commentaireUser);
  149. }
  150. $em->flush();
  151. $this->addFlash('success', 'Le commentaire a été posté');
  152. return $this->redirectToRoute('videotheque_voirfilm', array('id' => $film->getId()));
  153. }
  154. $commentaires = $repoComment->findBy(array('film'=>$film));
  155. return $this->render('videotheque/voirfilm.html.twig', array(
  156. 'film' => $film,
  157. 'commentaires' => $commentaires,
  158. 'form' => $form->createView()
  159. ));
  160. }
  161. /**
  162. * @Route("/ajax_req_realisateurs", name="videotheque_ajax_realisateurs")
  163. */
  164. public function ajaxRealisateurs(Request $request)
  165. {
  166. $realisateurs = $this
  167. ->getDoctrine()
  168. ->getManager()
  169. ->getRepository('App:Realisateur')
  170. ->findNomsComplets();
  171. $liste = array();
  172. foreach ($realisateurs as $key=>$nom)
  173. {
  174. $liste[$key] = $this->get('serializer')->serialize($nom, 'json');
  175. }
  176. return new JsonResponse($liste);
  177. }
  178. /**
  179. * @Route("/ajax_req_genres", name="videotheque_ajax_genres")
  180. */
  181. public function ajaxGenres(Request $request)
  182. {
  183. $genres = $this
  184. ->getDoctrine()
  185. ->getManager()
  186. ->getRepository('App:Genre')
  187. ->findGenres();
  188. $liste = array();
  189. foreach ($genres as $key=>$nom)
  190. {
  191. $liste[$key] = $this->get('serializer')->serialize($nom, 'json');
  192. }
  193. return new JsonResponse($liste);
  194. }
  195. }