Die PDF-Vorlage bestimmt, ob das gedruckte Verzeichnis erscheint (automatisch ab zwei Einträgen, immer, nie) und bis zu welcher Stufe es reicht (nur Kapitel, bis Überschrift 3 bis 6). Ohne Verzeichnis beginnt das erste Kapitel auf Seite 1 beziehungsweise nach der Titelseite, ohne leere Seite. Lesezeichen im PDF und das Verzeichnis der Seitenansicht zeigen weiter alle Ebenen. Dokumente ohne Vorlage nutzen automatisch mit voller Tiefe. Additive Migration mit Vorgaben. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
388 lines
13 KiB
PHP
388 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Domain\Catalog\PdfTemplate;
|
|
use App\Domain\Catalog\TemplateImage;
|
|
use App\Filament\Resources\ContentElements\ContentElementResource;
|
|
use App\Filament\Support\ChoosesMedia;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Filament\Support\Enums\Width;
|
|
use Illuminate\Contracts\Support\Htmlable;
|
|
|
|
/**
|
|
* PDF-Vorlagen für Dokumente (ADR-51, Nachtrag): Titelseite, grafische Kopf- und Fußzeile und Standardvermerke. Die Bilder
|
|
* wählt der gemeinsame Picker aus dem Medienbestand (freigegebene Bildfassungen); eine Vorlage ist reines Layout und ändert
|
|
* keinen Inhalt. Gespeichert wird erst mit „Vorlage speichern“; offene Eingaben bleiben beim Wechsel der Vorlage nicht stehen,
|
|
* der Wechsel fragt bei offenen Änderungen nach.
|
|
*/
|
|
class PdfVorlagen extends Page
|
|
{
|
|
use ChoosesMedia;
|
|
|
|
protected static string|\UnitEnum|null $navigationGroup = 'Inhalte';
|
|
|
|
protected static ?string $navigationLabel = 'PDF-Vorlagen';
|
|
|
|
protected static ?int $navigationSort = 60;
|
|
|
|
protected static ?string $slug = 'pdf-vorlagen';
|
|
|
|
protected static ?string $title = 'PDF-Vorlagen';
|
|
|
|
protected string $view = 'filament.pdf.vorlagen';
|
|
|
|
/** Gewählte Vorlage; 0 = neue, noch nicht gespeicherte. */
|
|
public int $templateId = 0;
|
|
|
|
public string $name = '';
|
|
|
|
public string $publisher = '';
|
|
|
|
public string $copyright = '';
|
|
|
|
public bool $showCover = true;
|
|
|
|
/** Inhaltsverzeichnis im PDF: `auto` (nur bei mehr als einem Eintrag), `always`, `never`. */
|
|
public string $tocModus = 'auto';
|
|
|
|
/** Tiefe des Verzeichnisses: 2 = nur Kapitel, 3 bis 6 = bis zu dieser Überschriftenstufe. */
|
|
public int $tocTiefe = PdfTemplate::TOC_DEPTH_MAX;
|
|
|
|
/**
|
|
* Anordnung von Kopf und Fuß: `full` (Band in voller Seitenbreite), `left` oder `right` (Logo).
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
public array $anordnung = ['header' => 'full', 'footer' => 'full'];
|
|
|
|
/**
|
|
* Höhe des Logos in Punkt, wenn Kopf oder Fuß ein Logo tragen.
|
|
*
|
|
* @var array<string, int|string>
|
|
*/
|
|
public array $logohoehe = ['header' => PdfTemplate::LOGO_DEFAULT, 'footer' => PdfTemplate::LOGO_DEFAULT];
|
|
|
|
/**
|
|
* Die Bilder je Bildfeld (header, footer, cover) als Ziel des Pickers: `element:<Fassung>` oder `medium:<Medium>`.
|
|
*
|
|
* @var array<string, string|null>
|
|
*/
|
|
public array $bilder = ['header' => null, 'footer' => null, 'cover' => null];
|
|
|
|
/** Das Bildfeld, in das die Auswahl übernommen wird: header, footer oder cover. */
|
|
public ?string $bildFeld = null;
|
|
|
|
/** @var array{aktion: string, argument: int|string|null, titel: string, text: string, label: string, gefahr: bool}|null */
|
|
public ?array $bestaetigung = null;
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
return ContentElementResource::editor();
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$first = PdfTemplate::query()->where('kind', PdfTemplate::KIND_DOCUMENT)->orderBy('name')->first();
|
|
$first instanceof PdfTemplate ? $this->loadTemplate($first) : $this->clearForm();
|
|
}
|
|
|
|
public function getHeading(): string|Htmlable|null
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function getMaxContentWidth(): Width|string|null
|
|
{
|
|
return Width::Full;
|
|
}
|
|
|
|
/**
|
|
* @return list<array{id: int, name: string, documents: int}>
|
|
*/
|
|
public function templates(): array
|
|
{
|
|
return PdfTemplate::query()->where('kind', PdfTemplate::KIND_DOCUMENT)->withCount('compositions')->orderBy('name')->get()
|
|
->map(fn (PdfTemplate $template): array => ['id' => $template->id, 'name' => $template->name, 'documents' => (int) $template->compositions_count])->all();
|
|
}
|
|
|
|
public function select(int $id): void
|
|
{
|
|
$template = PdfTemplate::query()->where('kind', PdfTemplate::KIND_DOCUMENT)->find($id);
|
|
|
|
if ($template instanceof PdfTemplate) {
|
|
$this->loadTemplate($template);
|
|
}
|
|
}
|
|
|
|
public function newTemplate(): void
|
|
{
|
|
$this->clearForm();
|
|
}
|
|
|
|
/** Ob die Eingaben vom gespeicherten Stand abweichen. */
|
|
public function changed(): bool
|
|
{
|
|
$template = $this->templateId > 0 ? PdfTemplate::query()->find($this->templateId) : null;
|
|
|
|
if (! $template instanceof PdfTemplate) {
|
|
return $this->name !== '' || $this->publisher !== '' || $this->copyright !== '' || array_filter($this->bilder) !== [] || $this->layouts() !== [PdfTemplate::normalizeLayout(null, null), PdfTemplate::normalizeLayout(null, null)] || $this->tocSetting() !== PdfTemplate::normalizeToc(null, null);
|
|
}
|
|
|
|
return $this->name !== $template->name
|
|
|| $this->publisher !== (string) $template->publisher
|
|
|| $this->copyright !== (string) $template->copyright
|
|
|| $this->showCover !== (bool) $template->show_cover
|
|
|| $this->bilder !== $this->templateSlots($template)
|
|
|| $this->layouts() !== [$template->layout('header'), $template->layout('footer')]
|
|
|| $this->tocSetting() !== $template->toc();
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
abort_unless(ContentElementResource::editor(), 403);
|
|
|
|
if (trim($this->name) === '') {
|
|
Notification::make()->danger()->title('Der Name fehlt.')->body('Er bezeichnet die Vorlage bei der Auswahl am Dokument.')->send();
|
|
|
|
return;
|
|
}
|
|
|
|
foreach (PdfTemplate::SLOTS as $slot) {
|
|
$problem = $this->bilder[$slot] === null ? null : TemplateImage::problem($this->bilder[$slot]);
|
|
|
|
if ($problem !== null) {
|
|
Notification::make()->danger()->title('Das Bild für '.$this->slotLabel($slot).' lässt sich nicht verwenden.')->body($problem)->send();
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
$values = [
|
|
'name' => trim($this->name),
|
|
'kind' => PdfTemplate::KIND_DOCUMENT,
|
|
'show_cover' => $this->showCover,
|
|
...PdfTemplate::columns('header', $this->bilder['header']),
|
|
...PdfTemplate::columns('footer', $this->bilder['footer']),
|
|
...PdfTemplate::columns('cover', $this->bilder['cover']),
|
|
'header_align' => $this->layouts()[0]['align'],
|
|
'header_logo_height' => $this->layouts()[0]['height'],
|
|
'footer_align' => $this->layouts()[1]['align'],
|
|
'footer_logo_height' => $this->layouts()[1]['height'],
|
|
'toc_mode' => $this->tocSetting()['mode'],
|
|
'toc_depth' => $this->tocSetting()['depth'],
|
|
'publisher' => trim($this->publisher) === '' ? null : trim($this->publisher),
|
|
'copyright' => trim($this->copyright) === '' ? null : trim($this->copyright),
|
|
];
|
|
$template = $this->templateId > 0 ? PdfTemplate::query()->find($this->templateId) : null;
|
|
$template instanceof PdfTemplate ? $template->update($values) : $template = PdfTemplate::query()->create($values);
|
|
$this->loadTemplate($template);
|
|
Notification::make()->success()->title('Vorlage gespeichert')->body('Sie gilt sofort für jedes Dokument, das sie verwendet, auch für ältere Fassungen.')->send();
|
|
}
|
|
|
|
public function askConfirmation(string $aktion): void
|
|
{
|
|
if ($aktion === 'delete' && $this->templateId > 0) {
|
|
$uses = PdfTemplate::query()->withCount('compositions')->find($this->templateId)?->compositions_count ?? 0;
|
|
$this->bestaetigung = [
|
|
'aktion' => 'delete',
|
|
'argument' => null,
|
|
'titel' => '„'.$this->name.'“ löschen?',
|
|
'text' => $uses > 0
|
|
? $uses.' '.($uses === 1 ? 'Dokument verwendet' : 'Dokumente verwenden').' diese Vorlage und '.($uses === 1 ? 'erzeugt' : 'erzeugen').' danach wieder das einfache PDF ohne Titelseite. Die Bilder bleiben im Medienbestand.'
|
|
: 'Die Vorlage wird entfernt. Die Bilder bleiben im Medienbestand.',
|
|
'label' => 'Vorlage löschen',
|
|
'gefahr' => true,
|
|
];
|
|
}
|
|
}
|
|
|
|
public function confirmConfirmation(): void
|
|
{
|
|
$frage = $this->bestaetigung;
|
|
$this->bestaetigung = null;
|
|
|
|
if (($frage['aktion'] ?? null) === 'delete' && $this->templateId > 0) {
|
|
abort_unless(ContentElementResource::editor(), 403);
|
|
PdfTemplate::query()->whereKey($this->templateId)->delete();
|
|
$this->mount();
|
|
Notification::make()->success()->title('Vorlage gelöscht')->send();
|
|
}
|
|
}
|
|
|
|
public function cancelConfirmation(): void
|
|
{
|
|
$this->bestaetigung = null;
|
|
}
|
|
|
|
public function openImagePicker(string $slot): void
|
|
{
|
|
if (! in_array($slot, ['header', 'footer', 'cover'], true)) {
|
|
return;
|
|
}
|
|
|
|
$this->bildFeld = $slot;
|
|
$this->resetPicker('vorlagenbild');
|
|
$this->pickerKind = 'bild';
|
|
}
|
|
|
|
public function clearImage(string $slot): void
|
|
{
|
|
if (in_array($slot, PdfTemplate::SLOTS, true)) {
|
|
$this->bilder[$slot] = null;
|
|
}
|
|
}
|
|
|
|
public function pickerAllowsMany(): bool
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public function closePicker(): void
|
|
{
|
|
$this->pickerOpen = false;
|
|
$this->pickerSelected = '';
|
|
$this->pickerInspected = '';
|
|
$this->pickerMarked = [];
|
|
$this->bildFeld = null;
|
|
}
|
|
|
|
public function confirmPicker(): void
|
|
{
|
|
$target = array_key_first($this->pickerMarked);
|
|
$slot = $this->bildFeld;
|
|
|
|
if ($target === null || $slot === null || ! in_array($slot, PdfTemplate::SLOTS, true)) {
|
|
return;
|
|
}
|
|
|
|
$problem = TemplateImage::problem((string) $target);
|
|
|
|
if ($problem !== null) {
|
|
Notification::make()->danger()->title('Dieses Bild lässt sich nicht verwenden.')->body($problem)->send();
|
|
|
|
return;
|
|
}
|
|
|
|
$this->bilder[$slot] = (string) $target;
|
|
$this->closePicker();
|
|
}
|
|
|
|
/** @return array<string, string> */
|
|
public function pickerKinds(): array
|
|
{
|
|
return ['bild' => 'Bilder'];
|
|
}
|
|
|
|
public function pickerTitle(): string
|
|
{
|
|
return 'Bild für '.$this->slotLabel((string) $this->bildFeld);
|
|
}
|
|
|
|
public function pickerConfirmLabel(): string
|
|
{
|
|
return 'Bild übernehmen';
|
|
}
|
|
|
|
/** @return array{art: string, titel: string} */
|
|
public function pickerKontext(): array
|
|
{
|
|
return ['art' => 'die PDF-Vorlage', 'titel' => trim($this->name) !== '' ? $this->name : 'neue Vorlage'];
|
|
}
|
|
|
|
/** @return array<string, string> */
|
|
public function presentationRoleOptions(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
/** @return array<string, mixed>|null */
|
|
public function pickerReplacing(): ?array
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function imageUrl(?string $target): ?string
|
|
{
|
|
return TemplateImage::previewUrl($target);
|
|
}
|
|
|
|
public function imageTitle(?string $target): string
|
|
{
|
|
return TemplateImage::title($target);
|
|
}
|
|
|
|
public function slotLabel(string $slot): string
|
|
{
|
|
return match ($slot) {
|
|
'header' => 'den Kopf der Seiten',
|
|
'footer' => 'den Fuß der Seiten',
|
|
'cover' => 'die Titelseite',
|
|
default => 'die Vorlage',
|
|
};
|
|
}
|
|
|
|
private function loadTemplate(PdfTemplate $template): void
|
|
{
|
|
$this->templateId = $template->id;
|
|
$this->name = $template->name;
|
|
$this->publisher = (string) $template->publisher;
|
|
$this->copyright = (string) $template->copyright;
|
|
$this->showCover = (bool) $template->show_cover;
|
|
$this->bilder = $this->templateSlots($template);
|
|
$this->anordnung = ['header' => $template->layout('header')['align'], 'footer' => $template->layout('footer')['align']];
|
|
$this->logohoehe = ['header' => $template->layout('header')['height'], 'footer' => $template->layout('footer')['height']];
|
|
$this->tocModus = $template->toc()['mode'];
|
|
$this->tocTiefe = $template->toc()['depth'];
|
|
}
|
|
|
|
/**
|
|
* @return array{mode: string, depth: int}
|
|
*/
|
|
private function tocSetting(): array
|
|
{
|
|
return PdfTemplate::normalizeToc($this->tocModus, $this->tocTiefe);
|
|
}
|
|
|
|
/**
|
|
* Anordnung und Logohöhe von Kopf und Fuß, ungültige Eingaben auf gültige Werte gebracht.
|
|
*
|
|
* @return list<array{align: string, height: int}>
|
|
*/
|
|
private function layouts(): array
|
|
{
|
|
return [
|
|
PdfTemplate::normalizeLayout($this->anordnung['header'] ?? null, $this->logohoehe['header'] ?? null),
|
|
PdfTemplate::normalizeLayout($this->anordnung['footer'] ?? null, $this->logohoehe['footer'] ?? null),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string|null>
|
|
*/
|
|
private function templateSlots(PdfTemplate $template): array
|
|
{
|
|
return ['header' => $template->target('header'), 'footer' => $template->target('footer'), 'cover' => $template->target('cover')];
|
|
}
|
|
|
|
private function clearForm(): void
|
|
{
|
|
$this->templateId = 0;
|
|
$this->name = '';
|
|
$this->publisher = '';
|
|
$this->copyright = '';
|
|
$this->showCover = true;
|
|
$this->bilder = ['header' => null, 'footer' => null, 'cover' => null];
|
|
$this->anordnung = ['header' => 'full', 'footer' => 'full'];
|
|
$this->logohoehe = ['header' => PdfTemplate::LOGO_DEFAULT, 'footer' => PdfTemplate::LOGO_DEFAULT];
|
|
$this->tocModus = 'auto';
|
|
$this->tocTiefe = PdfTemplate::TOC_DEPTH_MAX;
|
|
}
|
|
|
|
protected function pickerExceptId(): int
|
|
{
|
|
return 0;
|
|
}
|
|
}
|