*/ public array $anordnung = ['header' => 'full', 'footer' => 'full']; /** * Höhe des Logos in Punkt, wenn Kopf oder Fuß ein Logo tragen. * * @var array */ public array $logohoehe = ['header' => PdfTemplate::LOGO_DEFAULT, 'footer' => PdfTemplate::LOGO_DEFAULT]; /** * Die Bilder je Bildfeld (header, footer, cover) als Ziel des Pickers: `element:` oder `medium:`. * * @var array */ 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 */ 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)]; } 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')]; } 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'], '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 */ 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 */ public function presentationRoleOptions(): array { return []; } /** @return array|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']]; } /** * Anordnung und Logohöhe von Kopf und Fuß, ungültige Eingaben auf gültige Werte gebracht. * * @return list */ 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 */ 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]; } protected function pickerExceptId(): int { return 0; } }