ldc-bien-etre-animal/app/Http/Controllers/Front/CommunityController.php
2026-09-04 17:29:30 +02:00

151 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Front;
use Inertia\Inertia;
use Inertia\Response;
use Uwil\LaravelCommon\Controllers\CommonController;
use Uwil\LaravelSchemaOrg\Services\SchemaOrgService;
class CommunityController extends CommonController
{
public function index(SchemaOrgService $schemaOrg): 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'];
$communitiesData = 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();
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' => $communitiesData,
'communitiesCount' => count($communitiesData),
'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
{
$trans = trans('community_detail_page');
$community = $trans['communities'][$slug] ?? null;
abort_unless($community, 404);
$realSlug = $community['redirect_slug'] ?? $slug;
$community = $trans['communities'][$realSlug];
$otherCommunities = $this->getOtherCommunities($trans, $slug);
return Inertia::render('front/Pages/Community/DetailCommunity', [
'metaTitle' => $community['meta']['seo_title'],
'metaDescription' => $community['meta']['seo_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' => $otherCommunities,
'otherCommunitiesCount' => count($otherCommunities),
'governanceHref' => route('front.gouvernance.index'),
'sections' => $community['sections'] ?? [],
'image' => $community['image'] ?? null,
'imageCaption' => $community['image_caption'] ?? null,
'schemas' => [
$schemaOrg->setWebPage()
->headline($community['meta']['seo_title'])
->description($community['meta']['seo_description'])
->name($community['header']['title'])
->isAccessibleForFree(true)
->keywords(['page'])
->dateCreated(new \DateTimeImmutable('2026-09-03'))
->dateModified(new \DateTimeImmutable('2026-09-03'))
->datePublished(new \DateTimeImmutable('2026-09-03')),
],
]);
}
/**
* @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();
}
}