index($request->user()); $untouched = $catalog['programs'] === [] && $catalog['modules'] === [] && $catalog['materials'] === []; $query = trim((string) $request->query('q', '')); $nutzung = (string) $request->query('nutzung', ''); $dimensionIds = array_values(array_filter(array_map(intval(...), (array) $request->query('dimension', [])))); $dimensions = app(IdentityDimension::class); foreach (['programs', 'modules'] as $shelf) { if ($query !== '') { $catalog[$shelf] = array_values(array_filter( $catalog[$shelf], fn (array $card): bool => $this->matches($card, $query), )); } if ($dimensionIds !== []) { $catalog[$shelf] = array_values(array_filter( $catalog[$shelf], fn (array $card): bool => $dimensions->matches($card['composition'], $dimensionIds), )); } if (in_array($nutzung, ['nutzbar', 'teilweise', 'nicht enthalten'], true)) { $catalog[$shelf] = array_values(array_filter( $catalog[$shelf], fn (array $card): bool => ($card['coverage'] ?? '') === $nutzung, )); } } if ($query !== '') { $catalog['materials'] = array_values(array_filter( $catalog['materials'], fn (array $card): bool => $this->matches($card, $query), )); } if ($dimensionIds !== []) { $catalog['materials'] = array_values(array_filter( $catalog['materials'], function (array $card) use ($dimensions, $dimensionIds): bool { $release = $card['article']->currentRelease; $subject = $release?->rootCompositionVersion?->composition ?? $release?->rootElementVersion?->element ?? $card['article']; return $dimensions->matches($subject, $dimensionIds); }, )); } if ($nutzung === 'teilweise' || $nutzung === 'nicht enthalten') { $catalog['materials'] = []; } return view('member.library', [ ...$catalog, 'query' => $query, 'sicht' => $sicht, 'nutzung' => $nutzung, 'dimensionIds' => $dimensionIds, 'filterDimensions' => EditorialDimension::query()->with('values')->orderBy('label')->get(), 'libraryEmpty' => $untouched, ]); } public function programs(Request $request): View { return $this->index($request, 'programme'); } public function modules(Request $request): View { return $this->index($request, 'module'); } public function program(Request $request, Composition $composition): View { return view('member.program', app(LibraryCatalog::class)->program($request->user(), $composition)); } public function materials(Request $request): View { return view('member.materials', [ 'grants' => $this->grantQuery($request)->with('article.currentRelease.contents.elementVersion.element')->get(), ]); } public function grants(Request $request): View { return view('member.grants', [ 'grants' => $this->grantQuery($request)->get(), ]); } public function show(Request $request, LicenseArticle $article): View { $release = $article->currentRelease()->with('contents.elementVersion.element.pillars')->first(); $entries = []; if ($release !== null) { foreach ($release->contents as $content) { $version = $content->elementVersion; $class = $version->element->output_class; $metadata = is_array($version->metadata) ? $version->metadata : []; $action = $class === 'film' || $class === 'audio' ? 'streamen' : 'ansehen'; $decision = app(DecideAccess::class)($request->user(), $article, $release, $version, $action); $file = $metadata['file'] ?? null; $entries[] = [ 'content' => $content, 'version' => $version, 'role' => $content->role, 'action' => $action, 'allowed' => $decision->allowed, 'reason' => $decision->reason, 'decision' => is_numeric($metadata['decision_seconds'] ?? null) ? (int) $metadata['decision_seconds'] : null, 'playable' => $decision->allowed && is_string($file) && $file !== '' && ($class === 'film' || $class === 'audio'), 'pillars' => $version->element->pillars->pluck('name')->all(), 'url' => $decision->allowed ? SignedLink::temporary('member.media', now()->addMinutes(10), [ 'article' => $article->id, 'release' => $release->id, 'version' => $version->id, 'action' => $action, 'reader' => $request->user()->id, ]) : null, ]; } } return view('member.article', [ 'article' => $article, 'release' => $release, 'entries' => $entries, 'title' => app(CourseOutline::class)->title($article), 'kind' => app(CourseOutline::class)->kindLabel($article), ]); } public function media( Request $request, LicenseArticle $article, ArticleRelease $release, ContentElementVersion $version, string $action, ): View|StreamedResponse { abort_unless(in_array($action, ['ansehen', 'streamen', 'laden'], true), 404); abort_unless((int) $request->query('reader') === $request->user()->id, 403); $decision = app(DecideAccess::class)($request->user(), $article, $release, $version, $action); abort_unless($decision->allowed, 403, $decision->reason); if ($decision->grant !== null) { MediaDelivery::query()->create([ 'grant_id' => $decision->grant->id, 'license_article_id' => $article->id, 'article_release_id' => $release->id, 'content_element_version_id' => $version->id, 'action' => $action, 'format' => 'bestand', 'delivered_at' => now(), ]); } $version->loadMissing('element'); $file = $version->metadata['file'] ?? null; if (! is_string($file) || $file === '' || str_contains($file, '..')) { return view('member.read', [ 'article' => $article, 'version' => $version, 'license' => $decision->grant?->rights_snapshot['purpose'] ?? null, ]); } $disk = Storage::disk('media_derived'); abort_unless($disk->exists($file), 404); return $disk->response($file, basename($file), [ 'Content-Disposition' => $action === 'laden' ? 'attachment' : 'inline', ]); } /** * @param array $card */ private function matches(array $card, string $query): bool { $haystack = mb_strtolower(($card['title'] ?? '').' '.($card['summary'] ?? '')); return str_contains($haystack, mb_strtolower($query)); } private function grantQuery(Request $request) { return Grant::query() ->where('status', GrantStatus::Aktiv) ->whereHas('acquisition', fn ($query) => $query->where('party_id', $request->user()->party_id)) ->with('article.currentRelease') ->orderBy('id'); } }