pp-planer/app/Services/SongMatchingService.php
Thorsten Bus a36841f920 feat(songs): add CTS song ID matching, info slide date filter, arrangement ordering, translation defaults
- Add cts_song_id column to songs and service_songs for CCLI-free matching fallback
- Filter information slides by uploaded_at <= service date (not shown before upload)
- New arrangements use song's default group ordering instead of cloning
- Auto-set use_translation=true when matched song has translation
- Update syncSongs/syncServiceAgendaSongs to store and use cts_song_id
- Add tests for CTS song ID fallback, upload date filtering, and translation defaults
2026-03-02 14:10:40 +01:00

87 lines
2.2 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 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;
}
$serviceSong->update([
'song_id' => $song->id,
'matched_at' => Carbon::now(),
'use_translation' => $song->has_translation,
]);
return true;
}
/**
* Manually assign a song to a service song.
* Overwrites any existing assignment.
*/
public function manualAssign(ServiceSong $serviceSong, Song $song): void
{
$serviceSong->update([
'song_id' => $song->id,
'matched_at' => Carbon::now(),
'use_translation' => $song->has_translation,
]);
}
/**
* 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,
]);
}
}