- Speichern löst nur Zuordnungen, die beim Laden da waren; zwischenzeitlich
anderswo ergänzte bleiben erhalten.
- Eine belegte Einzelrolle (Titelbild, Hero, Hover) wird im Picker
ausdrücklich ausgetauscht, mit Banner und benanntem Ersatz.
- Gemeinsamer Bestätigungsdialog statt Browser-confirm(); Warnung beim
Verlassen der Seite mit ungespeicherten Änderungen.
- „Ausgabe prüfen“ am Medium benennt, welche Angaben aktuell und welche
gespeichert sind.
- Das frühere Gesamtformular für Medien leitet in die Redaktion; „Löschen“
steht unter „Weitere Aktionen“, die Medienliste öffnet die Redaktion.
- Vorschaubild aus vorhandenem Bild wieder über den gemeinsamen Picker
wählbar (Oberfläche war seit 72130d6 entfallen).
- Vorgemerkte Upload-Dateien bleiben sichtbar, Brotkrumen führen zur Liste,
die Redaktion lässt sich über die Adresse nicht ohne Recht öffnen.
- Agentenregeln und CLAUDE.md verweisen auf die gemeinsamen Bausteine.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
516 lines
21 KiB
PHP
516 lines
21 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
use App\Domain\Media\ThumbnailRef;
|
|
use App\Domain\Media\WebsiteMedia;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Präsentationsmedien sind Referenzen auf Inhaltselemente.
|
|
* Die Rolle gehört zur Zuordnung. Eine Zuordnung gibt nichts frei und ändert keine Fassung.
|
|
*/
|
|
class PresentationMedia
|
|
{
|
|
public function __construct(private readonly WebsiteMedia $website) {}
|
|
|
|
/**
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
public function rows(Model $subject): array
|
|
{
|
|
$assignments = PresentationAssignment::query()
|
|
->with(['role', 'element.editorialContentKind', 'sourceVersion'])
|
|
->where('subject_type', $subject->getMorphClass())
|
|
->where('subject_id', $subject->getKey())
|
|
->orderBy('position')
|
|
->orderBy('id')
|
|
->get();
|
|
|
|
$rows = [];
|
|
|
|
foreach ($assignments as $assignment) {
|
|
$element = $assignment->element;
|
|
$released = $element instanceof ContentElement && $this->website->present($element) !== null;
|
|
$binding = $this->binding($assignment);
|
|
|
|
$rows[] = [
|
|
'id' => $assignment->id,
|
|
'role' => (string) $assignment->role?->slug,
|
|
'role_label' => (string) ($assignment->role?->label ?? ''),
|
|
'ordered' => (bool) $assignment->role?->ordered,
|
|
'position' => (int) $assignment->position,
|
|
'standard' => $assignment->role?->slug === 'titelbild',
|
|
'element_id' => (int) $assignment->content_element_id,
|
|
'title' => $element instanceof ContentElement
|
|
? (string) ($element->display_title ?: $element->working_title)
|
|
: 'Medium fehlt',
|
|
'image' => $element instanceof ContentElement
|
|
? ThumbnailRef::url('editorial.card', 'element', $element->id, $element->thumbnail_path)
|
|
: null,
|
|
'kind' => $element?->editorialContentKind?->label,
|
|
'output' => $element instanceof ContentElement ? (string) $element->output_class : '',
|
|
'released' => $released,
|
|
'release_label' => $released ? 'Für die Website freigegeben' : 'Nicht für die Website freigegeben',
|
|
'binding' => $binding,
|
|
];
|
|
}
|
|
|
|
return $rows;
|
|
}
|
|
|
|
/**
|
|
* Gespeicherte Zuordnungen als bearbeitbarer Arbeitsstand, nach Rolle und Position geordnet.
|
|
*
|
|
* @return list<array{key: string, id: int|null, role: string, element_id: int, source: int|null}>
|
|
*/
|
|
public function staged(Model $subject): array
|
|
{
|
|
$assignments = PresentationAssignment::query()
|
|
->with('role')
|
|
->where('subject_type', $subject->getMorphClass())
|
|
->where('subject_id', $subject->getKey())
|
|
->get()
|
|
->sortBy(fn (PresentationAssignment $row): string => sprintf('%05d-%05d-%08d', (int) $row->role?->sort_order, (int) $row->position, (int) $row->id))
|
|
->values();
|
|
|
|
$rows = [];
|
|
|
|
foreach ($assignments as $assignment) {
|
|
$rows[] = [
|
|
'key' => 'a'.$assignment->id,
|
|
'id' => (int) $assignment->id,
|
|
'role' => (string) $assignment->role?->slug,
|
|
'element_id' => (int) $assignment->content_element_id,
|
|
'source' => $assignment->source_version_id !== null ? (int) $assignment->source_version_id : null,
|
|
];
|
|
}
|
|
|
|
return $rows;
|
|
}
|
|
|
|
/**
|
|
* Anzeigezeilen für einen Arbeitsstand, gruppiert nach Rolle. Liest nur, schreibt nichts.
|
|
*
|
|
* @param list<array{key: string, id: int|null, role: string, element_id: int, source: int|null}> $staged
|
|
* @return list<array{role: string, label: string, multiple: bool, ordered: bool, rows: list<array<string, mixed>>}>
|
|
*/
|
|
public function describe(array $staged): array
|
|
{
|
|
$roles = PresentationRole::query()->orderBy('sort_order')->get()->keyBy('slug');
|
|
$elements = ContentElement::query()
|
|
->with('editorialContentKind')
|
|
->whereIn('id', array_values(array_unique(array_map(fn (array $row): int => (int) $row['element_id'], $staged))))
|
|
->get()
|
|
->keyBy('id');
|
|
$sources = ContentElementVersion::query()
|
|
->whereIn('id', array_values(array_filter(array_map(fn (array $row): ?int => $row['source'] ?? null, $staged))))
|
|
->get()
|
|
->keyBy('id');
|
|
$groups = [];
|
|
|
|
foreach ($roles as $slug => $role) {
|
|
$rows = array_values(array_filter($staged, fn (array $row): bool => $row['role'] === $slug));
|
|
|
|
if ($rows === []) {
|
|
continue;
|
|
}
|
|
|
|
$described = [];
|
|
|
|
foreach ($rows as $row) {
|
|
$element = $elements->get((int) $row['element_id']);
|
|
$released = $element instanceof ContentElement && $this->website->present($element) !== null;
|
|
$source = $row['source'] !== null ? $sources->get((int) $row['source']) : null;
|
|
|
|
$described[] = [
|
|
'key' => $row['key'],
|
|
'id' => $row['id'],
|
|
'element_id' => (int) $row['element_id'],
|
|
'title' => $element instanceof ContentElement
|
|
? (string) ($element->display_title ?: $element->working_title)
|
|
: 'Medium fehlt',
|
|
'image' => $element instanceof ContentElement
|
|
? ThumbnailRef::url('editorial.card', 'element', $element->id, $element->thumbnail_path)
|
|
: null,
|
|
'kind' => $element?->editorialContentKind?->label,
|
|
'output' => $element instanceof ContentElement ? (string) $element->output_class : '',
|
|
'released' => $released,
|
|
'release_label' => $released ? 'Für die Website freigegeben' : 'Nicht für die Website freigegeben',
|
|
'binding' => $source instanceof ContentElementVersion ? $this->bindingFor($source) : null,
|
|
'new' => $row['id'] === null,
|
|
];
|
|
}
|
|
|
|
$groups[] = [
|
|
'role' => (string) $slug,
|
|
'label' => PresentationRole::displayLabel((string) $slug, (string) $role->label),
|
|
'multiple' => (bool) $role->multiple,
|
|
'ordered' => (bool) $role->ordered,
|
|
'rows' => $described,
|
|
];
|
|
}
|
|
|
|
return $groups;
|
|
}
|
|
|
|
/**
|
|
* Prüft, ob ein Medium im Arbeitsstand einer Rolle zugeordnet werden darf. Liefert den Ablehnungsgrund oder null.
|
|
*/
|
|
public function refusal(Model $subject, string $roleSlug, int $elementId): ?string
|
|
{
|
|
$role = PresentationRole::query()->where('slug', $roleSlug)->first();
|
|
$element = ContentElement::query()->find($elementId);
|
|
|
|
if (! $role instanceof PresentationRole || ! $element instanceof ContentElement) {
|
|
return 'Rolle oder Medium fehlt.';
|
|
}
|
|
|
|
if ($element->archived_at !== null) {
|
|
return 'Ein archiviertes Medium wird nicht zugeordnet.';
|
|
}
|
|
|
|
if ($subject instanceof ContentElement && (int) $subject->getKey() === (int) $element->id) {
|
|
return 'Ein Medium kann nicht seine eigene Präsentation sein.';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Schreibt einen Arbeitsstand: entfernte Zuordnungen lösen, neue anlegen, Bindung und Reihenfolge je Rolle setzen.
|
|
* Medien, Fassungen und Website-Freigaben bleiben unberührt.
|
|
*
|
|
* Mit $baseline (dem beim Laden gelesenen Stand) werden nur Zuordnungen gelöst, die dort standen und im
|
|
* Arbeitsstand fehlen. Was inzwischen an anderer Stelle hinzugekommen ist, bleibt erhalten.
|
|
*
|
|
* @param list<array{key: string, id: int|null, role: string, element_id: int, source: int|null}> $staged
|
|
* @param list<array{key: string, id: int|null, role: string, element_id: int, source: int|null}>|null $baseline
|
|
*/
|
|
public function commit(Model $subject, array $staged, ?User $actor = null, ?array $baseline = null): void
|
|
{
|
|
$roles = PresentationRole::query()->get()->keyBy('slug');
|
|
$seen = [];
|
|
|
|
foreach ($staged as $row) {
|
|
$role = $roles->get($row['role']);
|
|
|
|
if (! $role instanceof PresentationRole) {
|
|
throw new DomainRuleException('Diese Rolle gibt es nicht.');
|
|
}
|
|
|
|
$refusal = $this->refusal($subject, $row['role'], (int) $row['element_id']);
|
|
|
|
if ($refusal !== null) {
|
|
throw new DomainRuleException($refusal);
|
|
}
|
|
|
|
$pair = $row['role'].':'.$row['element_id'];
|
|
|
|
if (isset($seen[$pair])) {
|
|
throw new DomainRuleException('Ein Medium steht in derselben Rolle nur einmal.');
|
|
}
|
|
|
|
$seen[$pair] = true;
|
|
$count = count(array_filter($staged, fn (array $other): bool => $other['role'] === $row['role']));
|
|
|
|
if (! $role->multiple && $count > 1) {
|
|
throw new DomainRuleException($role->label.' hat nur eine Zuordnung.');
|
|
}
|
|
|
|
$element = ContentElement::query()->findOrFail((int) $row['element_id']);
|
|
$this->checkedSource($subject, $element, $row['source']);
|
|
}
|
|
|
|
DB::transaction(function () use ($subject, $staged, $roles, $actor, $baseline): void {
|
|
$existing = PresentationAssignment::query()
|
|
->with(['role', 'element'])
|
|
->where('subject_type', $subject->getMorphClass())
|
|
->where('subject_id', $subject->getKey())
|
|
->get()
|
|
->keyBy('id');
|
|
$kept = array_filter(array_map(fn (array $row): ?int => $row['id'], $staged));
|
|
$known = $baseline === null ? null : array_filter(array_map(fn (array $row): ?int => $row['id'], $baseline));
|
|
|
|
foreach ($existing as $id => $assignment) {
|
|
$wasLoaded = $known === null || in_array((int) $id, $known, true);
|
|
$replacedSingle = ! (bool) $assignment->role?->multiple
|
|
&& ! in_array((int) $id, $kept, true)
|
|
&& array_filter($staged, fn (array $row): bool => $row['role'] === $assignment->role?->slug) !== [];
|
|
|
|
if (! in_array((int) $id, $kept, true) && ($wasLoaded || $replacedSingle)) {
|
|
$this->record($subject, ($assignment->role?->label ?? 'Präsentation').': '.($assignment->element?->working_title ?? ''), null, $actor);
|
|
$assignment->delete();
|
|
}
|
|
}
|
|
|
|
$positions = [];
|
|
|
|
foreach ($staged as $row) {
|
|
$role = $roles->get($row['role']);
|
|
$position = $positions[$row['role']] = ($positions[$row['role']] ?? 0) + 1;
|
|
$assignment = $row['id'] !== null ? $existing->get($row['id']) : null;
|
|
|
|
if ($assignment instanceof PresentationAssignment) {
|
|
$changed = (int) $assignment->position !== $position
|
|
|| (int) ($assignment->source_version_id ?? 0) !== (int) ($row['source'] ?? 0);
|
|
$assignment->position = $position;
|
|
$assignment->source_version_id = $row['source'];
|
|
|
|
if ($changed) {
|
|
$assignment->save();
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
$element = ContentElement::query()->findOrFail((int) $row['element_id']);
|
|
$same = PresentationAssignment::query()
|
|
->where('subject_type', $subject->getMorphClass())
|
|
->where('subject_id', $subject->getKey())
|
|
->where('presentation_role_id', $role->id)
|
|
->where('content_element_id', $element->id)
|
|
->first();
|
|
|
|
if ($same instanceof PresentationAssignment) {
|
|
$same->position = $position;
|
|
$same->source_version_id = $row['source'];
|
|
$same->save();
|
|
|
|
continue;
|
|
}
|
|
|
|
PresentationAssignment::query()->create([
|
|
'subject_type' => $subject->getMorphClass(),
|
|
'subject_id' => $subject->getKey(),
|
|
'content_element_id' => $element->id,
|
|
'presentation_role_id' => $role->id,
|
|
'position' => $position,
|
|
'source_version_id' => $row['source'],
|
|
]);
|
|
$this->record($subject, null, $role->label.': '.$element->working_title, $actor);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function assign(Model $subject, string $roleSlug, int $elementId, ?int $sourceVersionId, ?User $actor = null): string
|
|
{
|
|
$role = PresentationRole::query()->where('slug', $roleSlug)->first();
|
|
$element = ContentElement::query()->find($elementId);
|
|
|
|
if (! $role instanceof PresentationRole || ! $element instanceof ContentElement) {
|
|
throw new DomainRuleException('Rolle oder Medium fehlt.');
|
|
}
|
|
|
|
if ($element->archived_at !== null) {
|
|
throw new DomainRuleException('Ein archiviertes Medium wird nicht zugeordnet.');
|
|
}
|
|
|
|
if ($subject instanceof ContentElement && (int) $subject->getKey() === (int) $element->id) {
|
|
throw new DomainRuleException('Ein Medium kann nicht seine eigene Präsentation sein.');
|
|
}
|
|
|
|
$source = $this->checkedSource($subject, $element, $sourceVersionId);
|
|
|
|
$existing = PresentationAssignment::query()
|
|
->where('subject_type', $subject->getMorphClass())
|
|
->where('subject_id', $subject->getKey())
|
|
->where('presentation_role_id', $role->id)
|
|
->where('content_element_id', $element->id)
|
|
->first();
|
|
|
|
if ($existing instanceof PresentationAssignment) {
|
|
if ((int) $existing->source_version_id !== (int) ($source?->id ?? 0) && ($source !== null || $existing->source_version_id !== null)) {
|
|
$existing->source_version_id = $source?->id;
|
|
$existing->save();
|
|
$this->record($subject, null, $role->label.': '.$element->working_title, $actor);
|
|
}
|
|
|
|
return $element->working_title.' ist in dieser Rolle bereits zugeordnet.';
|
|
}
|
|
|
|
if (! $role->multiple) {
|
|
PresentationAssignment::query()
|
|
->where('subject_type', $subject->getMorphClass())
|
|
->where('subject_id', $subject->getKey())
|
|
->where('presentation_role_id', $role->id)
|
|
->delete();
|
|
}
|
|
|
|
$position = (int) PresentationAssignment::query()
|
|
->where('subject_type', $subject->getMorphClass())
|
|
->where('subject_id', $subject->getKey())
|
|
->where('presentation_role_id', $role->id)
|
|
->max('position') + 1;
|
|
|
|
PresentationAssignment::query()->create([
|
|
'subject_type' => $subject->getMorphClass(),
|
|
'subject_id' => $subject->getKey(),
|
|
'content_element_id' => $element->id,
|
|
'presentation_role_id' => $role->id,
|
|
'position' => max(1, $position),
|
|
'source_version_id' => $source?->id,
|
|
]);
|
|
|
|
$this->record($subject, null, $role->label.': '.$element->working_title, $actor);
|
|
|
|
return $element->working_title.' ist als '.$role->label.' zugeordnet. Es entsteht keine Fassung und keine Freigabe.';
|
|
}
|
|
|
|
public function detach(Model $subject, int $assignmentId, ?User $actor = null): string
|
|
{
|
|
$assignment = $this->owned($subject, $assignmentId);
|
|
$label = ($assignment->role?->label ?? 'Präsentation').': '.($assignment->element?->working_title ?? '');
|
|
$assignment->delete();
|
|
$this->record($subject, $label, null, $actor);
|
|
|
|
return 'Die Zuordnung ist entfernt. Das Medium bleibt im Bestand.';
|
|
}
|
|
|
|
public function move(Model $subject, int $assignmentId, int $direction, ?User $actor = null): void
|
|
{
|
|
$assignment = $this->owned($subject, $assignmentId);
|
|
|
|
if (! $assignment->role?->ordered || $direction === 0) {
|
|
return;
|
|
}
|
|
|
|
$neighbors = PresentationAssignment::query()
|
|
->where('subject_type', $subject->getMorphClass())
|
|
->where('subject_id', $subject->getKey())
|
|
->where('presentation_role_id', $assignment->presentation_role_id)
|
|
->orderBy('position')
|
|
->orderBy('id')
|
|
->get()
|
|
->values();
|
|
|
|
$index = $neighbors->search(fn (PresentationAssignment $row): bool => $row->id === $assignment->id);
|
|
|
|
if ($index === false) {
|
|
return;
|
|
}
|
|
|
|
$swap = $index + ($direction < 0 ? -1 : 1);
|
|
$other = $neighbors->get($swap);
|
|
|
|
if (! $other instanceof PresentationAssignment) {
|
|
return;
|
|
}
|
|
|
|
$position = $assignment->position;
|
|
$assignment->position = $other->position === $position ? $position + ($direction < 0 ? -1 : 1) : $other->position;
|
|
$other->position = $position;
|
|
$assignment->save();
|
|
$other->save();
|
|
$this->record($subject, null, 'Reihenfolge '.$assignment->role?->label, $actor);
|
|
}
|
|
|
|
/**
|
|
* Kennung des ersten Titel- oder Galeriebildes. Ob es öffentlich erscheint, entscheidet WebsiteMedia.
|
|
*/
|
|
public function coverElementId(Model $subject): ?int
|
|
{
|
|
$assignments = PresentationAssignment::query()
|
|
->with('role')
|
|
->where('subject_type', $subject->getMorphClass())
|
|
->where('subject_id', $subject->getKey())
|
|
->orderBy('position')
|
|
->get();
|
|
|
|
foreach (PresentationRole::COVER_ROLES as $slug) {
|
|
$match = $assignments->first(fn (PresentationAssignment $row): bool => $row->role?->slug === $slug);
|
|
|
|
if ($match instanceof PresentationAssignment) {
|
|
return (int) $match->content_element_id;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function owned(Model $subject, int $assignmentId): PresentationAssignment
|
|
{
|
|
$assignment = PresentationAssignment::query()
|
|
->with(['role', 'element'])
|
|
->where('subject_type', $subject->getMorphClass())
|
|
->where('subject_id', $subject->getKey())
|
|
->whereKey($assignmentId)
|
|
->first();
|
|
|
|
if (! $assignment instanceof PresentationAssignment) {
|
|
throw new DomainRuleException('Diese Zuordnung gehört zu einem anderen Objekt.');
|
|
}
|
|
|
|
return $assignment;
|
|
}
|
|
|
|
private function checkedSource(Model $subject, ContentElement $element, ?int $sourceVersionId): ?ContentElementVersion
|
|
{
|
|
if ($sourceVersionId === null) {
|
|
return null;
|
|
}
|
|
|
|
$version = ContentElementVersion::query()->find($sourceVersionId);
|
|
|
|
if (! $version instanceof ContentElementVersion) {
|
|
throw new DomainRuleException('Die gebundene Fassung fehlt.');
|
|
}
|
|
|
|
$ownsFilm = $subject instanceof ContentElement && (int) $version->content_element_id === (int) $subject->getKey();
|
|
$ownsClip = (int) $version->content_element_id === (int) $element->id;
|
|
|
|
if (! $ownsFilm && ! $ownsClip) {
|
|
throw new DomainRuleException('Die gebundene Fassung gehört weder zum Fachobjekt noch zum zugeordneten Medium.');
|
|
}
|
|
|
|
return $version;
|
|
}
|
|
|
|
/**
|
|
* @return array{label: string, stale: bool}|null
|
|
*/
|
|
private function binding(PresentationAssignment $assignment): ?array
|
|
{
|
|
if ($assignment->source_version_id === null || ! $assignment->sourceVersion instanceof ContentElementVersion) {
|
|
return null;
|
|
}
|
|
|
|
return $this->bindingFor($assignment->sourceVersion);
|
|
}
|
|
|
|
/**
|
|
* @return array{label: string, stale: bool}
|
|
*/
|
|
private function bindingFor(ContentElementVersion $version): array
|
|
{
|
|
$latestId = ContentElementVersion::query()
|
|
->where('content_element_id', $version->content_element_id)
|
|
->where('status', ContentVersionStatus::Bereit)
|
|
->orderByDesc('number')
|
|
->value('id');
|
|
$stale = $latestId !== null && (int) $latestId !== (int) $version->id;
|
|
|
|
return [
|
|
'label' => $stale
|
|
? 'Gebunden an Fassung '.$version->number.'. Die aktuelle Fassung ist eine andere. Der Clip wurde nicht neu erzeugt.'
|
|
: 'Gebunden an Fassung '.$version->number.'.',
|
|
'stale' => $stale,
|
|
];
|
|
}
|
|
|
|
private function record(Model $subject, ?string $before, ?string $after, ?User $actor): void
|
|
{
|
|
IdentityChange::query()->create([
|
|
'subject_type' => $subject->getMorphClass(),
|
|
'subject_id' => $subject->getKey(),
|
|
'field' => 'praesentation',
|
|
'before' => $before,
|
|
'after' => $after,
|
|
'actor_id' => $actor?->id,
|
|
'occurred_at' => now(),
|
|
]);
|
|
}
|
|
}
|