<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Article;
use App\Entity\ArticleType;
use App\Entity\Media;
use App\Form\UpdateArticleType;
use Knp\Component\Pager\PaginatorInterface;
class ArticleController extends AbstractController
{
/**
* @Route("/article/{id}", name="showArticle", requirements={"id"="\d+"})
*/
public function showArticle(Request $request, $id)
{
$article = $this->getDoctrine()->getRepository(Article::class)->find($id);
if (!$article) {
throw $this->createNotFoundException('Pas d\'article avec l\'id ' . $id . '.');
}
$slug = $article->getSlug();
if($slug && $slug !== "") {
return $this->redirect($this->generateUrl('showArticleBySlug', ['slug' => $slug]));
} else {
if(!$article->getPublished()) {
$redirectUrl = $this->_notPublished($request);
if($redirectUrl) return $this->redirect($redirectUrl);
}
return $this->render('article/show.html.twig', [
'article' => $article
]);
}
}
/**
* @Route("/article/{slug}", name="showArticleBySlug")
*/
public function showArticleBySlug(Request $request, $slug)
{
$article = $this->getDoctrine()->getRepository(Article::class)->findOneBySlug($slug);
if (!$article) {
throw $this->createNotFoundException('Pas d\'article avec le slug "' . $slug . '".');
}
if(!$article->getPublished()) {
$redirectUrl = $this->_notPublished($request);
if($redirectUrl) return $this->redirect($redirectUrl);
}
return $this->render('article/show.html.twig', [
'article' => $article,
]);
}
/**
* action if entity not published
* @return string $redirectUrl
*/
private function _notPublished($request) {
if(!$this->isGranted('ROLE_ADMIN')) {
$requestUri = $request->getRequestUri();
$baseUrl = $request->getSchemeAndHttpHost();
$this->addFlash(
'error-popup',
'L\'url demandée : "' . $baseUrl . $requestUri . '" n\'existe pas.'
);
$referer = $request->headers->get('referer');
if(null != $referer && substr($referer, 0, strlen($baseUrl)) === $baseUrl) {
// on retourne de là oú l'on vient, si l'on vient de ce site
$redirectUrl = substr($referer, strlen($baseUrl));
} else {
// pas de referer trouvé ou appel depuis site extérieur, on va donc à la home
$redirectUrl = $this->generateUrl('home');
}
} else {
// rôle admin, on laisse consulter
$this->addFlash(
'notice',
'Cette question-réponse n\'est pas publiée. En tant qu\'admin vous pouvez toutefois la voir.'
);
$redirectUrl = null;
}
return $redirectUrl;
}
/**
* @Route("/articles/{typeSlug}/{title}", name="showArticlesByType", requirements={"typeSlug"="{'article-fond','publication','a-la-une'}"})
* @param bool $listWithThumbnails si on veut afficher une liste d'images, sinon liste textuelle
*/
public function showArticlesByType(Request $request, PaginatorInterface $paginator, $typeSlug, $title = null, $pageLimit = 15, $listWithThumbnails = false)
{
$articleTypeRepository = $this->getDoctrine()->getRepository(ArticleType::class);
$type = $articleTypeRepository->findOneBySlug($typeSlug);
if (!$type) {
throw $this->createNotFoundException('Pas de type d\'article avec le slug ' . $typeSlug . '.');
}
$articleRepository = $this->getDoctrine()->getRepository(Article::class);
$pageNb = $request->query->getInt('page', 1);
$articles = $paginator->paginate(
$articleRepository->getQueryByType($type), /* query NOT result */
$pageNb, /*page number*/
$pageLimit /*limit per page*/
);
return $this->render('article/list.html.twig', [
'listWithThumbnails' => $listWithThumbnails,
'articles' => $articles,
'type' => $type,
'pageNb' => $pageNb,
'pageLimit' => $pageLimit,
'title' => $title
]);
}
/**
* @Route("/a-la-une/", name="aLaUneList")
*/
public function aLaUneList(PaginatorInterface $paginator, Request $request)
{
$typeSlug = "a-la-une";
$title = "Toutes les unes";
return $this->showArticlesByType($request, $paginator, $typeSlug, $title, pageLimit: 24, listWithThumbnails : true);
}
// pas utilisé pour l'instant
// /**
// * @Route("/a-la-une/{slug}", name="aLaUneShow")
// */
// public function aLaUneShow($slug, Request $request)
// {
// $article = $this->getDoctrine()->getRepository(Article::class)->findOneBySlug($slug);
// if (!$article) {
// throw $this->createNotFoundException('Pas d\'article avec le slug "' . $slug . '".');
// }
// $image ="";
// if (count($article->getMedias())) {
// $image = $article->getMedias()[0];
// $image = $image->getFile();
// }
// return $this->render('article/aLaUneShow.html.twig', [
// 'article' => $article,
// 'image' => $image,
// ]);
// }
/**
* @Route("/publications/", name="publicationsList")
*/
public function publicationsList(PaginatorInterface $paginator, Request $request)
{
$typeSlug = "article-fond";
$title = "Toutes les publications";
return $this->showArticlesByType($request, $paginator, $typeSlug, $title);
}
/**
* @Route("/articles-fond/", name="articlesFondList")
*/
public function articlesFondList(PaginatorInterface $paginator, Request $request)
{
$typeSlug = "article-fond";
$title = "Toutes les publications";
return $this->showArticlesByType($request, $paginator, $typeSlug, $title, pageLimit: 24, listWithThumbnails : true);
}
// pas utilisé pour l'instant
// /**
// * @Route("/publication/{id}", name="publicationShow")
// */
// public function publicationShow($id)
// {
// $article = $this->getDoctrine()->getRepository(Article::class)->find($id);
// if (!$article) {
// throw $this->createNotFoundException('Pas d\'article avec l\'id ' . $id . '.');
// }
// return $this->render('article/publicationShow.html.twig', [
// 'article' => $article
// ]);
// }
/**
* utilisée pour les résultats de recherche
* @Route("/article/teasers/{ids}", name="showSearchResult")
* @param string $ids liste d'ids séparées par des tirets
* @param bool $listWithThumbnails si on veut afficher une liste d'images, sinon liste textuelle
*/
public function showSearchResult($ids, $listWithThumbnails = false)
{
$ids = explode("-", $ids); // $ids devient un array d'ids
$articles = [];
foreach ($ids as $id) {
$article = $this->getDoctrine()->getRepository(Article::class)->find($id);
if($article) {
$key = sprintf('%08d', $article->getPriority()) . "_" . substr($article->getName(), 0, 20) . "_" . sprintf('%08d', $article->getId()); // pour ksort par la suite
if(! isset($articles[$key])) $articles[$key] = $article;
}
}
krsort($articles);
return $this->render('article/searchResult.html.twig', [
'listWithThumbnails' => $listWithThumbnails,
'articles' => $articles
]);
}
}