pp-planer/app/Services/TranslationService.php
Thorsten Bus d915f8cfc2 feat: Wave 2 - Service list, Song CRUD, Slide upload, Arrangements, Song matching, Translation
T8: Service List Page
- ServiceController with index, finalize, reopen actions
- Services/Index.vue with status indicators (songs mapped/arranged, slides uploaded)
- German UI with finalize/reopen toggle buttons
- Status aggregation via SQL subqueries for efficiency
- Tests: 3 passing (46 assertions)

T9: Song CRUD Backend
- SongController with full REST API (index, store, show, update, destroy)
- SongService for default groups/arrangements creation
- SongRequest validation (title required, ccli_id unique)
- Search by title and CCLI ID
- last_used_in_service accessor via service_songs join
- Tests: 20 passing (85 assertions)

T10: Slide Upload Component
- SlideController with store, destroy, updateExpireDate
- SlideUploader.vue with vue3-dropzone drag-and-drop
- SlideGrid.vue with thumbnail grid and inline expire date editing
- Multi-format support: images (sync), PPT (async job), ZIP (extract)
- Type validation: information (global), moderation/sermon (service-specific)
- Tests: 15 passing (37 assertions)

T11: Arrangement Configurator
- ArrangementController with store, clone, update, destroy
- ArrangementConfigurator.vue with vue-draggable-plus
- Drag-and-drop arrangement editor with colored group pills
- Clone from default or existing arrangement
- Color picker for group customization
- Prevent deletion of last arrangement
- Tests: 4 passing (17 assertions)

T12: Song Matching Service
- SongMatchingService with autoMatch, manualAssign, requestCreation, unassign
- ServiceSongController API endpoints for song assignment
- Auto-match by CCLI ID during CTS sync
- Manual assignment with searchable song select
- Email request for missing songs (MissingSongRequest mailable)
- Tests: 14 passing (33 assertions)

T13: Translation Service
- TranslationService with fetchFromUrl, importTranslation, removeTranslation
- TranslationController API endpoints
- URL scraping (best-effort HTTP fetch with strip_tags)
- Line-count distribution algorithm (match original slide line counts)
- Mark song as translated, remove translation
- Tests: 18 passing (18 assertions)

All tests passing: 103/103 (488 assertions)
Build: ✓ Vite production build successful
German UI: All user-facing text in German with 'Du' form
2026-03-01 19:55:37 +01:00

100 lines
2.9 KiB
PHP

<?php
namespace App\Services;
use App\Models\Song;
use App\Models\SongSlide;
use Illuminate\Support\Facades\Http;
class TranslationService
{
/**
* Text von einer URL abrufen (Best-Effort).
*
* HTML-Tags werden entfernt, nur reiner Text zurückgegeben.
* Bei Fehlern wird null zurückgegeben, ohne Exception.
*/
public function fetchFromUrl(string $url): ?string
{
try {
$response = Http::timeout(10)->get($url);
if ($response->successful()) {
$html = $response->body();
$text = strip_tags($html);
$text = trim($text);
return $text !== '' ? $text : null;
}
} catch (\Exception) {
// Best-effort: Fehler stillschweigend behandeln
}
return null;
}
/**
* Übersetzungstext auf Slides verteilen, basierend auf der Zeilenanzahl jeder Slide.
*
* Für jede Gruppe (nach order sortiert) und jede Slide (nach order sortiert):
* Nimm so viele Zeilen aus dem übersetzten Text, wie die Original-Slide Zeilen hat.
*
* Beispiel:
* Slide 1 hat 4 Zeilen → bekommt die nächsten 4 Zeilen der Übersetzung
* Slide 2 hat 2 Zeilen → bekommt die nächsten 2 Zeilen
* Slide 3 hat 4 Zeilen → bekommt die nächsten 4 Zeilen
*/
public function importTranslation(Song $song, string $text): void
{
$translatedLines = explode("\n", $text);
$offset = 0;
// Alle Gruppen nach order sortiert laden, mit Slides
$groups = $song->groups()->orderBy('order')->with([
'slides' => fn ($query) => $query->orderBy('order'),
])->get();
foreach ($groups as $group) {
foreach ($group->slides as $slide) {
$originalLineCount = count(explode("\n", $slide->text_content ?? ''));
$chunk = array_slice($translatedLines, $offset, $originalLineCount);
$offset += $originalLineCount;
$slide->update([
'text_content_translated' => implode("\n", $chunk),
]);
}
}
$this->markAsTranslated($song);
}
/**
* Song als "hat Übersetzung" markieren.
*/
public function markAsTranslated(Song $song): void
{
$song->update(['has_translation' => true]);
}
/**
* Übersetzung eines Songs komplett entfernen.
*
* Löscht alle text_content_translated Felder und setzt has_translation auf false.
*/
public function removeTranslation(Song $song): void
{
// Alle Slides des Songs über die Gruppen aktualisieren
$slideIds = SongSlide::whereIn(
'song_group_id',
$song->groups()->pluck('id')
)->pluck('id');
SongSlide::whereIn('id', $slideIds)->update([
'text_content_translated' => null,
]);
$song->update(['has_translation' => false]);
}
}