<?php
namespace App\Controller\Frontend;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations as Rest;
use App\Misc\ImageUrlHelper;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\Annotation\Route;
class NewsController extends BaseFrontendController
{
/**
* @Route("/{_locale}/actualites", requirements={"_locale": "en|fr"}, defaults={"_locale"="fr"}, name="news.index")
*/
public function index(
UrlGeneratorInterface $urlGenerator,
ImageUrlHelper $imageUrlHelper,
TranslatorInterface $translator
) {
$requestData['limit'] = 11;
$news = $this->currentService->getListAll($requestData);
$siteName = $translator->trans('siteName');
$metaTitle = $translator->trans(
'news.title'
);
$this->addTitle($metaTitle);
$this->setDescription($translator->trans('news.desc'));
// BreadcrumbSnippet
$breadcrumbSnippet = [
'@context' => 'https://schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => [
[
'@type' => 'ListItem',
'position' => 1,
'name' => $siteName,
'item' => $urlGenerator->generate(
'home',
[],
UrlGeneratorInterface::ABSOLUTE_URL
)
],
[
'@type' => 'ListItem',
'position' => 2,
'name' => $metaTitle,
],
]
];
// Article snippet
$pageModifyDate = new \DateTime('first day of this month');
$newsArticleRichSnippet = [
'@context' => 'https://schema.org',
'@type' => 'NewsArticle',
'headline' => $metaTitle,
'image' => [
$imageUrlHelper->getNewsImageUrl()
],
'datePublished' => '2022-09-01 00:00:00',
'dateModified' => $pageModifyDate->format('Y-m-d') . ' 00:00:00',
'author' => [
'@type' => 'Organization',
'name' => 'Estiam France',
'url' => $urlGenerator->generate(
'home',
[],
UrlGeneratorInterface::ABSOLUTE_URL
)
],
];
$data['news'] = $news;
$data['richSnippets'] = [];
$data['richSnippets'][] = $breadcrumbSnippet;
$data['richSnippets'][] = $newsArticleRichSnippet;
return $this->render(
'news/index.html.twig',
$data
);
}
/**
* @Rest\Get("/load-more", name="news.loadMore")
*/
public function loadMore(
Request $request
) {
$requestData = $request->query->all();
$requestData['initOffset'] = 11;
$news = $this->currentService->getListAll($requestData);
if (!$news) {
return $this->json([
'data' => []
]);
}
$data['list'] = $this->renderView(
'news/_load_more.html.twig',
[
'newsMore' => $news
]
);
return $this->json($data);
}
/**
* @Route("/{_locale}/actualites/{slug}-{id}", requirements={"_locale": "en|fr", "slug": "[a-zA-Z0-9\-]+","id": "[0-9]+"}, defaults={"_locale"="fr"}, name="news.detail")
*/
public function detail(
$slug,
$id,
Request $request,
UrlGeneratorInterface $urlGenerator,
ImageUrlHelper $imageUrlHelper,
TranslatorInterface $translator
) {
$news = $this->currentService->getBySlug($slug, $request);
if (!$news) throw $this->createNotFoundException('');
$siteName = $translator->trans('siteName');
$this->addTitle($news->seoTitle);
$this->setDescription($news->seoDescription);
// Breadcrumb snippet
$breadcrumbSnippet = [
'@context' => 'https://schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => [
[
'@type' => 'ListItem',
'position' => 1,
'name' => $news->seoTitle,
'item' => $urlGenerator->generate(
'home',
[],
UrlGeneratorInterface::ABSOLUTE_URL
)
],
[
'@type' => 'ListItem',
'position' => 2,
'name' => $siteName
]
]
];
// NewsArticle richSnippet ------------------------------------
$newsArticleRichSnippet = [
'@context' => 'https://schema.org',
'@type' => 'NewsArticle',
'headline' => $news->seoTitle,
'image' => [
$news->photo ?? $imageUrlHelper->getNewsImageUrl()
],
'datePublished' => $news->createdDate,
'dateModified' => $news->updatedDate,
'author' => [
'@type' => 'Organization',
'name' => $siteName,
'url' => $urlGenerator->generate(
'home',
[],
UrlGeneratorInterface::ABSOLUTE_URL
)
],
];
$data['news'] = $news;
$data['newsLasted'] = $this->currentService->getListLasted($news->id);
$data['nextNews'] = $this->newsRepo->getNextNews($news->id);
$data['richSnippets'] = [];
$data['richSnippets'][] = $breadcrumbSnippet;
$data['richSnippets'][] = $newsArticleRichSnippet;
return $this->render(
'news/detail.html.twig',
$data
);
}
}