<?php
namespace App\Controller;
use App\Entity\Article;
use App\Entity\Category;
use App\Entity\Comment;
use App\Entity\Tag;
use App\Form\ArticleType;
use App\Form\CommentType;
use App\Repository\ArticleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class BlogController extends AbstractController
{
/**
* @var ManagerRegistry
*/
private $doctrine;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
}
// Liste des articles sur la partie publique
/**
* @Route("/blog", name="blog")
*/
public function index(ArticleRepository $repo, Request $request, PaginatorInterface $paginator)
{
//$donnees = $repo->findAll();
$donnees = $repo->findArticlesPublished();
$articles = $paginator->paginate(
$donnees, // on passe les données
$request->query->getInt('page', 1), // N° de la page en cours, 1 par défaut
3 // nombre d'éléments par page
);
return $this->render('blog/index.html.twig', [
'controller_name' => 'BlogController',
'articles' => $articles,
]);
}
// Page d'accueil publique
/**
* @Route("/", name="home")
*/
public function home()
{
$arrayArticles = [];
$categories = $this->doctrine->getRepository(Category::class)->findAll();
foreach($categories as $category) {
$res = $this->doctrine->getRepository(Article::class)->findByCategory($category, ['position' => 'ASC']);
$arrayArticles[$category->getSlug()] = $res;
}
$articles = new ArrayCollection($arrayArticles);
return $this->render('/home.html.twig', [
'categories' => $categories,
'articles' => $articles,
'title' => "Bienvenue sur la page du Dasboard ACS 2020",
]);
}
// Vue dans la partie publique qui permet d'afficher un article
/**
* @Route("/post/{slug}", name="post_show")
*/
public function show(Article $article, Request $request, EntityManagerInterface $manager)
{
$comment = new Comment();
$form = $this->createForm(CommentType::class, $comment);
$articles = $this->doctrine->getRepository(Article::class)->findByCategory($article->getCategory());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/*$comment->setCreatedAt(new \DateTime())
->setArticle($article);*/
$manager->persist($comment);
$manager->flush();
return $this->redirectToRoute('post_show', [
'id' => $article->getId()
]);
}
return $this->render('blog/show.html.twig', [
'article' => $article,
'articles' => $articles,
'commentForm' => $form->createView()
]);
}
}