src/Controller/ArticleController.php line 172

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use App\Entity\Article;
  7. use App\Entity\ArticleType;
  8. use App\Entity\Media;
  9. use App\Form\UpdateArticleType;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. class ArticleController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/article/{id}", name="showArticle", requirements={"id"="\d+"})
  15.      */
  16.     public function showArticle(Request $request$id)
  17.     {
  18.         $article $this->getDoctrine()->getRepository(Article::class)->find($id);
  19.         if (!$article) {
  20.             throw $this->createNotFoundException('Pas d\'article avec l\'id ' $id '.');
  21.         }
  22.         $slug $article->getSlug();
  23.         if($slug && $slug !== "") {
  24.             return $this->redirect($this->generateUrl('showArticleBySlug', ['slug' => $slug]));
  25.         } else {
  26.             if(!$article->getPublished()) {
  27.                 $redirectUrl $this->_notPublished($request);
  28.                 if($redirectUrl) return $this->redirect($redirectUrl);
  29.             }
  30.             return $this->render('article/show.html.twig', [
  31.                 'article' => $article
  32.             ]);
  33.         }
  34.     }
  35.     /**
  36.      * @Route("/article/{slug}", name="showArticleBySlug")
  37.      */
  38.     public function showArticleBySlug(Request $request$slug)
  39.     {
  40.         $article $this->getDoctrine()->getRepository(Article::class)->findOneBySlug($slug);
  41.         if (!$article) {
  42.             throw $this->createNotFoundException('Pas d\'article avec le slug "' $slug '".');
  43.         }
  44.         if(!$article->getPublished()) {
  45.             $redirectUrl $this->_notPublished($request);
  46.             if($redirectUrl) return $this->redirect($redirectUrl);
  47.         }
  48.         return $this->render('article/show.html.twig', [
  49.             'article' => $article,
  50.         ]);
  51.     }
  52.     /**
  53.      * action if entity not published
  54.      * @return string $redirectUrl
  55.      */
  56.     private function _notPublished($request) {
  57.         if(!$this->isGranted('ROLE_ADMIN')) {
  58.             $requestUri $request->getRequestUri();
  59.             $baseUrl $request->getSchemeAndHttpHost();
  60.             $this->addFlash(
  61.                 'error-popup',
  62.                 'L\'url demandée : "' $baseUrl $requestUri '" n\'existe pas.'
  63.             );
  64.             $referer $request->headers->get('referer');
  65.             if(null != $referer && substr($referer0strlen($baseUrl)) === $baseUrl) {
  66.                 // on retourne de là oú l'on vient, si l'on vient de ce site
  67.                 $redirectUrl substr($refererstrlen($baseUrl));
  68.             } else {
  69.                 // pas de referer trouvé ou appel depuis site extérieur, on va donc à la home
  70.                 $redirectUrl $this->generateUrl('home');
  71.             }
  72.             
  73.         } else { 
  74.             // rôle admin, on laisse consulter
  75.             $this->addFlash(
  76.                 'notice',
  77.                 'Cette question-réponse n\'est pas publiée. En tant qu\'admin vous pouvez toutefois la voir.'
  78.             );
  79.             $redirectUrl null;
  80.         }
  81.         return $redirectUrl;
  82.     }
  83.     /**
  84.      * @Route("/articles/{typeSlug}/{title}", name="showArticlesByType", requirements={"typeSlug"="{'article-fond','publication','a-la-une'}"})
  85.      * @param bool $listWithThumbnails si on veut afficher une liste d'images, sinon liste textuelle 
  86.      */
  87.     public function showArticlesByType(Request $requestPaginatorInterface $paginator$typeSlug$title null$pageLimit 15$listWithThumbnails false)
  88.     {
  89.         $articleTypeRepository $this->getDoctrine()->getRepository(ArticleType::class);
  90.         $type $articleTypeRepository->findOneBySlug($typeSlug);
  91.         if (!$type) {
  92.             throw $this->createNotFoundException('Pas de type d\'article avec le slug ' $typeSlug '.');
  93.         }
  94.         $articleRepository $this->getDoctrine()->getRepository(Article::class);
  95.         $pageNb $request->query->getInt('page'1);
  96.         $articles $paginator->paginate(
  97.             $articleRepository->getQueryByType($type), /* query NOT result */
  98.             $pageNb/*page number*/
  99.             $pageLimit /*limit per page*/
  100.         );
  101.         return $this->render('article/list.html.twig', [
  102.             'listWithThumbnails' => $listWithThumbnails,
  103.             'articles' => $articles,
  104.             'type' => $type,
  105.             'pageNb' => $pageNb,
  106.             'pageLimit' => $pageLimit,
  107.             'title' => $title
  108.         ]);
  109.     }
  110.     /**
  111.      * @Route("/a-la-une/", name="aLaUneList")
  112.      */
  113.     public function aLaUneList(PaginatorInterface $paginatorRequest $request)
  114.     {
  115.         $typeSlug "a-la-une";
  116.         $title "Toutes les unes";
  117.         return $this->showArticlesByType($request$paginator$typeSlug$titlepageLimit24,  listWithThumbnails true);
  118.     }
  119.     // pas utilisé pour l'instant
  120.     // /**
  121.     // * @Route("/a-la-une/{slug}", name="aLaUneShow")
  122.     // */
  123.     // public function aLaUneShow($slug, Request $request)
  124.     // {
  125.     //     $article = $this->getDoctrine()->getRepository(Article::class)->findOneBySlug($slug);
  126.     //     if (!$article) {
  127.     //         throw $this->createNotFoundException('Pas d\'article avec le slug "' . $slug . '".');
  128.     //     }
  129.     //     $image ="";
  130.     //     if (count($article->getMedias())) {
  131.     //         $image = $article->getMedias()[0];
  132.     //         $image = $image->getFile();
  133.     //     }
  134.     //     return $this->render('article/aLaUneShow.html.twig', [
  135.     //         'article' => $article,
  136.     //         'image' => $image,
  137.     //     ]);
  138.     // }
  139.     /**
  140.      * @Route("/publications/", name="publicationsList")
  141.      */
  142.     public function publicationsList(PaginatorInterface $paginatorRequest $request)
  143.     {
  144.         $typeSlug "article-fond";
  145.         $title "Toutes les publications";
  146.         return $this->showArticlesByType($request$paginator$typeSlug$title);
  147.     }
  148.     /**
  149.      * @Route("/articles-fond/", name="articlesFondList")
  150.      */
  151.     public function articlesFondList(PaginatorInterface $paginatorRequest $request)
  152.     {
  153.         $typeSlug "article-fond";
  154.         $title "Toutes les publications";
  155.         return $this->showArticlesByType($request$paginator$typeSlug$titlepageLimit24,  listWithThumbnails true);
  156.     }
  157.     // pas utilisé pour l'instant
  158.     // /**
  159.     // * @Route("/publication/{id}", name="publicationShow")
  160.     // */
  161.     // public function publicationShow($id)
  162.     // {
  163.     //     $article = $this->getDoctrine()->getRepository(Article::class)->find($id);
  164.     //     if (!$article) {
  165.     //         throw $this->createNotFoundException('Pas d\'article avec l\'id ' . $id . '.');
  166.     //     }
  167.     //     return $this->render('article/publicationShow.html.twig', [
  168.     //         'article' => $article
  169.     //     ]);
  170.     // }
  171.     /**
  172.      * utilisée pour les résultats de recherche
  173.      * @Route("/article/teasers/{ids}", name="showSearchResult")
  174.      * @param string $ids liste d'ids séparées par des tirets 
  175.      * @param bool $listWithThumbnails si on veut afficher une liste d'images, sinon liste textuelle 
  176.      */
  177.     public function showSearchResult($ids$listWithThumbnails false)
  178.     {
  179.         $ids explode("-"$ids); // $ids devient un array d'ids
  180.         $articles = [];
  181.         foreach ($ids as $id) {
  182.             $article $this->getDoctrine()->getRepository(Article::class)->find($id);
  183.             if($article) {
  184.                 $key sprintf('%08d'$article->getPriority()) . "_" substr($article->getName(), 020) . "_" sprintf('%08d'$article->getId()); // pour ksort par la suite
  185.                 if(! isset($articles[$key])) $articles[$key] = $article;
  186.             }
  187.         }
  188.         krsort($articles);
  189.         return $this->render('article/searchResult.html.twig', [
  190.             'listWithThumbnails' => $listWithThumbnails,
  191.             'articles' => $articles
  192.         ]);
  193.     }
  194. }