Browse Source

Gestion des commentaires dans service

François Drouhard 5 years ago
parent
commit
7a3b15cbee

+ 4 - 15
src/Controller/VideothequeController.php

@@ -13,6 +13,7 @@ use Symfony\Component\Routing\Annotation\Route;
 use Symfony\Component\HttpFoundation\Request;
 use App\Entity\Film;
 use App\Form\FilmType;
+use App\Service\CommentaireManager;
 use App\Service\FilmManager;
 use Doctrine\ORM\EntityManagerInterface;
 
@@ -124,30 +125,20 @@ class VideothequeController extends AbstractController
 	/**
      * @Route("/fichefilm/{id}", name="videotheque_voirfilm")
      */
-	public function voirFilmAction(Request $request, \App\Entity\Film $film, Security $security, EntityManagerInterface $em)
+	public function voirFilmAction(Request $request, \App\Entity\Film $film, CommentaireManager $cm, Security $security, EntityManagerInterface $em)
     {
-        $repoComment = $em->getRepository('App:Commentaire');
         if ($security->isGranted('IS_AUTHENTICATED_REMEMBERED'))
         {
-            $commentaireUser = $repoComment->findOneBy(array('film'=>$film, 'user'=>$this->getUser()));
+            $commentaireUser = $em->getRepository('App:Commentaire')->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();
+                $cm->delEditAdd($commentaireUser, $film);
                 $this->addFlash('success', 'Le commentaire a été posté');
                 return $this->redirectToRoute('videotheque_voirfilm', array('id' => $film->getId()));
 
@@ -155,11 +146,9 @@ class VideothequeController extends AbstractController
         } else {
             $form = $this->createForm('App\Form\CommentaireType', null);
         }
-        $commentaires = $repoComment->findBy(array('film'=>$film));
         
         return $this->render('videotheque/voirfilm.html.twig', array(
             'film' => $film,
-            'commentaires'  => $commentaires,
             'form'  =>  $form->createView()
         ));
     }

+ 47 - 3
src/Service/CommentaireManager.php

@@ -21,21 +21,65 @@ class CommentaireManager {
         $this->user = $security->getUser();
     }
 
-    public function addCommentaire (Commentaire $commentaire)
+    private function addCommentaire (Commentaire $commentaire, \App\Entity\Film $film)
     {
+        $commentaire->setUser($this->user);
+        $commentaire->setFilm(($film));
+        $film->addCommentaire($commentaire);
         $this->em->persist($commentaire);
         $this->em->flush();
     }
 
-    public function editCommentaire(Commentaire $commentaire)
+    private function editCommentaire(Commentaire $commentaire)
     {
         $this->em->flush();
     }
 
-    public function delCommentaire(Commentaire $commentaire)
+    private function delCommentaire(Commentaire $commentaire)
     {
         $commentaire->getFilm()->removeCommentaire($commentaire);
         $this->em->remove($commentaire);
         $this->em->flush();
     }
+
+    private function isEmpty (Commentaire $commentaire)
+    {
+        return (
+            is_null($commentaire->getContenu()) &&
+            is_null($commentaire->getNote()));
+    }
+
+    public function delEditAdd(Commentaire $commentaire, \App\Entity\Film $film)
+    {
+        if (!$this->isEmpty($commentaire))
+        {
+            if (!is_null($commentaire->getId()))
+            {
+                $this->editCommentaire($commentaire);
+                return;
+            } else {
+                $this->addCommentaire($commentaire, $film);
+                return;
+            }
+        } else {
+            if (!is_null($commentaire->getId()))
+            {
+                $this->delCommentaire($commentaire);
+                return;
+            }
+        }
+
+        /*if ($this->isEmpty($commentaire)) {
+            if (!is_null($commentaire->getId())) {
+                $this->delCommentaire($commentaire);
+                return;
+            } else {
+                $this->em->flush();
+                return;
+            }
+        } else {
+            $cm->addCommentaire($commentaireUser, $film);
+            return;
+        }*/
+    }
 }

+ 1 - 1
src/Service/NoteMoyenne.php

@@ -47,7 +47,7 @@ class NoteMoyenne {
         
         catch (Exception $e)
         {
-            Debug::dump($e->getCode());
+            dump($e);
         }
     }
 }

+ 1 - 4
templates/videotheque/voirfilm.html.twig

@@ -140,11 +140,8 @@
                             </div>
                     {% endif %}
                     <div class="list-group">
-                        {% for commentaire in commentaires %}
+                        {% for commentaire in film.commentaires %}
                             <div class="list-group-item">
-                                {#{% if commentaire.user == app.user %}
-                                    <a type="button" data-toggle="modal" data-target="#supprcommentaire" class="float-lg-right" id="supp_commentaire" href="#"><i class="fa fa-trash" style="color: Tomato"></i></a>
-                                {% endif %}#}
                                 <p class="mb-1"><strong>Posté par {{ commentaire.user.username }}</strong></p>
                                 {% if commentaire.note > 0 %}
                                     <input class="rating"