pp-planer/app/Http/Controllers/SongController.php
Thorsten Bus ae42b48753 feat(songs): per-song sections + section editing; fix CCLI import bugs
Refactor lyric storage so each song owns its sections instead of sharing
global labels. Adds song_sections (per song+label) owning song_slides;
labels stay global ProPresenter group tags (name/color/macro). Arrangements
now reference sections, so editing/importing one song no longer corrupts
others that share a label name.

- New: song_sections table + migration with safe backfill; SongSection,
  SongArrangementSection models; SongSectionController (edit/add/delete
  sections, immediate persistence) wired into SongEditModal.
- Refactor writers/readers: CcliImport, ProImport, SongService,
  ArrangementController, SongController, ProExport, PDF, Translation
  (translation reset now section-scoped), CCLI pairing.
- CCLI import fixes: parse SongSelect copy-icon format (German "Vers"
  abbrev + trailing author), fill empty CTS-synced songs instead of
  blocking as duplicate, distinct label colors per section kind,
  import&edit/existing-song open the edit modal (no 404/405), teleport
  paste dialog above assign dialog, preview shows section content,
  correct SongSelect search URL, copy-icon instructions.
- Bookmarklet clicks #generalCopyLyricsButton and captures clipboard;
  serves correct host from request.
- Export: embed key-visual/background under fixed bundle-relative names.
- Tests updated for the section model; new section + isolation coverage.
2026-05-31 14:45:47 +02:00

172 lines
6.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\SongRequest;
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();
if ($search = $request->input('search')) {
$query->where(function ($q) use ($search) {
$q->where('title', 'like', "%{$search}%")
->orWhere('ccli_id', 'like', "%{$search}%");
});
}
$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,
'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,
'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,
'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(),
];
}
}