Example #1
0
 public function answerAction(Request $request, $pole, $id, $body = '')
 {
     $user = $this->getUser();
     $permService = $this->container->get('ter_aelis_forum.permission');
     $perm = $permService->getPerm($user);
     $em = $this->getDoctrine()->getManager();
     $thread = $em->getRepository('TerAelisCommentBundle:Thread')->findOneById($id);
     if ($thread == null) {
         throw $this->createNotFoundException("Thread introuvable (idThread = " . $id . ")");
     }
     if ($thread->getLock()) {
         throw new HttpException(Response::HTTP_INTERNAL_SERVER_ERROR, 'Thread vérouillé');
     }
     $post = $thread->getPost();
     if ($post == null) {
         throw $this->createNotFoundException("Post introuvable (idThread = " . $id . ")");
     }
     $categorie = $post->getMainCategorie();
     if (empty($categorie)) {
         throw $this->createNotFoundException("Catégorie introuvable (idThread = " . $post->getId() . ")");
     }
     $path = $em->getRepository('TerAelisForumBundle:Categorie')->getPath($categorie);
     $repondreSujet = $perm['repondreSujet'][$categorie->getId()];
     if (!$repondreSujet) {
         throw new AccessDeniedException("Interdiction de poster dans cette catégorie (Categorie = " . $categorie->getTitle() . ")");
     }
     $returnArray = array();
     $comment = new Comment();
     $comment->setBody($body);
     $form = $this->createForm(new CommentType(), $comment);
     // Le sujet est déjà envoyé
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         $date = new \DateTime();
         $comment->setThread($thread);
         $comment->setCreatedAt($date);
         if ($user != null) {
             $comment->setAuthor($user);
         }
         if ($form->get('previsualiser')->isClicked()) {
             $returnArray['comment'] = $comment;
         } else {
             $lastComment = $em->getRepository('TerAelisCommentBundle:Comment')->findLastByUser($user->getId());
             $lastCommentMaxTime = new \DateTime();
             $lastCommentMaxTime->modify('-10 seconds');
             if (!empty($lastComment) && $lastComment->getCreatedAt()->getTimestamp() > $lastCommentMaxTime->getTimestamp()) {
                 $returnArray['notAllowed'] = 'Votre dernier commentaire a été créé il y a trop peu de temps. Vérifiez qu\'il n\'a pas déjà été créé avant de continuer';
                 $returnArray['comment'] = $comment;
             } else {
                 $em->beginTransaction();
                 $em->persist($comment);
                 $em->flush();
                 $thread->setNumberComment($thread->getNumberComment() + 1);
                 $em->persist($thread);
                 $em->flush();
                 // On prévient les users
                 $nonVuService = $this->container->get('ter_aelis_forum.non_vu');
                 $nonVuService->updateNonVu($post, $comment);
                 $postStatistics = $this->get('ter_aelis_forum.post_statistics');
                 $postStatistics->refreshPosts(array($post));
                 $postStatistics->refreshCategories(array($categorie));
                 $em->commit();
                 $page = intval(1 + ($thread->getNumberComment() - 1) / $this->container->getParameter('nb_commentaires'));
                 // Puis on redirige vers la page de visualisation de cet article
                 return $this->redirect($this->generateUrl('taforum_voirSujet', array('pole' => $pole, 'slug' => $post->getSlug(), 'page' => $page)) . '#last');
             }
         }
     }
     $returnArray = array_merge($returnArray, array('pole' => $pole, 'categorie' => $categorie, 'path' => $path, 'post' => $post, 'thread' => $thread, 'form' => $form->createView(), 'user' => $user));
     return $this->render('TerAelisCommentBundle:Thread:answer.html.twig', $returnArray);
 }
Example #2
0
 public function shortComment(Comment $post)
 {
     $res = $post->getBody();
     return strlen($res) > 150 ? mb_substr($res, 0, 150) . "..." : $res;
 }
Example #3
0
 protected function getQuotedUsersComment(Comment $comment)
 {
     $body = $comment->getBody();
     $arrayUserId = [];
     preg_match_all('#\\[quote="([0-9]+)"\\].*[/quote]#', $body, $matches);
     if (isset($matches['1'])) {
         foreach ($matches['1'] as $q) {
             $arrayUserId[] = $q;
         }
     }
     return $arrayUserId;
 }
Example #4
0
 public function getNumberCommentOlder(Post $post, Comment $comment)
 {
     $t = $comment->getThread();
     $builder = $this->createQueryBuilder('c');
     return $this->createQueryBuilder('c')->select('COUNT(c.id)')->join('c.thread', 't')->where('t.id = ' . $t->getId())->andWhere($builder->expr()->gt('c.createdAt', ':date'))->setParameter('date', $comment->getCreatedAt())->getQuery()->getSingleScalarResult();
 }