> */ 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 */ 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 $staged * @return list>}> */ 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. * * @param list $staged */ public function commit(Model $subject, array $staged, ?User $actor = 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): 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)); foreach ($existing as $id => $assignment) { if (! in_array((int) $id, $kept, true)) { $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']); 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(), ]); } }