src/Controller/ThemesWebsite/Blog/ArticlesController.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ThemesWebsite\Blog;
  3. use App\Entity\Articles\Articles;
  4. use App\Entity\Pages\Pages;
  5. use App\Entity\Pages\PagesHasBlocks;
  6. use App\Services\Core\Core;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Knp\Component\Pager\PaginatorInterface;
  18. /**
  19.  * Système d'articles
  20.  */
  21. class ArticlesController extends AbstractController
  22. {
  23.     private $em;
  24.     private $paginator;
  25.     public function __construct (EntityManagerInterface $em,
  26.                                  PaginatorInterface $paginator
  27.     ){
  28.         $this->em $em;
  29.         $this->paginator $paginator;
  30.     }
  31.     
  32.     /**
  33.      * Liste des articles
  34.      * @param Request $request
  35.      * @return mixed
  36.      */
  37.     public function articles(Request $request)
  38.     {
  39.         $themeSelection $_ENV['THEME_SELECTION'];
  40.         $page =  $this->em->getRepository(Pages::class)->findOneBy(['name' => 'articles']);
  41.         if($page->getType() == "brouillon") {
  42.             return $this->redirectToRoute('homepage');
  43.         }
  44.         if(!empty($page->getRedirect())) {
  45.             return $this->redirect($page->getRedirect());
  46.         }
  47.         $articles $this->em->getRepository(Articles::class)->getFinalArticles("fr");
  48.         $pagination $this->paginator->paginate(
  49.             $articles,
  50.             $request->query->getInt('page'1),
  51.             10
  52.         );
  53.         $blocks $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page'type' => 'prod''startPage' => false],['sequence' => 'ASC']);
  54.         $page->setViews((int)$page->getViews() + 1);
  55.         $this->em->persist($page);
  56.         $this->em->flush();
  57.         return $this->render('themesWebsite/blog'.$themeSelection.'/articles/list.html.twig', [
  58.             'pagination' => $pagination,
  59.             'page' => $page,
  60.             'blocks' => $blocks,
  61.             'formNewsletter' => null
  62.         ]);
  63.     }
  64.     /**
  65.      * Lire un article
  66.      * @param Request $request
  67.      * @param $slug
  68.      * @return mixed
  69.      */
  70.     public function article(Request $request$slug)
  71.     {
  72.         $themeSelection $_ENV['THEME_SELECTION'];
  73.         $article $this->em->getRepository(Articles::class)->getArticle($slug);
  74.         if($article->getVisibility() == false) {
  75.             return $this->redirectToRoute('homepage');
  76.         }
  77.         return $this->render('themesWebsite/blog'.$themeSelection.'/articles/article.html.twig',[
  78.             'article' => $article,
  79.         ]);
  80.     }
  81. }