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

123 lines
5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Front;
use Inertia\Inertia;
use Inertia\Response;
use Uwil\LaravelCommon\Controllers\CommonController;
class CommunityController extends CommonController
{
public function index(): Response
{
$trans = trans('gouvernance_page');
$communityTrans = trans('community_detail_page');
/** @var array<int, array<string, mixed>> $sections */
$sections = $trans['sections'];
/** @var array<string, array<string, mixed>> $communities */
$communities = $communityTrans['communities'];
return Inertia::render('front/Pages/Community/Gouvernance', [
'sections' => collect($sections)->map(fn ($s) => [
'title' => $s['title'],
'bgColor' => $s['bg_color'],
'textColor' => $s['text_color'],
'rotation' => $s['rotation'],
'maxWidth' => $s['max_width'] ?? 'max-w-[1000px]',
'edgeOffset' => $s['edge_offset'] ?? '',
'texts' => $s['texts'],
'list' => $s['list'] ?? null,
'textAfterList' => $s['text_after_list'] ?? null,
'image' => $s['image'] ?? null,
'imageCaption' => $s['image_caption'] ?? null,
])->all(),
'communities' => collect($communities)
->only(['amont', 'transformateurs', 'aval', 'autres', 'scientifique'])
->map(fn ($c, $key) => [
'number' => $c['number'] ?? '',
'title' => $c['header']['title'] ?? '',
'rotation' => $c['rotation'] ?? 0,
'href' => route('front.communities.show', $key),
])
->values()
->all(),
]);
}
public function show(string $slug): Response
{
$trans = trans('community_detail_page');
$community = $trans['communities'][$slug] ?? null;
abort_unless($community, 404);
$entry = $trans['communities'][$slug] ?? null;
$realSlug = $entry['redirect_slug'] ?? $slug;
$community = $trans['communities'][$realSlug];
return Inertia::render('front/Pages/Community/DetailCommunity', [
'metaTitle' => $community['meta']['title'],
'metaDescription' => $community['meta']['description'],
'headerTitle' => $community['header']['title'],
'heading' => $community['heading'],
'roleTitle' => $community['role_title'],
'roleText' => $community['role_text'],
'compositionTitle' => $community['composition_title'],
'compositionContent' => $community['composition_content'],
'testimonials' => $this->getTestimonials($community),
'otherCommunities' => $this->getOtherCommunities($trans, $slug),
'governanceHref' => route('front.gouvernance.index'),
'sections' => $community['sections'] ?? [],
'image' => $community['image'] ?? null,
'imageCaption' => $community['image_caption'] ?? null,
]);
}
/**
* @param array<string, mixed> $community
* @return array<int, array<string, mixed>>
*/
private function getTestimonials(array $community): array
{
/** @var array<int, array<string, mixed>> $testimonials */
$testimonials = $community['testimonials'] ?? [];
return collect($testimonials)
->map(fn ($t) => [
'quote' => $t['quote'],
'authorName' => $t['author_name'],
'authorRole' => $t['author_role'],
'authorPhoto' => $t['author_photo'],
'logo' => $t['logo'],
])
->all();
}
/**
* @param array<string, mixed> $trans
* @return array<int, array<string, mixed>>
*/
private function getOtherCommunities(array $trans, string $excludeSlug): array
{
// Si on affiche transformateurs ou aval, on exclut les deux (contenu fusionné)
$slugsToExclude = in_array($excludeSlug, ['transformateurs', 'aval'], true)
? ['transformateurs', 'aval']
: [$excludeSlug];
/** @var array<string, array<string, mixed>> $communities */
$communities = $trans['communities'];
return collect($communities)
->only(['amont', 'transformateurs', 'aval', 'autres', 'scientifique'])
->reject(fn ($c, $key) => in_array($key, $slugsToExclude, true))
->map(fn ($c, $key) => [
'number' => $c['number'],
'title' => $c['header']['title'],
'rotation' => $c['rotation'],
'href' => route('front.communities.show', $key),
])
->values()
->all();
}
}