src/Controller/ArticleController.php line 349

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CBRCArticle;
  4. use App\Entity\CBRCArticleComment;
  5. use App\Entity\CBRCUser;
  6. use App\Form\Type\CBRCArticleCommentType;
  7. use App\Form\Type\CBRCArticleType;
  8. use App\Form\Type\CBRCUserDescriptionType;
  9. use App\Form\Type\CBRCUserType;
  10. use App\Form\Type\ModifyEmailType;
  11. use App\Form\Type\ModifyPasswordType;
  12. use App\Repository\CBRCArticleRepository;
  13. use App\Utilities\Mail;
  14. use App\Utilities\StatusUtilities;
  15. use Knp\Component\Pager\PaginatorInterface;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  22. use Symfony\Component\HttpFoundation\Request;
  23. class ArticleController extends AbstractController
  24. {
  25.     /**
  26.      * @Route ("/article/{articleid}", name="show_article")
  27.      */
  28.     public function ShowArticle(int $articleidRequest $request)
  29.     {
  30.         $article $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
  31.         if (!$article)
  32.             throw new NotFoundHttpException();
  33.         $user $this->getUser();
  34.         $comment = new CBRCArticleComment();
  35.         $comment->setUser($user);
  36.         $comment->setArticle($article);
  37.         $formcomment $this->createForm(CBRCArticleCommentType::class, $comment);
  38.         $formcomment->handleRequest($request);
  39.         if ($formcomment->isSubmitted() && $formcomment->isValid()) {
  40.             $em $this->getDoctrine()->getManager();
  41.             $em->persist($comment);
  42.             $em->flush();
  43.             $this->addFlash("success""Le commentaire a été publié");
  44.             try {
  45.                 //send mails for new comment
  46.                 $articlecomments $article->getComments();
  47.                 $arrayemails = array();
  48.                 foreach ($articlecomments as $articlecomment) {
  49.                     $email $articlecomment->getUser()->getEmail();
  50.                     if (!in_array($email$arrayemailstrue)) {
  51.                         array_push($arrayemails$email);
  52.                     }
  53.                 }
  54.                 //add the author
  55.                 array_push($arrayemails$article->getAuthor()->getEmail());
  56.                 //remove current user
  57.                 $index array_search($user->getEmail(), $arrayemails);
  58.                 if ($index != false) {
  59.                     array_splice($arrayemails$index1);
  60.                 }
  61.                 $txtmessage "Pas de message en format texte disponible";
  62.                 $subject "Nouveau commentaire";
  63.                 $htmlmessage $this->renderView('mail/newcommentmail.html.twig', [
  64.                     'subject' => $subject,
  65.                     'comment' => $comment,
  66.                 ]);
  67.                 foreach ($arrayemails as $email) {
  68.                     $return Mail::SendMailToMail($email$subject$htmlmessage$txtmessage);
  69.                     /*if ($return == false) {
  70.                         $this->addFlash('danger', 'Erreur lors de l\'envoi du mail');
  71.                     } else {
  72.                         $this->addFlash('success', 'Le message a été envoyé');
  73.                     }*/
  74.                 }
  75.             }
  76.             catch (\Exception $e)
  77.             {
  78.                 $this->addFlash('warning'$e->getMessage());
  79.             }
  80.             //$this->addFlash('info', $request->getUri());
  81.             return $this->redirect($request->getRequestUri());
  82.         }
  83.         if ($formcomment->isSubmitted() && !$formcomment->isValid()) {
  84.             foreach ($formcomment->getErrors(true) as $error)
  85.                 $this->addFlash('danger'"(" $error->getOrigin()->getName() . ") " $error->getMessage());
  86.             $formcomment->clearErrors(true);
  87.         }
  88.         $comments $this->getDoctrine()->getRepository(CBRCArticleComment::class)->findByArticle($article);
  89.         return $this->render('article/articleview.html.twig', [
  90.             'article' => $article,
  91.             'comments' => $comments,
  92.             'formcomment' => $formcomment->createView(),
  93.         ]);
  94.     }
  95.     /**
  96.      * @Route ("/admin/article/create", name="create_article")
  97.      */
  98.     public function ShowCreateArticle(Request $request)
  99.     {
  100.         $this->denyAccessUnlessGranted('ROLE_WRITER');
  101.         $user $this->getUser();
  102.         $newarticle = new CBRCArticle();
  103.         $newarticle->setAuthor($user);
  104.         $newarticle->getContent()->setSignature($user->getDescription()->getForname() . ' ' $user->getDescription()->getName());
  105.         $formcreatearticle $this->createForm(CBRCArticleType::class, $newarticle);
  106.         $formcreatearticle->handleRequest($request);
  107.         if ($formcreatearticle->isSubmitted() && $formcreatearticle->isValid()) {
  108.             $em $this->getDoctrine()->getManager();
  109.             $em->persist($newarticle);
  110.             $em->flush();
  111.             $this->addFlash("success""Nouvel article créé");
  112.             return $this->redirectToRoute("edit_article", [
  113.                 'articleid' => $newarticle->getId(),
  114.             ]);
  115.         }
  116.         if ($formcreatearticle->isSubmitted() && !$formcreatearticle->isValid()) {
  117.             foreach ($formcreatearticle->getErrors(true) as $error)
  118.                 $this->addFlash('danger'"(" $error->getOrigin()->getName() . ") " $error->getMessage());
  119.             $formcreatearticle->clearErrors(true);
  120.         }
  121.         return $this->render('article/createarticle.html.twig', [
  122.                 'formarticle' => $formcreatearticle->createView(),
  123.                 'idactive' => 1,
  124.             ]
  125.         );
  126.     }
  127.     /**
  128.      * @Route ("/admin/article/{articleid}/edit", name="edit_article")
  129.      */
  130.     public function ShowEditArticle(int $articleidRequest $request)
  131.     {
  132.         $this->denyAccessUnlessGranted('ROLE_WRITER');
  133.         $article $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
  134.         if (!$article)
  135.             throw  new NotFoundHttpException("article non trouvé");
  136.         $formeditarticle $this->createForm(CBRCArticleType::class, $article, [
  137.             'submit_label' => 'Modifier'
  138.         ]);
  139.         $formeditarticle->handleRequest($request);
  140.         if ($formeditarticle->isSubmitted() && $formeditarticle->isValid()) {
  141.             $article->setUpdateddate(new \DateTime('now'));
  142.             $em $this->getDoctrine()->getManager();
  143.             $em->persist($article);
  144.             $em->flush();
  145.             $this->addFlash("success""Article modifié");
  146.             return $this->redirect($request->getUri());
  147.         }
  148.         if ($formeditarticle->isSubmitted() && !$formeditarticle->isValid()) {
  149.             foreach ($formeditarticle->getErrors(true) as $error)
  150.                 $this->addFlash('danger'"(" $error->getOrigin()->getName() . ") " $error->getMessage());
  151.             $formeditarticle->clearErrors(true);
  152.         }
  153.         return $this->render('article/editarticle.html.twig', [
  154.                 'formarticle' => $formeditarticle->createView(),
  155.                 'article' => $article,
  156.                 'idactive' => 2,
  157.             ]
  158.         );
  159.     }
  160.     /**
  161.      * @Route ("/admin/articles/me", name="my_articles")
  162.      */
  163.     public function ShowOwnArticles(Request $requestPaginatorInterface $paginator)
  164.     {
  165.         $this->denyAccessUnlessGranted('ROLE_WRITER');
  166.         $user $this->getUser();
  167.         $articles $user->getArticles();
  168.         $pagination $paginator->paginate(
  169.             $articles/* query NOT result */
  170.             $request->query->getInt('page'1), /*page number*/
  171.             20 /*limit per page*/
  172.         );
  173.         return $this->render('article/adminlistarticles.html.twig', [
  174.             'title' => "Mes Articles",
  175.             'idactive' => 3,
  176.             'articles' => $articles,
  177.             'pagination' => $pagination,
  178.         ]);
  179.     }
  180.     /**
  181.      * @Route("/admin/articles/all", name="all_articles")
  182.      */
  183.     public function ShowAllArticles(Request $requestPaginatorInterface $paginator)
  184.     {
  185.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  186.         $user $this->getUser();
  187.         $articles $this->getDoctrine()->getRepository(CBRCArticle::class)->findAllAdmin();
  188.         $pagination $paginator->paginate(
  189.             $articles/* query NOT result */
  190.             $request->query->getInt('page'1), /*page number*/
  191.             20 /*limit per page*/
  192.         );
  193.         return $this->render('article/adminlistarticles.html.twig', [
  194.             'title' => "Tous les Articles",
  195.             'idactive' => 4,
  196.             'articles' => $articles,
  197.             'pagination' => $pagination,
  198.         ]);
  199.     }
  200.     /**
  201.      * @Route("/admin/commentaires/moi", name="my_comments")
  202.      */
  203.     public function ShownOwnComments(Request $requestPaginatorInterface $paginator)
  204.     {
  205.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  206.         $user $this->getUser();
  207.         $comments $user->getComments();
  208.         $pagination $paginator->paginate(
  209.             $comments/* query NOT result */
  210.             $request->query->getInt('page'1), /*page number*/
  211.             20 /*limit per page*/
  212.         );
  213.         return $this->render('article/adminlistcomments.html.twig', [
  214.             'idactive' => 9,
  215.             'comments' => $comments,
  216.             'pagination' => $pagination,
  217.         ]);
  218.     }
  219.     /**
  220.      * @Route ("/admin/article/{articleid}/delete", name="delete_article")
  221.      */
  222.     public function DeleteArticle(int $articleidRequest $request)
  223.     {
  224.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  225.         $article $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
  226.         if (!$article)
  227.             throw new NotFoundHttpException();
  228.         $this->denyAccessUnlessGranted('delete'$article);
  229.         $em $this->getDoctrine()->getManager();
  230.         $em->remove($article);
  231.         $em->flush();
  232.         return $this->redirectToRoute('my_articles');
  233.     }
  234.     /**
  235.      * @Route ("/admin/article/{articleid}/publish" , name="publish_article")
  236.      */
  237.     public function PublishArticle(int $articleid)
  238.     {
  239.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  240.         $article $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
  241.         if (!$article)
  242.             throw new NotFoundHttpException();
  243.         $article->setStatus(StatusUtilities::StatusArray['published']);
  244.         $article->setPublisheddate(new \DateTime('now'));
  245.         $em $this->getDoctrine()->getManager();
  246.         $em->persist($article);
  247.         $em->flush();
  248.         $this->addFlash("success""Article publié!");
  249.         return $this->redirectToRoute('edit_article', [
  250.             'articleid' => $articleid
  251.         ]);
  252.     }
  253.     /**
  254.      * @Route ("/admin/article/{articleid}/archive" , name="archive_article")
  255.      */
  256.     public function ArchiveArticle(int $articleid)
  257.     {
  258.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  259.         $article $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
  260.         if (!$article)
  261.             throw new NotFoundHttpException();
  262.         $article->setStatus(StatusUtilities::StatusArray['archived']);
  263.         $article->setArchiveddate(new \DateTime('now'));
  264.         $em $this->getDoctrine()->getManager();
  265.         $em->persist($article);
  266.         $em->flush();
  267.         $this->addFlash("warning""Article archivé!");
  268.         return $this->redirectToRoute('edit_article', [
  269.             'articleid' => $articleid
  270.         ]);
  271.     }
  272.     public function ShowListArticleView(int $articleidRequest $request): \Symfony\Component\HttpFoundation\Response
  273.     {
  274.         $article $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
  275.         if (!$article)
  276.             throw new NotFoundHttpException();
  277.         return $this->render('article/articleviewlist.html.twig', [
  278.             'article' => $article,
  279.         ]);
  280.     }
  281.     /**
  282.      * @Route ("/comment/{commentid}/delete", name="delete_comment")
  283.      */
  284.     public function DeleteComment(int $commentidRequest $request)
  285.     {
  286.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  287.         $comment $this->getDoctrine()->getRepository(CBRCArticleComment::class)->find($commentid);
  288.         if (!$comment)
  289.             throw new NotFoundHttpException();
  290.         $article $comment->getArticle();
  291.         $this->denyAccessUnlessGranted('edit'$comment);
  292.         $em $this->getDoctrine()->getManager();
  293.         $em->remove($comment);
  294.         $em->flush();
  295.         $this->addFlash('warning''Le commentaire a été supprimé');
  296.         return $this->redirectToRoute('show_article', ['articleid' => $article->getId()]);
  297.     }
  298. }