123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- namespace App\Controller;
- use App\Entity\Commentaire;
- use App\Entity\Realisateur;
- use App\Service\UniciteCollections;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Component\Routing\Annotation\Route;
- use Symfony\Component\HttpFoundation\Request;
- use App\Entity\Film;
- use App\Form\FilmType;
- class VideothequeController extends AbstractController
- {
- /**
- * @Route("/", name="racine_test")
- */
- public function indexAction()
- {
- return $this->redirectToRoute('videotheque_liste');
- }
- /**
- * @Route("/liste", name="videotheque_liste")
- */
- public function listeAction()
- {
- $em = $this->getDoctrine()->getManager();
- $repo = $em->getRepository('App:Film');
- $listeFilms = $repo->findTous();
- return $this->render('videotheque/liste.html.twig', array(
- 'listeFilms' => $listeFilms
- ));
- }
- /**
- * @Route("/liste-by/{id}", name="videotheque_listepargenre")
- */
- public function listeParGenreAction(\App\Entity\Genre $genre)
- {
- $em = $this->getDoctrine()->getManager();
- $films = $em->getRepository('App:Film')->findFilmWithGenre(array($genre->getName()));
- return $this->render('videotheque/liste.html.twig', array(
- 'listeFilms' => $films,
- 'titre' => 'Films par catégorie : '.$genre->getName()
- ));
- }
- /**
- * @Route("/liste-by_real/{id}", name="videotheque_listeparreal")
- */
- public function listeParRealisateurAction(Realisateur $realisateur)
- {
- $em = $this->getDoctrine()->getManager();
- $films = $em->getRepository('App:Film')->findFilmWithReal(array($realisateur->getNomComplet()));
- return $this->render('videotheque/liste.html.twig', array(
- 'listeFilms' => $films,
- 'titre' => 'Films par réalisateur : '.$realisateur->getNomComplet()
- ));
- }
- /**
- * @Route("/ajouter", name="videotheque_ajouter")
- */
- public function ajouterAction(Request $request, UniciteCollections $uniciteCollections)
- {
- $film = new Film;
- $film->setAuthered($this->getUser());
- $form = $this->createForm(FilmType::class, $film);
- $em = $this->getDoctrine()->getManager();
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid())
- {
- $film = $uniciteCollections->assureUniciteCollections($film);
- $em->persist($film);
- $em->flush();
- $this->addFlash('success', 'Le film a été ajouté');
- return $this->redirectToRoute('videotheque_voirfilm', array('id'=>$film->getId()));
- }
- return $this->render('videotheque/ajouter.html.twig', array(
- 'form' => $form->createView(),
- ));
- }
- /**
- * @Route("/modifier/{id}", name="videotheque_modifier")
- */
- public function modifierAction(Request $request, Film $film, UniciteCollections $uniciteCollections)
- {
- $form = $this->createForm(FilmType::class, $film);
- $form->handleRequest($request);
- $em = $this->getDoctrine()->getManager();
- if ($form->isSubmitted() && $form->isValid())
- {
- $film = $uniciteCollections->assureUniciteCollections($film);
- $em->flush();
- $this->addFlash('success', 'Le film a été modifié');
- return $this->redirectToRoute('videotheque_voirfilm',array('id'=>$film->getId()));
- }
- return $this->render('videotheque/modifier.html.twig', array(
- 'form' => $form->createView(),
- ));
- }
- /**
- * @Route("/supprimer/{id}", name="videotheque_supprimer")
- * @Security("has_role('ROLE_ADMIN')")
- */
- public function supprimerAction(Request $request, Film $film)
- {
- $form = $this->get('form.factory')->create();
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid())
- {
- $em = $this->getDoctrine()->getManager();
- $commentaire = $em->getRepository('App:Commentaire')->findOneBy(array('film'=>$film));
- if ($commentaire != null)
- {
- $em->remove($commentaire);
- }
- $em->remove($film);
- $em->flush();
- $this->addFlash('success', 'Le film '.$film->getTitre().' a bien été supprimé.');
- return $this->redirectToRoute('videotheque_liste');
- }
- return $this->render('videotheque/supprimer.html.twig', array(
- 'film' => $film,
- 'form' => $form->createView()
- ));
- }
- /**
- * @Route("/fichefilm/{id}", name="videotheque_voirfilm")
- */
- public function voirFilmAction(Request $request, \App\Entity\Film $film)
- {
- $em = $this->getDoctrine()->getManager();
- $repoComment = $em->getRepository('App:Commentaire');
- $commentaireUser = $repoComment->findOneBy(array('film'=>$film, 'user'=>$this->getUser()));
- if (is_null($commentaireUser))
- {
- $commentaireUser = new Commentaire();
- $commentaireUser->setUser($this->getUser());
- $commentaireUser->setFilm(($film));
- }
- $form = $this->createForm('App\Form\CommentaireType', $commentaireUser);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid())
- {
- if ($commentaireUser->getContenu() =="" && $commentaireUser->getNote() == 0) {
- if (!is_null($commentaireUser->getId())) {
- $em->remove($commentaireUser);
- }
- } else {
- $em->persist($commentaireUser);
- }
- $em->flush();
- $this->addFlash('success', 'Le commentaire a été posté');
- return $this->redirectToRoute('videotheque_voirfilm', array('id' => $film->getId()));
- }
- $commentaires = $repoComment->findBy(array('film'=>$film));
- return $this->render('videotheque/voirfilm.html.twig', array(
- 'film' => $film,
- 'commentaires' => $commentaires,
- 'form' => $form->createView()
- ));
- }
- /**
- * @Route("/ajax_req_realisateurs", name="videotheque_ajax_realisateurs")
- */
- public function ajaxRealisateurs(Request $request)
- {
- $realisateurs = $this
- ->getDoctrine()
- ->getManager()
- ->getRepository('App:Realisateur')
- ->findNomsComplets();
- $liste = array();
- foreach ($realisateurs as $key=>$nom)
- {
- $liste[$key] = $this->get('serializer')->serialize($nom, 'json');
- }
- return new JsonResponse($liste);
- }
- /**
- * @Route("/ajax_req_genres", name="videotheque_ajax_genres")
- */
- public function ajaxGenres(Request $request)
- {
- $genres = $this
- ->getDoctrine()
- ->getManager()
- ->getRepository('App:Genre')
- ->findGenres();
- $liste = array();
- foreach ($genres as $key=>$nom)
- {
- $liste[$key] = $this->get('serializer')->serialize($nom, 'json');
- }
- return new JsonResponse($liste);
- }
- }
|