Files
laraundfelix/app/Domain/Catalog/PresentationMedia.php
T
LarsandCursor f29ea58fd5
CI Feature / test (push) Successful in 54s
Deploy Test / test (push) Successful in 47s
Deploy Test / deploy (push) Successful in 24s
Ergänze die gemeinsame redaktionelle Basis an den Fachobjekten.
Texte, Materialart und Präsentationsmedien bleiben am Medium, Modul, Programm und Lizenzartikel und werden von Verwaltung und Karten aus derselben Quelle gelesen, ohne Fassung oder Lizenz zu verändern.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-19 07:54:36 +01:00

270 lines
10 KiB
PHP

<?php
namespace App\Domain\Catalog;
use App\Domain\Media\WebsiteMedia;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
/**
* 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',
'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;
}
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;
}
$version = $assignment->sourceVersion;
$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(),
]);
}
}