src/Controller/Vitrine/ArticlesController.php line 83

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Vitrine;
  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_BLOG'];
  40.         $locale $request->getLocale();
  41.         $page =  $this->em->getRepository(Pages::class)->findOneBy(['locale' => $locale'name' => 'articles']);
  42.         if($page->getType() == "brouillon") {
  43.             return $this->redirectToRoute('homepage');
  44.         }
  45.         if(!empty($page->getRedirect())) {
  46.             return $this->redirect($page->getRedirect());
  47.         }
  48.         $articles $this->em->getRepository(Articles::class)->getFinalArticles($locale);
  49.         $pagination $this->paginator->paginate(
  50.             $articles,
  51.             $request->query->getInt('page'1),
  52.             10
  53.         );
  54.         $blocks $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page'type' => 'prod''startPage' => false],['sequence' => 'ASC']);
  55.         $page->setViews((int)$page->getViews() + 1);
  56.         $this->em->persist($page);
  57.         $this->em->flush();
  58.         return $this->render('vitrine/'.$themeSelection.'/articles/list.html.twig', [
  59.             'pagination' => $pagination,
  60.             'page' => $page,
  61.             'blocks' => $blocks,
  62.             'formNewsletter' => null
  63.         ]);
  64.     }
  65.     /**
  66.      * Lire un article
  67.      * @param Request $request
  68.      * @param $slug
  69.      * @return mixed
  70.      */
  71.     public function article(Request $request$slug)
  72.     {
  73.         $themeSelection $_ENV['THEME_BLOG'];
  74.         $locale $request->getLocale();
  75.         $article $this->em->getRepository(Articles::class)->getArticle($locale,$slug);
  76.         if($article->getVisibility() == false) {
  77.             return $this->redirectToRoute('homepage');
  78.         }
  79.         return $this->render('vitrine/'.$themeSelection.'/articles/article.html.twig',[
  80.             'article' => $article,
  81.         ]);
  82.     }
  83. }