pp-planer/app/Http/Controllers/TranslationController.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

124 lines
3.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Song;
use App\Services\TranslationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
class TranslationController extends Controller
{
public function __construct(
private readonly TranslationService $translationService,
) {}
public function page(Song $song): Response
{
$song->load([
'arrangements' => fn ($q) => $q->where('is_default', true),
'arrangements.arrangementSections' => fn ($q) => $q->orderBy('order'),
'arrangements.arrangementSections.section.slides',
'arrangements.arrangementSections.section.label',
]);
$defaultArr = $song->arrangements->firstWhere('is_default', true) ?? $song->arrangements->first();
$groups = collect();
if ($defaultArr !== null) {
$groups = $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,
])->values()
: collect(),
]);
}
return Inertia::render('Songs/Translate', [
'song' => [
'id' => $song->id,
'title' => $song->title,
'ccli_id' => $song->ccli_id,
'has_translation' => $song->has_translation,
'groups' => $groups,
],
'prefilledTranslation' => session()->pull('ccli_prefilled'),
]);
}
public function fetchUrl(Request $request): JsonResponse
{
$request->validate([
'url' => ['required', 'url'],
]);
$text = $this->translationService->fetchFromUrl($request->input('url'));
if ($text === null) {
return response()->json([
'message' => 'Konnte Text nicht abrufen',
], 422);
}
return response()->json([
'text' => $text,
]);
}
public function import(int $songId, Request $request): JsonResponse
{
$song = Song::find($songId);
if (! $song) {
return response()->json([
'message' => 'Song nicht gefunden',
], 404);
}
$request->validate([
'text' => ['required', 'string'],
]);
$this->translationService->importTranslation($song, $request->input('text'));
return response()->json([
'message' => 'Übersetzung erfolgreich importiert',
]);
}
public function destroy(int $songId): JsonResponse
{
$song = Song::find($songId);
if (! $song) {
return response()->json([
'message' => 'Song nicht gefunden',
], 404);
}
$this->translationService->removeTranslation($song);
return response()->json([
'message' => 'Übersetzung entfernt',
]);
}
}