<?php
namespace App\Controller\ThemesWebsite\Blog;
use App\Entity\Articles\Articles;
use App\Entity\Pages\Pages;
use App\Entity\Pages\PagesHasBlocks;
use App\Services\Core\Core;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Knp\Component\Pager\PaginatorInterface;
/**
* Système d'articles
*/
class ArticlesController extends AbstractController
{
private $em;
private $paginator;
public function __construct (EntityManagerInterface $em,
PaginatorInterface $paginator
){
$this->em = $em;
$this->paginator = $paginator;
}
/**
* Liste des articles
* @param Request $request
* @return mixed
*/
public function articles(Request $request)
{
$themeSelection = $_ENV['THEME_SELECTION'];
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'articles']);
if($page->getType() == "brouillon") {
return $this->redirectToRoute('homepage');
}
if(!empty($page->getRedirect())) {
return $this->redirect($page->getRedirect());
}
$articles = $this->em->getRepository(Articles::class)->getFinalArticles("fr");
$pagination = $this->paginator->paginate(
$articles,
$request->query->getInt('page', 1),
10
);
$blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']);
$page->setViews((int)$page->getViews() + 1);
$this->em->persist($page);
$this->em->flush();
return $this->render('themesWebsite/blog'.$themeSelection.'/articles/list.html.twig', [
'pagination' => $pagination,
'page' => $page,
'blocks' => $blocks,
'formNewsletter' => null
]);
}
/**
* Lire un article
* @param Request $request
* @param $slug
* @return mixed
*/
public function article(Request $request, $slug)
{
$themeSelection = $_ENV['THEME_SELECTION'];
$article = $this->em->getRepository(Articles::class)->getArticle($slug);
if($article->getVisibility() == false) {
return $this->redirectToRoute('homepage');
}
return $this->render('themesWebsite/blog'.$themeSelection.'/articles/article.html.twig',[
'article' => $article,
]);
}
}