Files
laraundfelix/app/Filament/Pages/PdfVorlagen.php
T
LarsandClaude Sonnet 5.5 58b68b768c
Deploy Test / test (push) Successful in 3m18s
Deploy Test / deploy (push) Successful in 29s
Erlaube in PDF-Vorlagen jedes Bild des Medienmanagers, auch ohne Inhaltsfassung
Kopf-, Fuß- und Titelbild einer PDF-Vorlage sind Gestaltung und brauchen keine
Inhaltsfassung. Der Picker bietet auch Präsentationsmedien und für die Website
freigegebene Bilder an (Spalten *_element_id, Klasse TemplateImage); gedruckt wird
die Website-Datei, sonst die Präsentationsdatei. Abgelehnt werden nur gesperrte
oder archivierte Bilder, fehlende Dateien und Nicht-Bilder. Bilder im Text eines
Dokuments bleiben an freigegebene Bildfassungen gebunden.

Die öffentliche Eigenschaft heißt bilder, weil slots mit Livewires internem
Slot-Mechanismus kollidiert.

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

331 lines
11 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;
/**
* 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) !== [];
}
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);
}
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']),
'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);
}
/**
* @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];
}
protected function pickerExceptId(): int
{
return 0;
}
}