Files
laraundfelix/app/Http/Controllers/Member/ExerciseController.php
T
LarsandClaude Sonnet 5.5 c57a506f85 Biete Tabellen und Bilder im gemeinsamen Texteditor an und setze sie in Ansicht und PDF
Tabellen und Bilder gehören zur festen Auswahl von FormattedText. Ein Bild
ist ein Verweis auf eine freigegebene Bildfassung und kommt per Picker aus
dem Medienbestand; der Stand schreibt die Bildfassung mit fest, die Rechte
entscheidet DecideAccess. Der PDF-Satz (PdfFlow) ist für Übung und kommende
Dokumente gemeinsam; PdfDocument kann zusätzlich Bilder, Flächen, Verweise
und Lesezeichen, Belege bleiben byte-gleich.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 11:31:03 +01:00

133 lines
5.8 KiB
PHP

<?php
namespace App\Http\Controllers\Member;
use App\Domain\Catalog\ArticleRelease;
use App\Domain\Catalog\CompositionVersion;
use App\Domain\Catalog\EmbeddedImages;
use App\Domain\Catalog\ExerciseGuide;
use App\Domain\Catalog\ExercisePdf;
use App\Domain\Catalog\LicenseArticle;
use App\Domain\Media\DeliveryLog;
use App\Http\Controllers\Controller;
use App\Http\SignedLink;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
/**
* Die Übung als formatierte Anleitung innerhalb des erworbenen Moduls oder Programms und ihre PDF-Ausgabe. Beide zeigen
* die im Artikel-Stand gebundene Übungsfassung und die dort angebotenen Varianten. Über jeden Abschnitt und jedes
* Medium entscheidet `DecideAccess`; auch die direkte PDF-Adresse wird bei jedem Abruf neu entschieden.
*/
class ExerciseController extends Controller
{
public function show(Request $request, LicenseArticle $article, ArticleRelease $release, string $path): View
{
[$guide, $located] = $this->load($request, $article, $release, $path);
$user = $request->user();
$copy = DeliveryLog::newCopyId();
$shown = false;
foreach ($guide['entries'] as $index => $entry) {
$link = fn (string $action) => SignedLink::temporary('member.media', now()->addMinutes(10), [
'article' => $article->id,
'release' => $release->id,
'version' => $entry['version']->id,
'action' => $action,
'reader' => $user->id,
]);
$guide['entries'][$index]['url'] = $entry['allowed'] && $entry['class'] !== 'text' ? $link($entry['action']) : null;
$guide['entries'][$index]['download_url'] = $entry['allowed'] && $entry['may_download'] && $entry['class'] !== 'text' ? $link('laden') : null;
if ($entry['allowed'] && $entry['class'] === 'text' && $entry['grant'] !== null) {
app(DeliveryLog::class)->write($entry['grant'], $article, $release, $entry['version'], 'ansehen', 'text', hash('sha256', (string) $entry['version']->body), 'k2', $copy, $located['version']->id);
$shown = true;
}
}
$pdfAllowed = app(ExercisePdf::class)->sections($guide) !== null;
return view('member.exercise', [
'guide' => $guide,
'article' => $article,
'release' => $release,
'path' => $path,
'reader' => $user->name,
'copy' => $shown ? $copy : null,
'pdfUrl' => $pdfAllowed ? SignedLink::temporary('member.exercise.pdf', now()->addMinutes(10), [
'article' => $article->id,
'release' => $release->id,
'path' => $path,
'variante' => $guide['chosen'],
'reader' => $user->id,
]) : null,
'variantUrl' => fn (string $key): string => route('member.exercise', ['article' => $article, 'release' => $release, 'path' => $path, 'variante' => $key]),
]);
}
public function pdf(Request $request, LicenseArticle $article, ArticleRelease $release, string $path): Response
{
abort_unless((int) $request->query('reader') === $request->user()->id, 403);
[$guide, $located] = $this->load($request, $article, $release, $path);
$pdf = app(ExercisePdf::class);
$sections = $pdf->sections($guide);
abort_if($sections === null, 403, 'Für dieses PDF fehlt die Berechtigung oder ein erforderlicher Bestandteil.');
$copy = DeliveryLog::newCopyId();
$variant = collect($guide['variants'])->firstWhere('key', $guide['chosen']);
$images = app(EmbeddedImages::class)->forPdf($request->user(), $article, $release);
$content = $pdf->render(
$guide,
$sections,
$request->user()->name,
$copy,
(string) ($variant['name'] ?? ''),
route('member.exercise', ['article' => $article, 'release' => $release, 'path' => $path, 'variante' => $guide['chosen']]),
$images,
);
$checksum = hash('sha256', $content);
foreach ($sections as $section) {
$grant = $section['download_grant'] ?? $section['grant'];
if ($grant !== null) {
app(DeliveryLog::class)->write($grant, $article, $release, $section['version'], 'laden', 'pdf', $checksum, 'k2', $copy, $located['version']->id);
}
}
// Bilder im Text sind eigene Fassungen: Auch sie stehen im Ausgabenachweis dieses PDF.
foreach ($images->used() as $image) {
app(DeliveryLog::class)->write($image['grant'], $article, $release, $image['version'], 'laden', 'pdf', $checksum, 'k2', $copy, $located['version']->id);
}
$name = str($guide['title'])->slug()->value() ?: 'uebung';
return response($content, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="'.$name.'.pdf"',
'X-License-Copy' => $copy,
'Cache-Control' => 'private, no-store',
]);
}
/**
* @return array{0: array<string, mixed>, 1: array{version: CompositionVersion, selection: list<string>|null}}
*/
private function load(Request $request, LicenseArticle $article, ArticleRelease $release, string $path): array
{
$guides = app(ExerciseGuide::class);
abort_unless($guides->mayOpen($request->user(), $article, $release), 403);
$located = $guides->locate($release, $path);
abort_if($located === null, 404);
$variant = $request->query('variante');
return [
$guides->build($request->user(), $article, $release, $located['version'], $located['selection'], is_string($variant) ? $variant : null),
$located,
];
}
}