Eine Vorlage ist reines Layout (Titelseite mit allein stehendem Titel, Kopf- und Fußgrafik aus dem Medienbestand, Herausgeber und Standardvermerk) und wird dem Dokument zugeordnet. Untertitel, Ausgabe, Veröffentlichungsdatum und Copyright-Vermerk gehören zur Dokumentfassung (document_meta). Ohne Vorlage bleibt das PDF unverändert. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
137 lines
5.4 KiB
PHP
137 lines
5.4 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::shiftHeadings(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] += [
|
|
'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[] = [
|
|
'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);
|
|
}
|
|
}
|