pp-planer/app/Services/SongMatchingService.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

118 lines
3.4 KiB
PHP

<?php
namespace App\Services;
use App\Mail\MissingSongRequest;
use App\Models\ServiceSong;
use App\Models\Song;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;
class SongMatchingService
{
public function __construct(
private readonly SongPrefixPostfixService $songPrefixPostfixService,
) {}
public function autoMatch(ServiceSong $serviceSong): bool
{
if ($serviceSong->song_id !== null) {
return false;
}
$song = null;
if ($serviceSong->cts_ccli_id !== null && $serviceSong->cts_ccli_id !== '') {
$song = Song::where('ccli_id', $serviceSong->cts_ccli_id)->first();
}
if ($song === null && $serviceSong->cts_song_id) {
$song = Song::where('cts_song_id', $serviceSong->cts_song_id)->first();
}
if ($song === null) {
return false;
}
// Find default arrangement: is_default → name='normal' → first
$defaultArrangement = $song->arrangements()
->where('is_default', true)
->first() ?? $song->arrangements()
->where('name', 'normal')
->first() ?? $song->arrangements()
->first();
$serviceSong->update([
'song_id' => $song->id,
'song_arrangement_id' => $defaultArrangement?->id,
'matched_at' => Carbon::now(),
'use_translation' => $song->has_translation,
]);
return true;
}
/**
* Manually assign a song to a service song.
* Overwrites any existing assignment.
* Only sets arrangement if currently null (preserves existing selection).
*/
public function manualAssign(ServiceSong $serviceSong, Song $song): void
{
$updateData = [
'song_id' => $song->id,
'matched_at' => Carbon::now(),
'use_translation' => $song->has_translation,
];
// Only set arrangement if currently null
if ($serviceSong->song_arrangement_id === null) {
// Find default arrangement: is_default → name='normal' → first
$defaultArrangement = $song->arrangements()
->where('is_default', true)
->first() ?? $song->arrangements()
->where('name', 'normal')
->first() ?? $song->arrangements()
->first();
$updateData['song_arrangement_id'] = $defaultArrangement?->id;
}
$serviceSong->update($updateData);
$this->songPrefixPostfixService->ensure($song);
}
/**
* Send a missing song request email and record the timestamp.
*/
public function requestCreation(ServiceSong $serviceSong): void
{
$service = $serviceSong->service;
$recipientEmail = (string) Config::get('services.song_request.email', 'songs@example.com');
Mail::to($recipientEmail)->send(new MissingSongRequest(
songName: $serviceSong->cts_song_name,
ccliId: $serviceSong->cts_ccli_id,
service: $service,
));
$serviceSong->update([
'request_sent_at' => Carbon::now(),
]);
}
/**
* Remove song assignment from a service song.
*/
public function unassign(ServiceSong $serviceSong): void
{
$serviceSong->update([
'song_id' => null,
'matched_at' => null,
]);
}
}