pp-planer/app/Http/Controllers/SongController.php
Thorsten Bus e33418f716 feat: song pre/postfix, settings overhaul, export & schedule fixes
Resolves a batch of bugs and feature requests across songs, services,
settings and export:

Songs & sections
- Every song now carries permanent, empty, locked PREFIX (COPYRIGHT) and
  POSTFIX (BLANK) sections, deduplicated on import; locked sections cannot
  be edited or deleted via UI or API.
- Song edit modal: explicit Speichern/Schließen with dirty-tracking,
  editable section headline (combobox + custom values), and a fix for the
  419 CSRF errors after CCLI "Importieren & Bearbeiten" (token read fresh
  per request).
- CCLI bookmarklet "Importieren & Bearbeiten" now opens the edit dialog.

Service schedule & arrangements
- Fixed assigned songs showing no sections (slides loaded for all
  arrangements, not just the default).
- Added "Song entfernen / neu zuordnen" to reassign an assigned song.
- Worship-leader arrangement is created/selected lazily when the
  arrangement dialog opens (only when not user-overridden); the leader is
  resolved from the "Lobpreis" agenda item, and manual create/clone names
  are prefixed with the leader name.

Navigation
- "/" redirects to the next upcoming service's edit page (or the list).
- Service titles link to the edit page.

Settings
- Renamed "Makro-Import"/"Label-Import" menu items; fixed drag-and-drop
  imports (were downloading the dropped file); added label-import hint;
  made the panel scrollable.
- Nametag now uses a single MacroPicker; added song prefix/postfix label
  defaults (COPYRIGHT #24B34C / BLANK #000000); new "Export-Dateien" menu
  to upload prefix/postfix .pro files added to every export.

Export
- Filenames/playlist names are date-first ("YYYY-MM-DD <Title>").
- Keyvisual slide only for the first content-less item after real content;
  all other content-less items render as headlines.
- New "Vorschau herunterladen" for non-finalized services (filename and
  import name prefixed "Vorschau" with export timestamp).
- Uploaded prefix/postfix .pro files wrap every export.

Tests updated to the new behavior; full suite green (569 passed).
2026-06-01 08:56:20 +02:00

191 lines
7.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\SongRequest;
use App\Models\Label;
use App\Models\Song;
use App\Services\SongService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SongController extends Controller
{
public function __construct(
private readonly SongService $songService,
) {}
public function index(Request $request): JsonResponse
{
$query = Song::query()
->withCount(['sections as content_slides_count' => fn ($q) => $q->has('slides')]);
if ($search = $request->input('search')) {
$query->where(function ($q) use ($search) {
$q->where('title', 'like', "%{$search}%")
->orWhere('ccli_id', 'like', "%{$search}%");
});
}
// The SongDB UI sends with_content=1 by default to hide songs without content;
// pass with_content=0 (or omit) to include empty songs.
if ($request->boolean('with_content')) {
$query->whereHas('sections', fn ($q) => $q->has('slides'));
}
$songs = $query->orderBy('title')
->paginate($request->input('per_page', 20));
return response()->json([
'data' => $songs->map(fn (Song $song) => [
'id' => $song->id,
'title' => $song->title,
'ccli_id' => $song->ccli_id,
'author' => $song->author,
'has_translation' => $song->has_translation,
'has_content' => (int) $song->content_slides_count > 0,
'last_used_at' => $song->last_used_at?->toDateString(),
'last_used_in_service' => $song->last_used_in_service,
'created_at' => $song->created_at->toDateTimeString(),
'updated_at' => $song->updated_at->toDateTimeString(),
]),
'meta' => [
'current_page' => $songs->currentPage(),
'last_page' => $songs->lastPage(),
'per_page' => $songs->perPage(),
'total' => $songs->total(),
],
]);
}
public function store(SongRequest $request): JsonResponse
{
$song = DB::transaction(function () use ($request) {
$song = Song::create($request->validated());
$this->songService->createDefaultArrangement($song);
return $song;
});
return response()->json([
'message' => 'Song erfolgreich erstellt',
'data' => $this->formatSongDetail($song->fresh(['arrangements.arrangementSections.section.slides', 'arrangements.arrangementSections.section.label'])),
], 201);
}
public function show(int $id): JsonResponse
{
$song = Song::with(['arrangements.arrangementSections.section.slides', 'arrangements.arrangementSections.section.label'])->find($id);
if (! $song) {
return response()->json(['message' => 'Song nicht gefunden'], 404);
}
return response()->json([
'data' => $this->formatSongDetail($song),
]);
}
public function update(SongRequest $request, int $id): JsonResponse
{
$song = Song::find($id);
if (! $song) {
return response()->json(['message' => 'Song nicht gefunden'], 404);
}
$song->update($request->validated());
return response()->json([
'message' => 'Song erfolgreich aktualisiert',
'data' => $this->formatSongDetail($song->fresh(['arrangements.arrangementSections.section.slides', 'arrangements.arrangementSections.section.label'])),
]);
}
public function destroy(int $id): JsonResponse
{
$song = Song::find($id);
if (! $song) {
return response()->json(['message' => 'Song nicht gefunden'], 404);
}
$song->delete();
return response()->json([
'message' => 'Song erfolgreich gelöscht',
]);
}
public function formatSongDetail(Song $song): array
{
$defaultArr = $song->arrangements->firstWhere('is_default', true);
$groupsPayload = [];
if ($defaultArr !== null) {
$groupsPayload = $defaultArr->arrangementSections
->sortBy('order')
->values()
->map(fn ($arrangementSection) => [
'id' => $arrangementSection->section?->id,
'section_id' => $arrangementSection->section?->id,
'label_id' => $arrangementSection->section?->label_id,
'name' => $arrangementSection->section?->label?->name,
'color' => $arrangementSection->section?->label?->color,
'order' => $arrangementSection->order,
'locked' => (bool) ($arrangementSection->section?->locked ?? false),
'slides' => $arrangementSection->section
? $arrangementSection->section->slides
->sortBy('order')
->values()
->map(fn ($slide) => [
'id' => $slide->id,
'order' => $slide->order,
'text_content' => $slide->text_content,
'text_content_translated' => $slide->text_content_translated,
'notes' => $slide->notes,
])->toArray()
: [],
])->toArray();
}
return [
'id' => $song->id,
'title' => $song->title,
'ccli_id' => $song->ccli_id,
'author' => $song->author,
'copyright_text' => $song->copyright_text,
'copyright_year' => $song->copyright_year,
'publisher' => $song->publisher,
'has_translation' => $song->has_translation,
'last_used_at' => $song->last_used_at?->toDateString(),
'last_used_in_service' => $song->last_used_in_service,
'created_at' => $song->created_at->toDateTimeString(),
'updated_at' => $song->updated_at->toDateTimeString(),
'groups' => $groupsPayload,
'available_labels' => Label::query()
->whereNull('hidden_at')
->orderBy('name')
->get(['id', 'name', 'color'])
->map(fn (Label $label) => [
'id' => $label->id,
'name' => $label->name,
'color' => $label->color,
])->toArray(),
'arrangements' => $song->arrangements->map(fn ($arr) => [
'id' => $arr->id,
'name' => $arr->name,
'is_default' => $arr->is_default,
'arrangement_groups' => $arr->arrangementSections->sortBy('order')->values()->map(fn ($arrangementSection) => [
'id' => $arrangementSection->id,
'section_id' => $arrangementSection->song_section_id,
'label_id' => $arrangementSection->section?->label_id,
'order' => $arrangementSection->order,
])->toArray(),
])->toArray(),
];
}
}