ldc-bien-etre-animal/app/Http/Controllers/Front/NewsController.php

128 lines
4.9 KiB
PHP
Raw Permalink Normal View History

2026-08-28 08:40:04 +00:00
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Front;
use App\Services\NewsRepository;
use Inertia\Inertia;
use Inertia\Response;
2026-09-03 16:52:27 +02:00
use Spatie\SchemaOrg\Schema;
2026-08-28 08:40:04 +00:00
use Uwil\LaravelCommon\Controllers\CommonController;
2026-09-03 16:52:27 +02:00
use Uwil\LaravelSchemaOrg\Services\SchemaOrgService;
2026-08-28 08:40:04 +00:00
class NewsController extends CommonController
{
2026-09-03 16:52:27 +02:00
public function index(SchemaOrgService $schemaOrg): Response
2026-08-28 08:40:04 +00:00
{
$trans = trans('news_page');
/** @var array<string, string> $themesData */
$themesData = $trans['themes'];
$themes = collect($themesData)
2026-09-07 12:15:23 +02:00
->map(fn($label, $value) => ['label' => $label, 'value' => $value])
2026-08-28 08:40:04 +00:00
->values()
->all();
/** @var array<string, string> $monthsData */
$monthsData = $trans['months'];
2026-08-31 16:54:51 +02:00
$months = collect($monthsData)
2026-09-07 12:15:23 +02:00
->map(fn($label, $value) => ['label' => $label, 'value' => $value])
2026-08-28 08:40:04 +00:00
->values()
->all();
2026-09-04 16:54:02 +02:00
$newsTrans = trans('news_page');
2026-08-28 08:40:04 +00:00
return Inertia::render('front/Pages/News', [
2026-08-31 16:54:51 +02:00
'themes' => $themes,
'months' => $months,
2026-08-28 08:40:04 +00:00
'articles' => collect(NewsRepository::all())
2026-09-07 12:15:23 +02:00
->map(fn($a) => [
2026-09-04 16:54:02 +02:00
'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']],
2026-08-28 08:40:04 +00:00
])
->all(),
2026-09-03 16:52:27 +02:00
'schemas' => [
$schemaOrg->setWebPage()
->headline($trans['meta']['seo_title'])
->description($trans['meta']['seo_description'])
->name($trans['header']['title'])
->isAccessibleForFree(true)
->keywords(['page'])
2026-09-04 12:15:37 +02:00
->dateCreated(new \DateTimeImmutable('2026-09-03'))
->dateModified(new \DateTimeImmutable('2026-09-03'))
->datePublished(new \DateTimeImmutable('2026-09-03')),
],
2026-08-28 08:40:04 +00:00
]);
}
2026-09-03 16:52:27 +02:00
public function show(string $slug, SchemaOrgService $schemaOrg): Response
2026-08-28 08:40:04 +00:00
{
$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'],
2026-08-31 16:54:51 +02:00
'slug' => $article['slug'],
'title' => $article['title'],
2026-09-04 16:54:02 +02:00
'shortTitle' => $article['shortTitle'],
2026-08-31 16:54:51 +02:00
'meta' => $article['meta'] ?? null,
'type' => $article['type'],
'photos' => $article['photos'] ?? [],
'blocks' => $article['blocks'] ?? [],
'relatedArticles' => $this->getRelatedArticles($slug),
2026-09-03 16:52:27 +02:00
'schemas' => [
$schemaOrg->setWebPage()
2026-09-07 10:09:49 +02:00
->headline($article['seo_title'])
->description($article['seo_description'])
2026-09-03 16:52:27 +02:00
->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()),
],
2026-08-28 08:40:04 +00:00
]);
}
/**
* @return array<int, array<string, mixed>>
*/
private function getRelatedArticles(string $excludeSlug): array
{
2026-09-04 16:54:02 +02:00
$newsTrans = trans('news_page');
2026-08-28 08:40:04 +00:00
return collect(NewsRepository::all())
2026-09-07 12:15:23 +02:00
->reject(fn($a) => $a['slug'] === $excludeSlug)
2026-08-28 08:40:04 +00:00
->take(3)
2026-09-07 12:15:23 +02:00
->map(fn($a) => [
2026-09-04 16:54:02 +02:00
'slug' => $a['slug'],
'title' => $a['title'],
'shortTitle' => $a['shortTitle'],
'date' => $a['date'],
'rotation' => $a['rotation'],
'category' => $newsTrans['themes'][$a['theme']],
2026-08-28 08:40:04 +00:00
])
->values()
->all();
}
}