pp-planer/app/Models/Service.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

195 lines
6.1 KiB
PHP

<?php
namespace App\Models;
use App\Services\AgendaMatcherService;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Service extends Model
{
use HasFactory;
protected $fillable = [
'cts_event_id',
'title',
'date',
'preacher_name',
'preacher_name_override',
'beamer_tech_name',
'key_visual_filename',
'background_filename',
'moderator_name',
'finalized_at',
'last_synced_at',
'cts_data',
'has_agenda',
];
protected function casts(): array
{
return [
'date' => 'date',
'finalized_at' => 'datetime',
'last_synced_at' => 'datetime',
'cts_data' => 'array',
'has_agenda' => 'boolean',
];
}
public function serviceSongs(): HasMany
{
return $this->hasMany(ServiceSong::class);
}
public function slides(): HasMany
{
return $this->hasMany(Slide::class);
}
protected function keyVisualUrl(): Attribute
{
return Attribute::get(fn () => $this->key_visual_filename ? '/storage/'.$this->key_visual_filename : null);
}
protected function backgroundUrl(): Attribute
{
return Attribute::get(fn () => $this->background_filename ? '/storage/'.$this->background_filename : null);
}
public function agendaItems(): HasMany
{
return $this->hasMany(ServiceAgendaItem::class)->orderBy('sort_order');
}
/**
* Scope: nächster bevorstehender Gottesdienst (heute oder später), aufsteigend sortiert.
*/
public function scopeNextUpcoming(Builder $query): Builder
{
return $query->whereDate('date', '>=', Carbon::today())->orderBy('date');
}
/**
* Check finalization prerequisites and return warnings.
*
* @return array{ready: bool, warnings: string[]}
*/
public function finalizationStatus(): array
{
$warnings = [];
// Backward compatibility: if no agenda items exist, use old checks
if ($this->agendaItems()->count() === 0) {
return $this->finalizationStatusLegacy();
}
// New agenda-based checks
$agendaItems = $this->agendaItems()->with('serviceSong.song')->get();
// Check 1: Unmatched songs (song-type agenda items where song_id IS NULL)
$unmatchedSongs = $agendaItems->filter(function (ServiceAgendaItem $item) {
if ($item->service_song_id === null) {
return false;
}
$serviceSong = $item->serviceSong;
return $serviceSong && $serviceSong->song_id === null;
});
foreach ($unmatchedSongs as $item) {
$songName = $item->serviceSong?->cts_song_name ?? 'Unbekannter Song';
$warnings[] = "{$songName} wurde noch nicht zugeordnet";
}
// Check 2: Matched songs without arrangement
$matchedSongsWithoutArrangement = $agendaItems->filter(function (ServiceAgendaItem $item) {
if ($item->service_song_id === null) {
return false;
}
$serviceSong = $item->serviceSong;
return $serviceSong && $serviceSong->song_id !== null && $serviceSong->song_arrangement_id === null;
});
foreach ($matchedSongsWithoutArrangement as $item) {
$songName = $item->serviceSong?->cts_song_name ?? 'Unbekannter Song';
$warnings[] = "{$songName} hat kein Arrangement ausgewählt";
}
// Check 3: Sermon slides
$sermonPatterns = Setting::get('agenda_sermon_matching');
if ($sermonPatterns) {
// Settings configured: check if any agenda item flagged as sermon has slides
$agendaMatcher = app(AgendaMatcherService::class);
$sermonItems = $agendaItems->filter(function (ServiceAgendaItem $item) use ($agendaMatcher, $sermonPatterns) {
return $agendaMatcher->matches($item->title, $sermonPatterns);
});
$sermonHasSlides = false;
foreach ($sermonItems as $item) {
if ($item->slides()->whereNull('deleted_at')->exists()) {
$sermonHasSlides = true;
break;
}
}
if (! $sermonHasSlides) {
$warnings[] = 'Keine Predigt-Folien hochgeladen';
}
} else {
// No settings configured: fall back to checking slides table
if (! $this->slides()->where('type', 'sermon')->exists()) {
$warnings[] = 'Keine Predigt-Folien hochgeladen';
}
}
return [
'ready' => empty($warnings),
'warnings' => $warnings,
];
}
/**
* Legacy finalization status for services without agenda items.
*
* @return array{ready: bool, warnings: string[]}
*/
private function finalizationStatusLegacy(): array
{
$warnings = [];
$totalSongs = $this->serviceSongs()->count();
$mappedSongs = $this->serviceSongs()->whereNotNull('song_id')->count();
$arrangedSongs = $this->serviceSongs()->whereNotNull('song_arrangement_id')->count();
$sermonSlides = $this->slides()->where('type', 'sermon')->count();
if ($totalSongs > 0 && $mappedSongs < $totalSongs) {
$warnings[] = "Nur {$mappedSongs} von {$totalSongs} Songs sind zugeordnet.";
}
if ($totalSongs > 0 && $arrangedSongs < $totalSongs) {
$warnings[] = "Nur {$arrangedSongs} von {$totalSongs} Songs haben ein Arrangement.";
}
if ($sermonSlides === 0) {
$warnings[] = 'Es wurden keine Predigtfolien hochgeladen.';
}
return [
'ready' => empty($warnings),
'warnings' => $warnings,
];
}
protected function isReadyToFinalize(): Attribute
{
return Attribute::get(fn () => $this->finalizationStatus()['ready']);
}
}