Files
laraundfelix/app/Http/Controllers/DocumentPreviewController.php
T
LarsandClaude Sonnet 5.5 b942b5331a Biete Überschriften h3 bis h6 an und nimm sie ins Inhaltsverzeichnis auf
Ein Dokument gliedert sich vor allem durch Kapitel der ersten Ebene und
Überschriften im Text. Der Editor bietet h3 bis h6; h1 und h2 aus eingefügtem
Text werden h3. Alle Überschriften des Kapiteltexts stehen automatisch im
Inhaltsverzeichnis der Seite und des PDF (eingerückt, ohne Nummer), mit
Sprungmarken, im PDF mit Seitenzahl, Verweis und verschachtelten Lesezeichen.
Die PDF-Gliederung kennt jetzt bis zu sechs Ebenen und zählt alle Nachkommen.
Unter einem Unterkapitel rücken die Überschriften eine Stufe tiefer (höchstens h6).

Der bestehende Test erwartete h4 für eine Zwischenüberschrift im Kapitel; das gilt
bewusst nicht mehr (h3 bleibt h3 und trägt eine Sprungmarke). Keine Migration,
der Änderungsnachweis bleibt unverändert.

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

137 lines
5.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Domain\Catalog\Composition;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Catalog\DocumentGuide;
use App\Domain\Catalog\DocumentMeta;
use App\Domain\Catalog\DocumentPdf;
use App\Domain\Catalog\DocumentReader;
use App\Domain\Catalog\EditorialWorkingDraft;
use App\Domain\Catalog\EmbeddedImages;
use App\Domain\Catalog\FormattedText;
use App\Domain\Catalog\PdfTemplate;
use App\Domain\Identity\StaffRole;
use App\Filament\Support\EditorialCatalog;
use App\Models\User;
use Illuminate\Http\Response;
use Illuminate\View\View;
/**
* Vorschau eines Dokuments für die Redaktion: der gespeicherte Entwurf, sonst die letzte bereitgestellte Fassung, so wie ein
* Mitglied sie als Seite und als PDF sieht (ohne Rechteprüfung und ohne Ausgabenachweis). Ungespeicherte Eingaben im Editor
* erscheinen hier erst nach „Entwurf speichern“.
*/
class DocumentPreviewController extends Controller
{
public function show(Composition $composition): View
{
$this->authorizeStaff($composition);
$guide = $this->guide($composition);
$images = EmbeddedImages::staff();
foreach ($guide['chapters'] as $index => $chapter) {
$guide['chapters'][$index]['html'] = DocumentGuide::anchoredHtml($chapter, FormattedText::html($chapter['version']->body, $images));
}
return view('document.preview', ['guide' => $guide, 'composition' => $composition, 'pdfUrl' => route('editorial.document-preview.pdf', $composition)]);
}
public function pdf(Composition $composition): Response
{
$this->authorizeStaff($composition);
$content = app(DocumentPdf::class)->render($this->guide($composition), 'Vorschau der Redaktion', 'vorschau', EmbeddedImages::forStaffPdf());
return response($content, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="vorschau.pdf"',
'Cache-Control' => 'private, no-store',
]);
}
/**
* @return array<string, mixed>
*/
private function guide(Composition $composition): array
{
$chapters = app(DocumentReader::class)->forStaff($composition);
abort_if($chapters === null, 404);
foreach ($chapters as $index => $chapter) {
$chapters[$index] = DocumentGuide::withHeadings($chapters[$index] + [
'allowed' => true,
'grant' => null,
'may_download' => true,
'download_grant' => null,
'state' => 'ok',
'html' => null,
'blocks' => FormattedText::blocks($chapter['version']->body),
]);
}
$draft = app(EditorialWorkingDraft::class)->read($composition);
$ready = $composition->versions()->where('status', ContentVersionStatus::Bereit)->orderByDesc('number')->first();
return [
'title' => app(EditorialCatalog::class)->compositionTitle($composition),
'summary_html' => FormattedText::html($composition->summary),
'pdf_template' => $composition->pdfTemplate,
'meta' => DocumentMeta::normalize($draft !== null ? ($draft['document_meta'] ?? null) : $ready?->document_meta),
'chapters' => $chapters,
];
}
/**
* Muster-PDF einer Vorlage: ein kurzes Handbuch mit Beispielangaben, damit Titelseite, Kopf, Fuß und Seitenzahlen zu sehen
* sind, ohne ein Dokument zu brauchen.
*/
public function template(PdfTemplate $template): Response
{
$this->authorizeUser();
$table = '<table><tbody><tr><th><p>Zeit</p></th><th><p>Inhalt</p></th></tr><tr><td><p>10 Min.</p></td><td><p>Begrüßung</p></td></tr></tbody></table>';
$chapters = [];
foreach (['Grundlagen' => 1, 'Haltung und Abstand' => 1, 'Vertiefung' => 2] as $title => $level) {
$number = count($chapters) + 1;
$chapters[] = DocumentGuide::withHeadings([
'number' => (string) ($level === 1 ? count(array_filter($chapters, fn (array $c): bool => $c['level'] === 1)) + 1 : '2.1'),
'level' => $level,
'anchor' => 'muster-'.$number,
'title' => $title,
'allowed' => true,
'blocks' => FormattedText::blocks('<p>'.str_repeat('Mustertext für die Vorschau der Vorlage. ', 30).'</p><h3>Zwischenüberschrift</h3>'.$table),
]);
}
$guide = [
'title' => 'Musterhandbuch',
'pdf_template' => $template,
'meta' => ['subtitle' => 'Beispiel für eine Titelseite', 'edition' => 'Ausgabe 1.0 · Muster', 'published_on' => now()->format('Y-m-d')],
'chapters' => $chapters,
];
return response(app(DocumentPdf::class)->render($guide, 'Vorschau der Redaktion', 'vorschau'), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="muster.pdf"',
'Cache-Control' => 'private, no-store',
]);
}
private function authorizeUser(): void
{
$user = auth('staff')->user();
abort_unless(
$user instanceof User && ($user->hasStaffRole(StaffRole::Redakteur) || $user->hasStaffRole(StaffRole::Katalogverwalter)),
403,
);
}
private function authorizeStaff(Composition $composition): void
{
$this->authorizeUser();
abort_unless($composition->kind === CompositionKind::Document, 404);
}
}