>> */ private array $trees = []; /** @var array */ private array $texts = []; /** @var array */ private array $elements = []; public function __construct( private readonly CompositionDisplayRelease $display, private readonly CompositionStructureReader $structure, private readonly DecideAccess $access, private readonly EditorialProfileReader $profiles, ) {} /** * Programme, Module und einzelne Medien (Filme und Materialien) der Berechtigungen des Kontos. * * Einzelne Medien erscheinen genau einmal je stabiler Medienidentität (Inhaltselement), gleich in wie vielen Programmen * oder Modulen sie vorkommen. „used_in“ nennt diese Verwendungen. Innerhalb eines Programms bleibt jede Einbindung an * ihrer Stelle erhalten (program()). Gleiche Titel verschiedener Elemente bleiben getrennt. * * @return array{programs: list>, modules: list>, materials: list>} */ public function index(User $user): array { $programs = []; $modules = []; $media = []; foreach ($this->grants($user) as $grant) { $article = $grant->article; $release = $article->currentRelease; $composition = $release?->rootCompositionVersion?->composition; $version = $release?->rootCompositionVersion; if ($composition !== null && $version !== null && $this->display->isReleased($version)) { $shown = $this->display->highestReleasedVersion($composition); $nodes = $shown === null ? [] : $this->tree($user, $shown, false, $composition->id); $picture = app(EditorialCatalog::class)->visiblePicture($composition, $version); $text = $this->text($composition); $card = [ 'type' => $composition->editorialContentKind?->slug === 'modul' ? 'modul' : 'programm', 'composition' => $composition, 'subject' => $composition, 'version' => $version, 'title' => $text['title'], 'summary' => $text['summary'], 'kind' => $composition->editorialContentKind?->label ?? ($composition->kind?->label()), 'coverage' => $this->coverage($nodes), 'module_count' => $this->countCompositions($nodes), 'duration_minutes' => $shown?->duration_minutes, 'image' => $picture['alt'] ?? null, 'image_url' => $picture === null ? null : route('member.thumbnail', [ 'kind' => $picture['kind'], 'subject' => $picture['subject'], ]), 'url' => route('member.program', $composition), ]; if ($card['type'] === 'modul') { $modules[$composition->id] = $card; } else { $programs[$composition->id] = $card; } $this->collect($media, $nodes, [['title' => $text['title'], 'url' => $card['url'], 'id' => $composition->id]], $composition); continue; } $element = $release?->rootElementVersion?->element; $key = $composition !== null ? 'komposition-'.$composition->id : ($element?->id !== null ? 'element-'.$element->id : 'artikel-'.$article->id); $subject = $element ?? $composition ?? $article; $text = $element !== null ? $this->text($element) : null; $imageUrl = $element === null ? null : ThumbnailRef::url('member.thumbnail', 'element', $element->id, $element->thumbnail_path); $media[$key] = ($media[$key] ?? []) + [ 'used_in' => [], 'versions' => [], 'context' => null, ]; $media[$key] = [ ...$media[$key], 'key' => $key, 'type' => $element?->output_class === 'film' ? 'film' : 'material', 'article' => $article, 'subject' => $subject, 'title' => $text['title'] ?? app(CourseOutline::class)->title($article), 'summary' => $text['summary'] ?? $element?->summary ?? $composition?->summary, 'kind' => app(CourseOutline::class)->kindLabel($article) ?? ($element === null ? null : $this->outputLabel($element->output_class)), 'coverage' => 'nutzbar', 'image' => $imageUrl === null ? null : ($element->thumbnail_alt ?: ($text['title'] ?? $element->working_title)), 'image_url' => $imageUrl, 'url' => route('member.article', $article), ]; } foreach ($media as $key => $item) { if (! isset($item['url'])) { $media[$key]['url'] = route('member.content', [ 'composition' => $item['context']['composition']->id, 'version' => $item['context']['version_id'], ]); } } return [ 'programs' => array_values($programs), 'modules' => array_values($modules), 'materials' => array_values($media), ]; } /** * Programm- oder Moduldetail: gesamte Struktur mit allen Einbindungen an ihrer Stelle. * * @return array */ public function program(User $user, Composition $composition): array { $version = $this->display->highestReleasedVersion($composition); abort_if($version === null, 404); $nodes = $this->tree($user, $version, true, $composition->id); $leaves = $this->leaves($nodes); $text = $this->text($composition); return [ 'composition' => $composition, 'picture' => app(EditorialCatalog::class)->visiblePicture($composition, $version), 'title' => $text['title'], 'summary' => $text['summary'], 'description' => $text['description'], 'kind' => $composition->editorialContentKind?->label ?? ($composition->kind?->label()), 'is_module' => $composition->editorialContentKind?->slug === 'modul', 'learning_goal' => $version->learning_goal, 'duration_minutes' => $version->duration_minutes, 'material_note' => $version->material_note, 'coverage' => $this->coverage($nodes), 'module_count' => $this->countCompositions($nodes), 'film_count' => count(array_filter($leaves, fn (array $leaf): bool => $leaf['output_class'] === 'film')), 'material_count' => count(array_filter($leaves, fn (array $leaf): bool => $leaf['element_id'] !== null && $leaf['output_class'] !== 'film')), 'open_count' => count(array_filter($leaves, fn (array $leaf): bool => $leaf['view_url'] !== null)), 'leaf_count' => count($leaves), 'nodes' => $nodes, ]; } /** * Eine Filmstelle oder ein Material im Zusammenhang seines Programms oder Moduls. * Gesucht wird die Einbindung in der angezeigten Fassung; was dort nicht steht, gibt es hier nicht. * * @return array|null */ public function content(User $user, Composition $composition, int $elementVersionId): ?array { $version = $this->display->highestReleasedVersion($composition); if ($version === null) { return null; } $nodes = $this->tree($user, $version, true, $composition->id); $found = $this->locate($nodes, $elementVersionId, []); if ($found === null) { return null; } $node = $found['node']; $element = $node['element_id'] === null ? null : $this->element($node['element_id']); $text = $element === null ? ['title' => $node['title'], 'summary' => null, 'description' => null] : $this->text($element); $rootText = $this->text($composition); $trail = [['label' => $rootText['title'], 'url' => route('member.program', $composition)]]; foreach ($found['parents'] as $parent) { if ($parent['program_url'] !== null && $parent['composition_id'] !== $composition->id) { $trail[] = ['label' => $parent['title'], 'url' => $parent['program_url']]; } } $companions = array_values(array_filter( $found['siblings'], fn (array $sibling): bool => $sibling['children'] === [] && $sibling['element_id'] !== null && $sibling['version_id'] !== $elementVersionId, )); return [ 'composition' => $composition, 'program_title' => $rootText['title'], 'node' => $node, 'element' => $element, 'title' => $text['title'], 'summary' => $text['summary'], 'description' => $text['description'], 'trail' => $trail, 'back' => $trail[array_key_last($trail)], 'companions' => $companions, 'allowed' => $node['view_url'] !== null, ]; } /** * Anzeigetitel und -texte für die Mitgliederbibliothek: Abweichung für „Mitgliederbibliothek“, sonst die zentrale Angabe, * zuletzt die interne Bezeichnung. Nichts wird kopiert oder ergänzt. * * @return array{title: string, summary: ?string, description: ?string} */ public function text(Model $subject): array { $key = $subject->getMorphClass().':'.$subject->getKey(); if (isset($this->texts[$key])) { return $this->texts[$key]; } $profile = $this->profiles->read($subject, 'bibliothek'); return $this->texts[$key] = [ 'title' => $profile->value('display_title') ?? $profile->workingTitle ?? '', 'summary' => $profile->value('summary') ?? $profile->value('teaser'), 'description' => $profile->value('description'), ]; } public function outputLabel(?string $outputClass): ?string { return match ($outputClass) { 'film' => 'Film', 'audio' => 'Hörstück', 'document', 'pdf' => 'Dokument', 'text' => 'Text', 'bild' => 'Bild', null => null, default => $outputClass, }; } /** * @param list> $nodes * @param list $contexts * @param array> $media */ private function collect(array &$media, array $nodes, array $contexts, Composition $root): void { foreach ($nodes as $node) { if ($node['children'] !== []) { $nested = $contexts; if ($node['composition_id'] !== null && $node['program_url'] !== null && $node['composition_id'] !== $root->id) { $nested[] = ['title' => $node['title'], 'url' => $node['program_url'], 'id' => $node['composition_id']]; } $this->collect($media, $node['children'], $nested, $root); continue; } if ($node['element_id'] === null || $node['view_url'] === null) { continue; } $key = 'element-'.$node['element_id']; $element = $this->element($node['element_id']); if ($element === null) { continue; } $text = $this->text($element); $item = $media[$key] ?? [ 'key' => $key, 'type' => $node['output_class'] === 'film' ? 'film' : 'material', 'article' => null, 'subject' => $element, 'title' => $text['title'], 'summary' => $text['summary'], 'kind' => $node['kind'] ?? $this->outputLabel($node['output_class']), 'coverage' => 'nutzbar', 'image' => $node['poster_url'] === null ? null : ($element->thumbnail_alt ?: $text['title']), 'image_url' => $node['poster_url'], 'used_in' => [], 'versions' => [], 'context' => null, ]; foreach ($contexts as $context) { $item['used_in'][$context['id']] = ['title' => $context['title'], 'url' => $context['url']]; } $item['versions'][$node['version_id']] = true; $item['context'] ??= ['composition' => $root, 'version_id' => $node['version_id']]; $media[$key] = $item; } } /** * @return list> */ private function tree(User $user, CompositionVersion $version, bool $full, int $rootId): array { $key = $version->id.':'.($full ? 'voll' : 'kurz').':'.$rootId; return $this->trees[$key] ??= array_map( fn (StructureNode $node): array => $this->present($user, $node, $full, $rootId), $this->structure->tree($version), ); } /** * @return array */ private function present(User $user, StructureNode $node, bool $full, int $rootId): array { $view = null; $download = null; $playback = 'file'; $poster = null; $element = $node->contentElementId === null ? null : $this->element($node->contentElementId); if ($node->contentElementVersionId !== null) { $view = $this->open($user, $node->contentElementVersionId, $this->viewAction($node->outputClass)); if ($full) { $download = $this->open($user, $node->contentElementVersionId, 'laden'); $playback = app(FilmPlayback::class)->mode($node->contentElementVersionId); } if ($view !== null && $element !== null) { $poster = ThumbnailRef::url('member.thumbnail', 'element', $element->id, $element->thumbnail_path); } } $title = $node->title; if ($element !== null) { $title = $this->text($element)['title'] ?: $title; } elseif ($node->compositionId !== null) { $nested = Composition::query()->find($node->compositionId); $title = $nested === null ? $title : ($this->text($nested)['title'] ?: $title); } return [ 'role' => $node->role, 'title' => $title, 'summary' => $element === null ? null : $this->text($element)['summary'], 'kind' => $node->editorialKindLabel ?? $this->outputLabel($node->technicalKindLabel) ?? $node->technicalKindLabel, 'output_class' => $node->outputClass, 'playback' => $playback, 'view_url' => $view, 'poster_url' => $poster, 'download_url' => $download, 'program_url' => $this->programUrl($node), 'composition_id' => $node->compositionId, 'element_id' => $node->contentElementId, 'version_id' => $node->contentElementVersionId, 'content_url' => $node->contentElementVersionId === null ? null : route('member.content', [ 'composition' => $rootId, 'version' => $node->contentElementVersionId, ]), 'children' => array_map( fn (StructureNode $child): array => $this->present($user, $child, $full, $rootId), $node->children, ), ]; } /** * @param list> $nodes * @param list> $parents * @return array{node: array, parents: list>, siblings: list>}|null */ private function locate(array $nodes, int $versionId, array $parents): ?array { foreach ($nodes as $node) { if ($node['version_id'] === $versionId) { return ['node' => $node, 'parents' => $parents, 'siblings' => $nodes]; } if ($node['children'] !== []) { $found = $this->locate($node['children'], $versionId, [...$parents, $node]); if ($found !== null) { return $found; } } } return null; } /** * @param list> $nodes * @return list> */ private function leaves(array $nodes): array { $leaves = []; foreach ($nodes as $node) { if ($node['children'] === []) { $leaves[] = $node; continue; } $leaves = [...$leaves, ...$this->leaves($node['children'])]; } return $leaves; } /** * @param list> $nodes */ private function countCompositions(array $nodes): int { $count = 0; foreach ($nodes as $node) { if ($node['composition_id'] !== null) { $count++; } $count += $this->countCompositions($node['children']); } return $count; } /** * @param list> $nodes */ private function coverage(array $nodes): string { $leaves = $this->leaves($nodes); $open = count(array_filter($leaves, fn (array $leaf): bool => $leaf['view_url'] !== null)); if ($leaves === [] || $open === 0) { return 'nicht enthalten'; } return $open === count($leaves) ? 'nutzbar' : 'teilweise'; } private function element(int $id): ?ContentElement { if (! array_key_exists($id, $this->elements)) { $this->elements[$id] = ContentElement::query()->find($id); } return $this->elements[$id]; } private function programUrl(StructureNode $node): ?string { if ($node->compositionId === null || $node->compositionVersionId === null) { return null; } $version = CompositionVersion::query()->find($node->compositionVersionId); if ($version === null || ! $this->display->isReleased($version)) { return null; } return route('member.program', $node->compositionId); } private function viewAction(?string $outputClass): string { return in_array($outputClass, ['film', 'audio'], true) ? 'streamen' : 'ansehen'; } private function open(User $user, int $versionId, string $action): ?string { $version = ContentElementVersion::query()->with('element')->find($versionId); if ($version === null) { return null; } $releases = ArticleRelease::query() ->whereIn('status', [ReleaseStatus::Veroeffentlicht, ReleaseStatus::AbgeloestVerfuegbar]) ->whereHas('contents', fn ($query) => $query->where('content_element_version_id', $versionId)) ->with('article') ->orderByDesc('id') ->get(); foreach ($releases as $release) { $decision = ($this->access)($user, $release->article, $release, $version, $action); if (! $decision->allowed) { continue; } return SignedLink::temporary('member.media', now()->addMinutes(10), [ 'article' => $release->article->id, 'release' => $release->id, 'version' => $version->id, 'action' => $action, 'reader' => $user->id, ]); } return null; } /** * @return Collection */ private function grants(User $user): Collection { return Grant::query() ->where('status', GrantStatus::Aktiv) ->usableBy($user) ->with([ 'article.currentRelease.rootCompositionVersion.composition.editorialContentKind', 'article.currentRelease.rootElementVersion.element.editorialContentKind', ]) ->orderBy('id') ->get(); } }