pp-planer/app/Services/ProExportService.php
Thorsten Bus bdbf0c65e3 refactor(php): rename SongGroup references throughout controllers/services/tests
Replace all SongGroup/SongArrangementGroup model usages with Label/SongArrangementLabel
after the schema migration to global labels. Updates 12 app files and 11 test files:

- SongService: createDefaultGroups now finds-or-creates global Labels by name
- ArrangementController: validates label_id (labels are global, no song-ownership)
- ProImportService: imports groups as Labels (firstOrCreate by name); does not
  overwrite existing label colors per spec
- ProExportService/SongPdfController/TranslationService/etc: traverse via
  arrangements -> arrangementLabels -> label -> songSlides chain
- All test factories and assertions adapted to label-based schema
2026-05-03 22:55:02 +02:00

142 lines
3.9 KiB
PHP

<?php
namespace App\Services;
use App\Models\Setting;
use App\Models\Song;
use ProPresenter\Parser\ProFileGenerator;
class ProExportService
{
public function generateProFile(Song $song): string
{
$tempPath = sys_get_temp_dir().'/'.uniqid('pro-export-').'.pro';
ProFileGenerator::generateAndWrite(
$tempPath,
$song->title,
$this->buildGroups($song),
$this->buildArrangements($song),
$this->buildCcliMetadata($song),
);
return $tempPath;
}
public function generateParserSong(Song $song): \ProPresenter\Parser\Song
{
$song->loadMissing(['arrangements.arrangementLabels.label.songSlides']);
return ProFileGenerator::generate(
$song->title,
$this->buildGroups($song),
$this->buildArrangements($song),
$this->buildCcliMetadata($song),
);
}
private function buildGroups(Song $song): array
{
$defaultArr = $song->arrangements->firstWhere('is_default', true) ?? $song->arrangements->first();
if ($defaultArr === null) {
return [];
}
$defaultArr->loadMissing('arrangementLabels.label.songSlides');
$macroData = $this->buildMacroData();
$groups = [];
$seenLabelIds = [];
foreach ($defaultArr->arrangementLabels->sortBy('order') as $arrangementLabel) {
$label = $arrangementLabel->label;
if ($label === null) {
continue;
}
if (in_array($label->id, $seenLabelIds, true)) {
continue;
}
$seenLabelIds[] = $label->id;
$isCopyrightGroup = strcasecmp($label->name, 'COPYRIGHT') === 0;
$slides = [];
foreach ($label->songSlides->sortBy('order') as $slide) {
$slideData = ['text' => $slide->text_content ?? ''];
if ($slide->text_content_translated) {
$slideData['translation'] = $slide->text_content_translated;
}
if ($isCopyrightGroup && $macroData) {
$slideData['macro'] = $macroData;
}
$slides[] = $slideData;
}
$groups[] = [
'name' => $label->name,
'color' => ProImportService::hexToRgba($label->color ?? '#808080'),
'slides' => $slides,
];
}
return $groups;
}
private function buildMacroData(): ?array
{
$name = Setting::get('macro_name');
$uuid = Setting::get('macro_uuid');
if (! $name || ! $uuid) {
return null;
}
return [
'name' => $name,
'uuid' => $uuid,
'collectionName' => Setting::get('macro_collection_name', '--MAIN--'),
'collectionUuid' => Setting::get('macro_collection_uuid', '8D02FC57-83F8-4042-9B90-81C229728426'),
];
}
private function buildArrangements(Song $song): array
{
$arrangements = [];
foreach ($song->arrangements as $arrangement) {
$arrangement->loadMissing('arrangementLabels.label');
$groupNames = $arrangement->arrangementLabels
->sortBy('order')
->map(fn ($al) => $al->label?->name)
->filter()
->values()
->toArray();
$arrangements[] = [
'name' => $arrangement->name,
'groupNames' => $groupNames,
];
}
return $arrangements;
}
private function buildCcliMetadata(Song $song): array
{
return array_filter([
'author' => $song->author,
'song_title' => $song->title,
'copyright_year' => $song->copyright_year,
'publisher' => $song->publisher,
'song_number' => $song->ccli_id ? (int) $song->ccli_id : null,
]);
}
}