'Öffentliche Kachel', 'detail' => 'Öffentliche Detailvorstellung', 'bibliothek' => 'Mitgliederbibliothek', 'trainer' => 'Fachliche Traineransicht', 'artikel' => 'Lizenzartikelpräsentation', 'cms' => 'CMS-Teaser', 'suche' => 'Suchergebnis', ]; public const TEXT_FIELDS = ['display_title', 'teaser', 'summary', 'description']; public function read(Model $subject, ?string $audience = null): EditorialProfile { if ($audience !== null && ! array_key_exists($audience, self::AUDIENCES)) { throw new DomainRuleException('Diese Zielgruppe ist nicht vorgesehen.'); } $source = $subject instanceof LicenseArticle ? $this->sourceOf($subject) : null; $texts = []; foreach (self::TEXT_FIELDS as $field) { $texts[$field] = $this->resolve($subject, $field, $audience, $source); } $search = $this->own($subject, 'search_terms'); $searchOrigin = 'eigen'; if ($search === null && $source instanceof Model) { $search = $this->own($source, 'search_terms'); $searchOrigin = $search === null ? 'leer' : 'quelle'; } if ($search === null) { $searchOrigin = 'leer'; } return new EditorialProfile( workingTitle: $this->workingTitle($subject), texts: $texts, searchTerms: $search, searchOrigin: $searchOrigin, kindLabel: $subject->editorialContentKind?->label ?? ($source?->editorialContentKind?->label), source: $source, ); } public function sourceOf(LicenseArticle $article): ?Model { $release = $article->relationLoaded('currentRelease') ? $article->currentRelease : $article->currentRelease()->first(); if (! $release instanceof ArticleRelease) { return null; } $elementId = $release->root_element_version_id; $compositionId = $release->root_composition_version_id; if (($elementId === null) === ($compositionId === null)) { return null; } if ($elementId !== null) { return ContentElementVersion::query()->with('element.editorialContentKind')->find($elementId)?->element; } return CompositionVersion::query()->with('composition.editorialContentKind')->find($compositionId)?->composition; } public function putVariant(Model $subject, string $field, string $audience, ?string $body, ?User $actor = null): string { if (! in_array($field, self::TEXT_FIELDS, true) || ! array_key_exists($audience, self::AUDIENCES)) { throw new DomainRuleException('Diese Abweichung ist nicht vorgesehen.'); } $body = is_string($body) ? trim($body) : ''; $existing = $this->variant($subject, $field, $audience); if ($body === '') { if ($existing instanceof EditorialTextVariant) { $this->record($subject, $audience, $field, $existing->body, null, $actor); $existing->delete(); } return 'Die abweichende Formulierung ist entfernt. Es gilt wieder die zentrale Angabe.'; } if ($existing instanceof EditorialTextVariant && $existing->body === $body) { return 'Die abweichende Formulierung ist unverändert.'; } $this->record($subject, $audience, $field, $existing?->body, $body, $actor); EditorialTextVariant::query()->updateOrCreate( [ 'subject_type' => $subject->getMorphClass(), 'subject_id' => $subject->getKey(), 'field' => $field, 'audience' => $audience, ], ['body' => $body], ); return 'Die abweichende Formulierung gilt nur für '.self::AUDIENCES[$audience].'. Die zentrale Angabe bleibt.'; } public function forgetVariant(Model $subject, EditorialTextVariant $variant, ?User $actor = null): void { if ($variant->subject_type !== $subject->getMorphClass() || (int) $variant->subject_id !== (int) $subject->getKey()) { throw new DomainRuleException('Diese Abweichung gehört zu einem anderen Objekt.'); } $this->record($subject, $variant->audience, $variant->field, $variant->body, null, $actor); $variant->delete(); } private function resolve(Model $subject, string $field, ?string $audience, ?Model $source): ResolvedText { if ($audience !== null) { $variant = $this->variant($subject, $field, $audience); if ($variant instanceof EditorialTextVariant && trim($variant->body) !== '') { return new ResolvedText(trim($variant->body), 'variante', self::AUDIENCES[$audience]); } } $own = $this->own($subject, $field); if ($own !== null) { return new ResolvedText($own, 'eigen', null); } if ($source instanceof Model) { $fromSource = $this->resolve($source, $field, $audience, null); if ($fromSource->value !== null) { return new ResolvedText($fromSource->value, 'quelle', $this->sourceLabel($source)); } } return new ResolvedText(null, 'leer', null); } private function variant(Model $subject, string $field, string $audience): ?EditorialTextVariant { return EditorialTextVariant::query() ->where('subject_type', $subject->getMorphClass()) ->where('subject_id', $subject->getKey()) ->where('field', $field) ->where('audience', $audience) ->first(); } private function own(Model $subject, string $field): ?string { $value = $subject->getAttribute($field); if (! is_string($value)) { return null; } $value = trim($value); return $value === '' ? null : $value; } private function workingTitle(Model $subject): ?string { if ($subject instanceof Composition) { return $this->own($subject, 'designation'); } if ($subject instanceof LicenseArticle) { return $this->own($subject, 'working_title') ?? $this->own($subject, 'code'); } return $this->own($subject, 'working_title'); } private function sourceLabel(Model $source): string { if ($source instanceof Composition) { return (string) ($source->display_title ?: $source->designation); } if ($source instanceof ContentElement) { return (string) ($source->display_title ?: $source->working_title); } return 'der fachlichen Quelle'; } private function record(Model $subject, string $audience, string $field, ?string $before, ?string $after, ?User $actor): void { IdentityChange::query()->create([ 'subject_type' => $subject->getMorphClass(), 'subject_id' => $subject->getKey(), 'field' => 'variante:'.$audience.':'.$field, 'before' => $before, 'after' => $after, 'actor_id' => $actor?->id, 'occurred_at' => now(), ]); } }