|null Element-ID => erstes öffentliches Programm */ private ?array $publicIndex = null; public function __construct( private readonly PublicCatalogReader $catalog, private readonly CompositionStructureReader $structure, ) {} public function isSelectable(ContentElement $element): bool { return $element->archived_at === null && $element->versions()->where('status', '!=', ContentVersionStatus::Gesperrt->value)->exists(); } public function isReady(ContentElement $element): bool { return $element->versions()->where('status', ContentVersionStatus::Bereit->value)->exists(); } /** * Öffentliches Programm, in dessen gezeigter Fassung das Element vorkommt, sonst null. * * @return array{id: int, title: string}|null */ public function publicProgramFor(ContentElement $element): ?array { return $this->publicIndex()[$element->id] ?? null; } public function isPublic(ContentElement $element): bool { return $this->isSelectable($element) && $this->isReady($element) && ! in_array($element->editorialContentKind?->slug, self::CONFIDENTIAL_KINDS, true) && $this->publicProgramFor($element) !== null; } /** * @return array */ public function search(?string $term, int $limit = 25): array { $term = trim((string) $term); $candidates = ContentElement::query() ->with('editorialContentKind') ->whereNull('archived_at') ->whereHas('versions', fn ($query) => $query->where('status', '!=', ContentVersionStatus::Gesperrt->value)) ->when($term !== '', function ($query) use ($term): void { $like = '%'.mb_strtolower($term).'%'; $query->where(fn ($inner) => $inner ->whereRaw('LOWER(working_title) LIKE ?', [$like]) ->orWhereRaw('LOWER(COALESCE(display_title, \'\')) LIKE ?', [$like])); }) ->orderBy('working_title') ->limit($limit) ->get(); $options = []; foreach ($candidates as $element) { $options[$element->id] = $this->optionLabel($element); } return $options; } public function labelFor(mixed $id): ?string { $element = is_numeric($id) ? ContentElement::query()->with('editorialContentKind')->find((int) $id) : null; return $element === null ? null : $this->optionLabel($element); } /** * Anzeigedaten für eine Karte. Öffentlich null, wenn das Element nicht öffentlich genannt wird. * * @param array $draft ungespeicherte Anzeigedaten, nur für die Redaktionsvorschau */ public function subject(mixed $id, bool $includeNonPublic, array $draft = []): ?CardSubject { $element = is_numeric($id) ? ContentElement::query()->with('editorialContentKind')->find((int) $id) : null; if ($element === null || ! $this->isSelectable($element)) { return null; } $public = $this->isPublic($element); if (! $public && ! $includeNonPublic) { return null; } $shown = clone $element; foreach (['working_title', 'display_title', 'teaser', 'summary', 'description', 'thumbnail_path', 'thumbnail_alt'] as $field) { if (! array_key_exists($field, $draft)) { continue; } $value = $draft[$field]; $shown->setAttribute($field, is_string($value) && trim($value) === '' ? null : $value); } $program = $this->publicProgramFor($element); $output = self::outputLabel((string) $shown->output_class); $film = $shown->output_class === 'film'; $editorialKind = $element->editorialContentKind?->label; $summary = filled($shown->summary) ? trim((string) $shown->summary) : null; $profile = app(EditorialProfileReader::class)->read($shown, 'bibliothek'); if (! array_key_exists('summary', $draft) && $profile->origin('summary') === 'variante') { $summary = $profile->value('summary'); } elseif ($summary === null && filled($shown->teaser)) { $summary = trim((string) $shown->teaser); } $texts = []; foreach (['teaser', 'summary', 'description'] as $field) { $own = filled($shown->getAttribute($field)) ? trim((string) $shown->getAttribute($field)) : null; if (! array_key_exists($field, $draft) && $profile->origin($field) === 'variante') { $own = $profile->value($field); } $texts[$field] = $own; } $title = (string) ($shown->display_title ?: $shown->working_title); if (! array_key_exists('display_title', $draft) && $profile->origin('display_title') === 'variante') { $title = (string) $profile->value('display_title'); } $meta = [$output]; if ($program !== null) { $meta[] = 'Im Programm „'.$program['title'].'“'; } if ($film) { $meta[] = 'Vollständiger Film nur mit Lizenz im Mitgliederbereich'; } $missing = []; if ($summary === null) { $missing[] = 'Keine Kurzbeschreibung gepflegt (Medium bearbeiten → Kurzbeschreibung).'; } if (! filled($shown->display_title)) { $missing[] = 'Kein Anzeigetitel gepflegt; die Karte zeigt den Arbeitstitel.'; } $previewImage = $includeNonPublic ? ThumbnailRef::url('editorial.card', 'element', (int) $element->id, $shown->thumbnail_path) : null; return new CardSubject( title: $title, summary: $summary, kind: filled($editorialKind) ? (string) $editorialKind : $output, badge: $output, meta: $meta, url: $public && $program !== null ? route('public.program', $program['id']) : null, public: $public, notice: $public ? null : $this->notice($element, $program), visual: $film ? 'film' : 'material', visualLabel: $output, previewImage: $previewImage, missing: $missing, style: $film ? 'media' : 'material', texts: $texts, ); } /** Bezeichnung der Ausgabeart, wie in der Redaktionsstruktur (CompositionRelease::outputLabel). */ public static function outputLabel(string $outputClass): string { return match ($outputClass) { 'film' => 'Film', 'audio' => 'Hörstück', 'bild' => 'Bild', 'document', 'pdf' => 'Dokument', default => 'Text', }; } public function visibilityNote(ContentElement $element): string { if ($this->isPublic($element)) { $program = $this->publicProgramFor($element); return 'Öffentlich nur als Teil der Programmseite „'.($program['title'] ?? '').'“. Die Mediendatei wird dabei nicht ausgeliefert.'; } return $this->notice($element, $this->publicProgramFor($element)); } /** * @param array{id: int, title: string}|null $program */ private function notice(ContentElement $element, ?array $program): string { $reason = match (true) { in_array($element->editorialContentKind?->slug, self::CONFIDENTIAL_KINDS, true) => 'Traineranleitungen erscheinen nie öffentlich.', ! $this->isReady($element) => 'Das Medium hat noch keine bereitgestellte Fassung.', $program === null => 'Das Medium gehört zu keinem öffentlich gezeigten Programm. Medien haben noch keine eigene öffentliche Freigabe (öffentliches Profil, ADR-29).', default => 'Das Medium ist nicht öffentlich.', }; return 'Nur in der Redaktionsvorschau sichtbar: '.$reason; } private function optionLabel(ContentElement $element): string { $visibility = $this->isPublic($element) ? 'öffentlich über Programmseite' : ($this->isReady($element) ? 'nur Redaktionsvorschau' : 'Entwurf, nur Redaktionsvorschau'); $kind = $element->editorialContentKind?->label ?: self::outputLabel((string) $element->output_class); return (string) ($element->display_title ?: $element->working_title).' · '.$kind.' · '.$visibility; } /** * Index aller Elemente, die in der gezeigten Fassung eines öffentlichen Programms vorkommen (auch verschachtelt). * * @return array */ private function publicIndex(): array { if ($this->publicIndex !== null) { return $this->publicIndex; } $index = []; foreach ($this->catalog->publicPrograms() as $program) { $version = $this->catalog->shownVersion($program); if ($version === null) { continue; } $entry = ['id' => (int) $program->id, 'title' => (string) ($program->display_title ?: $program->designation)]; foreach ($this->elementIds($this->structure->tree($version)) as $elementId) { $index[$elementId] ??= $entry; } } return $this->publicIndex = $index; } /** * @param list $nodes * @return list */ private function elementIds(array $nodes): array { $ids = []; foreach ($nodes as $node) { if ($node->contentElementId !== null) { $ids[] = (int) $node->contentElementId; } array_push($ids, ...$this->elementIds($node->children)); } return $ids; } }