<?php
namespace App\Controller;
use App\Entity\CBRCArticle;
use App\Entity\CBRCArticleComment;
use App\Entity\CBRCUser;
use App\Form\Type\CBRCArticleCommentType;
use App\Form\Type\CBRCArticleType;
use App\Form\Type\CBRCUserDescriptionType;
use App\Form\Type\CBRCUserType;
use App\Form\Type\ModifyEmailType;
use App\Form\Type\ModifyPasswordType;
use App\Repository\CBRCArticleRepository;
use App\Utilities\Mail;
use App\Utilities\StatusUtilities;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\Request;
class ArticleController extends AbstractController
{
/**
* @Route ("/article/{articleid}", name="show_article")
*/
public function ShowArticle(int $articleid, Request $request)
{
$article = $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
if (!$article)
throw new NotFoundHttpException();
$user = $this->getUser();
$comment = new CBRCArticleComment();
$comment->setUser($user);
$comment->setArticle($article);
$formcomment = $this->createForm(CBRCArticleCommentType::class, $comment);
$formcomment->handleRequest($request);
if ($formcomment->isSubmitted() && $formcomment->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();
$this->addFlash("success", "Le commentaire a été publié");
try {
//send mails for new comment
$articlecomments = $article->getComments();
$arrayemails = array();
foreach ($articlecomments as $articlecomment) {
$email = $articlecomment->getUser()->getEmail();
if (!in_array($email, $arrayemails, true)) {
array_push($arrayemails, $email);
}
}
//add the author
array_push($arrayemails, $article->getAuthor()->getEmail());
//remove current user
$index = array_search($user->getEmail(), $arrayemails);
if ($index != false) {
array_splice($arrayemails, $index, 1);
}
$txtmessage = "Pas de message en format texte disponible";
$subject = "Nouveau commentaire";
$htmlmessage = $this->renderView('mail/newcommentmail.html.twig', [
'subject' => $subject,
'comment' => $comment,
]);
foreach ($arrayemails as $email) {
$return = Mail::SendMailToMail($email, $subject, $htmlmessage, $txtmessage);
/*if ($return == false) {
$this->addFlash('danger', 'Erreur lors de l\'envoi du mail');
} else {
$this->addFlash('success', 'Le message a été envoyé');
}*/
}
}
catch (\Exception $e)
{
$this->addFlash('warning', $e->getMessage());
}
//$this->addFlash('info', $request->getUri());
return $this->redirect($request->getRequestUri());
}
if ($formcomment->isSubmitted() && !$formcomment->isValid()) {
foreach ($formcomment->getErrors(true) as $error)
$this->addFlash('danger', "(" . $error->getOrigin()->getName() . ") " . $error->getMessage());
$formcomment->clearErrors(true);
}
$comments = $this->getDoctrine()->getRepository(CBRCArticleComment::class)->findByArticle($article);
return $this->render('article/articleview.html.twig', [
'article' => $article,
'comments' => $comments,
'formcomment' => $formcomment->createView(),
]);
}
/**
* @Route ("/admin/article/create", name="create_article")
*/
public function ShowCreateArticle(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_WRITER');
$user = $this->getUser();
$newarticle = new CBRCArticle();
$newarticle->setAuthor($user);
$newarticle->getContent()->setSignature($user->getDescription()->getForname() . ' ' . $user->getDescription()->getName());
$formcreatearticle = $this->createForm(CBRCArticleType::class, $newarticle);
$formcreatearticle->handleRequest($request);
if ($formcreatearticle->isSubmitted() && $formcreatearticle->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($newarticle);
$em->flush();
$this->addFlash("success", "Nouvel article créé");
return $this->redirectToRoute("edit_article", [
'articleid' => $newarticle->getId(),
]);
}
if ($formcreatearticle->isSubmitted() && !$formcreatearticle->isValid()) {
foreach ($formcreatearticle->getErrors(true) as $error)
$this->addFlash('danger', "(" . $error->getOrigin()->getName() . ") " . $error->getMessage());
$formcreatearticle->clearErrors(true);
}
return $this->render('article/createarticle.html.twig', [
'formarticle' => $formcreatearticle->createView(),
'idactive' => 1,
]
);
}
/**
* @Route ("/admin/article/{articleid}/edit", name="edit_article")
*/
public function ShowEditArticle(int $articleid, Request $request)
{
$this->denyAccessUnlessGranted('ROLE_WRITER');
$article = $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
if (!$article)
throw new NotFoundHttpException("article non trouvé");
$formeditarticle = $this->createForm(CBRCArticleType::class, $article, [
'submit_label' => 'Modifier'
]);
$formeditarticle->handleRequest($request);
if ($formeditarticle->isSubmitted() && $formeditarticle->isValid()) {
$article->setUpdateddate(new \DateTime('now'));
$em = $this->getDoctrine()->getManager();
$em->persist($article);
$em->flush();
$this->addFlash("success", "Article modifié");
return $this->redirect($request->getUri());
}
if ($formeditarticle->isSubmitted() && !$formeditarticle->isValid()) {
foreach ($formeditarticle->getErrors(true) as $error)
$this->addFlash('danger', "(" . $error->getOrigin()->getName() . ") " . $error->getMessage());
$formeditarticle->clearErrors(true);
}
return $this->render('article/editarticle.html.twig', [
'formarticle' => $formeditarticle->createView(),
'article' => $article,
'idactive' => 2,
]
);
}
/**
* @Route ("/admin/articles/me", name="my_articles")
*/
public function ShowOwnArticles(Request $request, PaginatorInterface $paginator)
{
$this->denyAccessUnlessGranted('ROLE_WRITER');
$user = $this->getUser();
$articles = $user->getArticles();
$pagination = $paginator->paginate(
$articles, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
20 /*limit per page*/
);
return $this->render('article/adminlistarticles.html.twig', [
'title' => "Mes Articles",
'idactive' => 3,
'articles' => $articles,
'pagination' => $pagination,
]);
}
/**
* @Route("/admin/articles/all", name="all_articles")
*/
public function ShowAllArticles(Request $request, PaginatorInterface $paginator)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$user = $this->getUser();
$articles = $this->getDoctrine()->getRepository(CBRCArticle::class)->findAllAdmin();
$pagination = $paginator->paginate(
$articles, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
20 /*limit per page*/
);
return $this->render('article/adminlistarticles.html.twig', [
'title' => "Tous les Articles",
'idactive' => 4,
'articles' => $articles,
'pagination' => $pagination,
]);
}
/**
* @Route("/admin/commentaires/moi", name="my_comments")
*/
public function ShownOwnComments(Request $request, PaginatorInterface $paginator)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$user = $this->getUser();
$comments = $user->getComments();
$pagination = $paginator->paginate(
$comments, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
20 /*limit per page*/
);
return $this->render('article/adminlistcomments.html.twig', [
'idactive' => 9,
'comments' => $comments,
'pagination' => $pagination,
]);
}
/**
* @Route ("/admin/article/{articleid}/delete", name="delete_article")
*/
public function DeleteArticle(int $articleid, Request $request)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$article = $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
if (!$article)
throw new NotFoundHttpException();
$this->denyAccessUnlessGranted('delete', $article);
$em = $this->getDoctrine()->getManager();
$em->remove($article);
$em->flush();
return $this->redirectToRoute('my_articles');
}
/**
* @Route ("/admin/article/{articleid}/publish" , name="publish_article")
*/
public function PublishArticle(int $articleid)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$article = $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
if (!$article)
throw new NotFoundHttpException();
$article->setStatus(StatusUtilities::StatusArray['published']);
$article->setPublisheddate(new \DateTime('now'));
$em = $this->getDoctrine()->getManager();
$em->persist($article);
$em->flush();
$this->addFlash("success", "Article publié!");
return $this->redirectToRoute('edit_article', [
'articleid' => $articleid
]);
}
/**
* @Route ("/admin/article/{articleid}/archive" , name="archive_article")
*/
public function ArchiveArticle(int $articleid)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$article = $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
if (!$article)
throw new NotFoundHttpException();
$article->setStatus(StatusUtilities::StatusArray['archived']);
$article->setArchiveddate(new \DateTime('now'));
$em = $this->getDoctrine()->getManager();
$em->persist($article);
$em->flush();
$this->addFlash("warning", "Article archivé!");
return $this->redirectToRoute('edit_article', [
'articleid' => $articleid
]);
}
public function ShowListArticleView(int $articleid, Request $request): \Symfony\Component\HttpFoundation\Response
{
$article = $this->getDoctrine()->getRepository(CBRCArticle::class)->find($articleid);
if (!$article)
throw new NotFoundHttpException();
return $this->render('article/articleviewlist.html.twig', [
'article' => $article,
]);
}
/**
* @Route ("/comment/{commentid}/delete", name="delete_comment")
*/
public function DeleteComment(int $commentid, Request $request)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$comment = $this->getDoctrine()->getRepository(CBRCArticleComment::class)->find($commentid);
if (!$comment)
throw new NotFoundHttpException();
$article = $comment->getArticle();
$this->denyAccessUnlessGranted('edit', $comment);
$em = $this->getDoctrine()->getManager();
$em->remove($comment);
$em->flush();
$this->addFlash('warning', 'Le commentaire a été supprimé');
return $this->redirectToRoute('show_article', ['articleid' => $article->getId()]);
}
}