167 lines
5.5 KiB
PHP
167 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http;
|
|
|
|
use App\Domain\Catalog\ArticleRelease;
|
|
use App\Domain\Catalog\Composition;
|
|
use App\Domain\Catalog\CompositionDisplayRelease;
|
|
use App\Domain\Catalog\CompositionStructureReader;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\ReleaseStatus;
|
|
use App\Domain\Catalog\StructureNode;
|
|
use App\Domain\Entitlement\DecideAccess;
|
|
use App\Domain\Entitlement\Grant;
|
|
use App\Domain\Entitlement\GrantStatus;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class LibraryCatalog
|
|
{
|
|
public function __construct(
|
|
private readonly CompositionDisplayRelease $display,
|
|
private readonly CompositionStructureReader $structure,
|
|
private readonly DecideAccess $access,
|
|
) {}
|
|
|
|
/**
|
|
* @return array{programs: list<array<string, mixed>>, materials: list<array<string, mixed>>}
|
|
*/
|
|
public function index(User $user): array
|
|
{
|
|
$programs = [];
|
|
$materials = [];
|
|
|
|
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)) {
|
|
$programs[$composition->id] = [
|
|
'composition' => $composition,
|
|
'version' => $version,
|
|
'title' => $composition->designation,
|
|
'kind' => $composition->editorialContentKind?->label
|
|
?? ($composition->kind?->label()),
|
|
];
|
|
|
|
continue;
|
|
}
|
|
|
|
$materials[$article->id] = [
|
|
'article' => $article,
|
|
'title' => app(CourseOutline::class)->title($article),
|
|
'kind' => app(CourseOutline::class)->kindLabel($article),
|
|
];
|
|
}
|
|
|
|
return [
|
|
'programs' => array_values($programs),
|
|
'materials' => array_values($materials),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array{composition: Composition, title: string, kind: ?string, nodes: list<array<string, mixed>>}
|
|
*/
|
|
public function program(User $user, Composition $composition): array
|
|
{
|
|
$version = $this->display->highestReleasedVersion($composition);
|
|
abort_if($version === null, 404);
|
|
|
|
return [
|
|
'composition' => $composition,
|
|
'title' => $composition->designation,
|
|
'kind' => $composition->editorialContentKind?->label
|
|
?? ($composition->kind?->label()),
|
|
'nodes' => array_map(
|
|
fn (StructureNode $node): array => $this->present($user, $node),
|
|
$this->structure->tree($version),
|
|
),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function present(User $user, StructureNode $node): array
|
|
{
|
|
$view = null;
|
|
$download = null;
|
|
|
|
if ($node->contentElementVersionId !== null) {
|
|
$view = $this->open($user, $node->contentElementVersionId, $this->viewAction($node->outputClass));
|
|
$download = $this->open($user, $node->contentElementVersionId, 'laden');
|
|
}
|
|
|
|
return [
|
|
'role' => $node->role,
|
|
'title' => $node->title,
|
|
'kind' => $node->editorialKindLabel ?? $node->technicalKindLabel,
|
|
'output_class' => $node->outputClass,
|
|
'view_url' => $view,
|
|
'download_url' => $download,
|
|
'children' => array_map(
|
|
fn (StructureNode $child): array => $this->present($user, $child),
|
|
$node->children,
|
|
),
|
|
];
|
|
}
|
|
|
|
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<int, Grant>
|
|
*/
|
|
private function grants(User $user): Collection
|
|
{
|
|
return Grant::query()
|
|
->where('status', GrantStatus::Aktiv)
|
|
->whereHas('acquisition', fn ($query) => $query->where('party_id', $user->party_id))
|
|
->with([
|
|
'article.currentRelease.rootCompositionVersion.composition.editorialContentKind',
|
|
'article.currentRelease.rootElementVersion.element.editorialContentKind',
|
|
])
|
|
->orderBy('id')
|
|
->get();
|
|
}
|
|
}
|