ldc-bien-etre-animal/app/Http/Controllers/Front/NewsController.php
2026-09-01 15:04:11 +02:00

85 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Front;
use App\Services\NewsRepository;
use Inertia\Inertia;
use Inertia\Response;
use Uwil\LaravelCommon\Controllers\CommonController;
class NewsController extends CommonController
{
public function index(): 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();
return Inertia::render('front/Pages/News', [
'themes' => $themes,
'months' => $months,
'articles' => collect(NewsRepository::all())
->map(fn ($a) => [
'slug' => $a['slug'],
'title' => $a['title'],
'excerpt' => $a['excerpt'],
'date' => $a['date'],
'theme' => $a['theme'],
'month' => $a['month'],
'rotation' => $a['rotation'],
])
->all(),
]);
}
public function show(string $slug): Response
{
$article = NewsRepository::findBySlug($slug);
abort_if($article === null, 404);
return Inertia::render('front/Pages/NewsDetail', [
'slug' => $article['slug'],
'title' => $article['title'],
'meta' => $article['meta'] ?? null,
'type' => $article['type'],
'photos' => $article['photos'] ?? [],
'blocks' => $article['blocks'] ?? [],
'relatedArticles' => $this->getRelatedArticles($slug),
]);
}
/**
* @return array<int, array<string, mixed>>
*/
private function getRelatedArticles(string $excludeSlug): array
{
return collect(NewsRepository::all())
->reject(fn ($a) => $a['slug'] === $excludeSlug)
->take(3)
->map(fn ($a) => [
'slug' => $a['slug'],
'title' => $a['title'],
'excerpt' => $a['excerpt'],
'date' => $a['date'],
'rotation' => $a['rotation'],
])
->values()
->all();
}
}