Files
crewli/api/app/Http/Controllers/Api/V1/FestivalSectionController.php
bert.hausmans c21bc085e9 feat: registration section preferences with show_in_registration filtering and deduplication
Add show_in_registration and registration_description columns to festival_sections.
Registration form now shows deduplicated sections by name (across sub-events),
filtered by show_in_registration=true, grouped by category with card-based UI.
Section preferences use section_name instead of section_id.
Add GET/PUT registration-settings endpoints for festival-level bulk management.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-10 20:03:54 +02:00

189 lines
6.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\V1;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\ReorderFestivalSectionsRequest;
use App\Http\Requests\Api\V1\StoreFestivalSectionRequest;
use App\Http\Requests\Api\V1\UpdateFestivalSectionRequest;
use App\Http\Requests\Api\V1\UpdateRegistrationSettingsRequest;
use App\Http\Resources\Api\V1\FestivalSectionResource;
use App\Models\Event;
use App\Models\FestivalSection;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Support\Facades\Gate;
final class FestivalSectionController extends Controller
{
public function index(Event $event): AnonymousResourceCollection
{
Gate::authorize('viewAny', [FestivalSection::class, $event]);
$sections = $event->festivalSections()->ordered()->get();
// For sub-events, also include cross_event sections from the parent festival
if ($event->isSubEvent()) {
$parentCrossEventSections = $event->parent
->festivalSections()
->where('type', 'cross_event')
->ordered()
->get();
$sections = $parentCrossEventSections->merge($sections)->sortBy('sort_order')->values();
}
return FestivalSectionResource::collection($sections);
}
public function store(StoreFestivalSectionRequest $request, Event $event): JsonResponse
{
Gate::authorize('create', [FestivalSection::class, $event]);
$data = $request->validated();
$redirectedToParent = false;
if (($data['type'] ?? 'standard') === 'cross_event') {
if ($event->isFlatEvent()) {
return $this->error(
'Overkoepelende secties kunnen alleen worden aangemaakt bij festivals met programmaonderdelen.',
422,
);
}
if ($event->isSubEvent()) {
$event = $event->parent;
Gate::authorize('create', [FestivalSection::class, $event]);
$redirectedToParent = true;
}
}
$section = $event->festivalSections()->create($data);
$response = $this->created(new FestivalSectionResource($section));
if ($redirectedToParent) {
$original = $response->getData(true);
$original['meta'] = [
'redirected_to_parent' => true,
'parent_event_name' => $event->name,
];
return response()->json($original, 201);
}
return $response;
}
public function update(UpdateFestivalSectionRequest $request, Event $event, FestivalSection $section): JsonResponse
{
Gate::authorize('update', [$section, $event]);
$section->update($request->validated());
return $this->success(new FestivalSectionResource($section->fresh()));
}
public function destroy(Event $event, FestivalSection $section): JsonResponse
{
Gate::authorize('delete', [$section, $event]);
$section->delete();
return response()->json(null, 204);
}
public function registrationSettings(Event $event): JsonResponse
{
Gate::authorize('viewAny', [FestivalSection::class, $event]);
$sections = $this->getFestivalSections($event);
$grouped = $sections->groupBy('name')->map(function ($group) {
$first = $group->first();
return [
'name' => $first->name,
'category' => $first->category,
'icon' => $first->icon,
'show_in_registration' => $group->contains('show_in_registration', true),
'registration_description' => $group->whereNotNull('registration_description')->first()?->registration_description,
'section_count' => $group->count(),
'section_ids' => $group->pluck('id')->values()->toArray(),
];
})->values();
return response()->json(['data' => $grouped]);
}
public function updateRegistrationSettings(UpdateRegistrationSettingsRequest $request, Event $event): JsonResponse
{
Gate::authorize('create', [FestivalSection::class, $event]);
$validated = $request->validated();
$sections = $this->getFestivalSections($event);
$matching = $sections->where('name', $validated['name']);
if ($matching->isEmpty()) {
return $this->error('Sectie niet gevonden.', 404);
}
FestivalSection::whereIn('id', $matching->pluck('id'))
->update([
'show_in_registration' => $validated['show_in_registration'],
'registration_description' => $validated['registration_description'],
]);
activity('section_management')
->performedOn($event)
->causedBy(auth()->user())
->withProperties([
'section_name' => $validated['name'],
'show_in_registration' => $validated['show_in_registration'],
'sections_updated' => $matching->count(),
])
->log('section.registration_settings_updated');
// Return updated settings
return $this->registrationSettings($event);
}
/**
* Get all sections across the festival context (parent + children).
*/
private function getFestivalSections(Event $event): \Illuminate\Support\Collection
{
$eventIds = collect([$event->id]);
if ($event->isSubEvent()) {
$parentId = $event->parent_event_id;
$eventIds = Event::where('parent_event_id', $parentId)
->orWhere('id', $parentId)
->pluck('id');
} elseif ($event->hasChildren()) {
$childIds = $event->children()->pluck('id');
$eventIds = $childIds->push($event->id);
}
return FestivalSection::whereIn('event_id', $eventIds)->ordered()->get();
}
public function reorder(ReorderFestivalSectionsRequest $request, Event $event): JsonResponse
{
Gate::authorize('reorder', [FestivalSection::class, $event]);
foreach ($request->validated('sections') as $index => $id) {
$event->festivalSections()
->where('id', $id)
->update(['sort_order' => $index]);
}
$sections = $event->festivalSections()->ordered()->get();
return $this->success(FestivalSectionResource::collection($sections));
}
}