ldc-bien-etre-animal/app/Http/Controllers/Front/NewsController.php
2026-09-07 12:15:23 +02:00

127 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Front;
use App\Services\NewsRepository;
use Inertia\Inertia;
use Inertia\Response;
use Spatie\SchemaOrg\Schema;
use Uwil\LaravelCommon\Controllers\CommonController;
use Uwil\LaravelSchemaOrg\Services\SchemaOrgService;
class NewsController extends CommonController
{
public function index(SchemaOrgService $schemaOrg): Response
{
$trans = trans('news_page');
/** @var array<string, string> $themesData */
$themesData = $trans['themes'];
$themes = collect($themesData)
->map(fn($label, $value) => ['label' => $label, 'value' => $value])
->values()
->all();
/** @var array<string, string> $monthsData */
$monthsData = $trans['months'];
$months = collect($monthsData)
->map(fn($label, $value) => ['label' => $label, 'value' => $value])
->values()
->all();
$newsTrans = trans('news_page');
return Inertia::render('front/Pages/News', [
'themes' => $themes,
'months' => $months,
'articles' => collect(NewsRepository::all())
->map(fn($a) => [
'slug' => $a['slug'],
'title' => $a['title'],
'shortTitle' => $a['shortTitle'],
'date' => $a['date'],
'theme' => $a['theme'],
'month' => $a['month'],
'rotation' => $a['rotation'],
'category' => $newsTrans['themes'][$a['theme']],
])
->all(),
'schemas' => [
$schemaOrg->setWebPage()
->headline($trans['meta']['seo_title'])
->description($trans['meta']['seo_description'])
->name($trans['header']['title'])
->isAccessibleForFree(true)
->keywords(['page'])
->dateCreated(new \DateTimeImmutable('2026-09-03'))
->dateModified(new \DateTimeImmutable('2026-09-03'))
->datePublished(new \DateTimeImmutable('2026-09-03')),
],
]);
}
public function show(string $slug, SchemaOrgService $schemaOrg): Response
{
$article = NewsRepository::findBySlug($slug);
abort_if($article === null, 404);
return Inertia::render('front/Pages/NewsDetail', [
'seo_title' => $article['seo_title'],
'seo_description' => $article['seo_description'],
'slug' => $article['slug'],
'title' => $article['title'],
'shortTitle' => $article['shortTitle'],
'meta' => $article['meta'] ?? null,
'type' => $article['type'],
'photos' => $article['photos'] ?? [],
'blocks' => $article['blocks'] ?? [],
'relatedArticles' => $this->getRelatedArticles($slug),
'schemas' => [
$schemaOrg->setWebPage()
->headline($article['seo_title'])
->description($article['seo_description'])
->name($article['title'])
->isAccessibleForFree(true)
->keywords(['page'])
->dateCreated(new \DateTimeImmutable('2026-09-03'))
->dateModified(new \DateTimeImmutable('2026-09-03'))
->datePublished(new \DateTimeImmutable('2026-09-03')),
Schema::NewsArticle()
->name($article['title'])
->url(route('front.news.show', $article['slug']))
->dateCreated(new \DateTimeImmutable('2026-09-03'))
->dateModified(new \DateTimeImmutable('2026-09-03'))
->datePublished(new \DateTimeImmutable('2026-09-03'))
->mainEntityOfPage($schemaOrg->getWebPage())
->author($schemaOrg->getOrganizationSchema())
->creator($schemaOrg->getOrganizationSchema())
->isPartOf($schemaOrg->getWebsiteSchema()),
],
]);
}
/**
* @return array<int, array<string, mixed>>
*/
private function getRelatedArticles(string $excludeSlug): array
{
$newsTrans = trans('news_page');
return collect(NewsRepository::all())
->reject(fn($a) => $a['slug'] === $excludeSlug)
->take(3)
->map(fn($a) => [
'slug' => $a['slug'],
'title' => $a['title'],
'shortTitle' => $a['shortTitle'],
'date' => $a['date'],
'rotation' => $a['rotation'],
'category' => $newsTrans['themes'][$a['theme']],
])
->values()
->all();
}
}