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 !== '' || $this->headerId !== null || $this->footerId !== null || $this->coverId !== null; } return $this->name !== $template->name || $this->publisher !== (string) $template->publisher || $this->copyright !== (string) $template->copyright || $this->showCover !== (bool) $template->show_cover || $this->headerId !== $template->header_version_id || $this->footerId !== $template->footer_version_id || $this->coverId !== $template->cover_version_id; } 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 (['header' => $this->headerId, 'footer' => $this->footerId, 'cover' => $this->coverId] as $slot => $id) { if ($id !== null && $this->imageProblem($id) !== null) { Notification::make()->danger()->title('Das Bild für '.$this->slotLabel($slot).' lässt sich nicht verwenden.')->body((string) $this->imageProblem($id))->send(); return; } } $values = [ 'name' => trim($this->name), 'kind' => PdfTemplate::KIND_DOCUMENT, 'show_cover' => $this->showCover, 'header_version_id' => $this->headerId, 'footer_version_id' => $this->footerId, 'cover_version_id' => $this->coverId, '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 { match ($slot) { 'header' => $this->headerId = null, 'footer' => $this->footerId = null, 'cover' => $this->coverId = null, default => 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 || preg_match('/^element:(\d+)$/', (string) $target, $match) !== 1) { return; } $problem = $this->imageProblem((int) $match[1]); if ($problem !== null) { Notification::make()->danger()->title('Dieses Bild lässt sich nicht verwenden.')->body($problem)->send(); return; } match ($slot) { 'header' => $this->headerId = (int) $match[1], 'footer' => $this->footerId = (int) $match[1], 'cover' => $this->coverId = (int) $match[1], default => null, }; $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(?int $versionId): ?string { return $versionId === null ? null : EmbeddedImages::staffSource($versionId); } public function imageTitle(?int $versionId): string { $version = $versionId === null ? null : ContentElementVersion::query()->with('element')->find($versionId); return $version?->element === null ? '' : (string) ($version->element->display_title ?: $version->element->working_title); } 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 imageProblem(int $versionId): ?string { $version = ContentElementVersion::query()->with('element')->find($versionId); if (! $version instanceof ContentElementVersion || $version->element === null || $version->element->output_class !== 'bild') { return 'Das ist kein Bild des Medienbestands.'; } if ($version->status !== ContentVersionStatus::Bereit) { return 'Die Bildfassung ist noch nicht freigegeben.'; } return EmbeddedImages::file($version) === null ? 'Die Bilddatei liegt nicht vor.' : null; } 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->headerId = $template->header_version_id; $this->footerId = $template->footer_version_id; $this->coverId = $template->cover_version_id; } private function clearForm(): void { $this->templateId = 0; $this->name = ''; $this->publisher = ''; $this->copyright = ''; $this->showCover = true; $this->headerId = null; $this->footerId = null; $this->coverId = null; } protected function pickerExceptId(): int { return 0; } }