<?php
namespace App\Controller\Frontend;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Article;
use App\Repository\ArticleRepository;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Knp\Component\Pager\PaginatorInterface;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
class ActualiteController extends AbstractController
{
/**
* @Route("/actualite-{slug}-{id}.html", name="actualite")
*/
public function actualite(EntityManagerInterface $em, $id, Request $request,UploaderHelper $helper): Response
{
$article = $em->getRepository(Article::class)->findOneById($id);
if($article == null || $article->getIsActive() == false)
throw $this->createNotFoundException('La page n\'existe pas');
$img_path = $helper->asset($article);
$text = html_entity_decode(str_replace(array("\r", "\n"), '',strip_tags($article->getText())), ENT_QUOTES | ENT_HTML5, 'UTF-8');
$jsonLd = [
"@context"=> "https://schema.org",
"@type"=> "BlogPosting",
"headline"=> $article->getTitle(),
"image"=> "https://{$_SERVER['HTTP_HOST']}{$img_path}",
"wordcount"=> str_word_count($text),
"publisher"=> "Croqvacances",
"url"=> $request->getUri(),
"datePublished"=> $article->getCreationDate()->format('Y-m-d'),
"description"=> substr($text, 0, 100),
"articleBody"=> $text,
];
$jsonLdScript = sprintf('<script type="application/ld+json">%s</script>', json_encode($jsonLd, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
return $this->render('frontend/actualite/showArticle.html.twig',['article' => $article,'jsonLdScript' => $jsonLdScript,]);
}
/**
* @Route("/actualites", name="frontend_actualites",options={"sitemap" = true})
*/
public function actualites(ArticleRepository $ar, PaginatorInterface $paginator, EntityManagerInterface $em, Request $request): Response{
$dql = "SELECT a FROM App\Entity\Article a WHERE a.isActive = 1 AND a.type = 'article' ORDER BY a.creationDate DESC";
$query = $em->createQuery($dql);
$pagination = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1), /* page number */
6 /* limit per page */
);
return $this->render('frontend/actualite/articles.html.twig',['pagination' => $pagination]);
}
}