194 lines
7.4 KiB
PHP
194 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Member;
|
|
|
|
use App\Domain\Catalog\ArticleRelease;
|
|
use App\Domain\Catalog\Composition;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\IdentityDimension;
|
|
use App\Domain\Catalog\LicenseArticle;
|
|
use App\Domain\Entitlement\DecideAccess;
|
|
use App\Domain\Entitlement\Grant;
|
|
use App\Domain\Entitlement\GrantStatus;
|
|
use App\Domain\Entitlement\MediaDelivery;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\CourseOutline;
|
|
use App\Http\LibraryCatalog;
|
|
use App\Http\SignedLink;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\View\View;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class LibraryController extends Controller
|
|
{
|
|
public function index(Request $request): View
|
|
{
|
|
$catalog = app(LibraryCatalog::class)->index($request->user());
|
|
$query = trim((string) $request->query('q', ''));
|
|
|
|
$dimensionIds = array_values(array_filter(array_map(intval(...), (array) $request->query('dimension', []))));
|
|
$dimensions = app(IdentityDimension::class);
|
|
|
|
if ($query !== '') {
|
|
$catalog['programs'] = array_values(array_filter(
|
|
$catalog['programs'],
|
|
fn (array $card): bool => $this->matches($card, $query),
|
|
));
|
|
$catalog['materials'] = array_values(array_filter(
|
|
$catalog['materials'],
|
|
fn (array $card): bool => $this->matches($card, $query),
|
|
));
|
|
}
|
|
|
|
if ($dimensionIds !== []) {
|
|
$catalog['programs'] = array_values(array_filter(
|
|
$catalog['programs'],
|
|
fn (array $card): bool => $dimensions->matches($card['composition'], $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);
|
|
},
|
|
));
|
|
}
|
|
|
|
return view('member.library', [...$catalog, 'query' => $query]);
|
|
}
|
|
|
|
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<string, mixed> $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');
|
|
}
|
|
}
|