Erlaube in PDF-Vorlagen jedes Bild des Medienmanagers, auch ohne Inhaltsfassung
Deploy Test / test (push) Successful in 3m18s
Deploy Test / deploy (push) Successful in 29s

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>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-24 16:05:09 +01:00
1 parent 765b8e779e
commit 58b68b768c
13 files changed
+369 -85

No files matched your search

+6 -7
View File
@@ -5,7 +5,6 @@
use App\Domain\Commerce\Receipt\PdfDocument;
use App\Domain\Media\PdfImage;
use Closure;
use Illuminate\Support\Facades\Storage;
use Transliterator;
/**
@@ -88,9 +87,9 @@ public function render(array $guide, string $licensee, string $copyId, ?PdfImage
private function prepareLayout(?PdfTemplate $template): void
{
$this->bands = [
'header' => $this->band($template?->headerVersion, 140.0),
'footer' => $this->band($template?->footerVersion, 140.0),
'cover' => $template !== null && $template->show_cover ? $this->band($template->coverVersion, self::PAGE_HEIGHT * 0.4) : null,
'header' => $this->band($template?->target('header'), 140.0),
'footer' => $this->band($template?->target('footer'), 140.0),
'cover' => $template !== null && $template->show_cover ? $this->band($template->target('cover'), self::PAGE_HEIGHT * 0.4) : null,
];
$header = (float) ($this->bands['header']['height'] ?? 0);
$footer = (float) ($this->bands['footer']['height'] ?? 0);
@@ -104,10 +103,10 @@ private function prepareLayout(?PdfTemplate $template): void
*
* @return array{jpeg: string, pixelWidth: int, pixelHeight: int, width: float, height: float, x: float}|null
*/
private function band(?ContentElementVersion $version, float $maxHeight): ?array
private function band(?string $target, float $maxHeight): ?array
{
$file = $version === null ? null : EmbeddedImages::file($version);
$image = $file === null ? null : PdfImage::fromBytes((string) Storage::disk('media_derived')->get($file));
$bytes = TemplateImage::bytes($target);
$image = $bytes === null ? null : PdfImage::fromBytes($bytes);
if ($image === null) {
return null;
+31
View File
@@ -22,6 +22,9 @@ class PdfTemplate extends Model
'header_version_id',
'footer_version_id',
'cover_version_id',
'header_element_id',
'footer_element_id',
'cover_element_id',
'publisher',
'copyright',
];
@@ -31,6 +34,34 @@ protected function casts(): array
return ['show_cover' => 'boolean'];
}
public const SLOTS = ['header', 'footer', 'cover'];
/**
* Das Bild eines Bildfelds als Ziel des Pickers: `element:<Fassung>`, `medium:<Medium>` oder null.
*/
public function target(string $slot): ?string
{
$version = $this->getAttribute($slot.'_version_id');
$element = $this->getAttribute($slot.'_element_id');
return $version !== null ? 'element:'.$version : ($element !== null ? 'medium:'.$element : null);
}
/**
* Die Spaltenwerte eines Bildfelds für ein Ziel des Pickers.
*
* @return array<string, int|null>
*/
public static function columns(string $slot, ?string $target): array
{
$ref = TemplateImage::parse($target);
return [
$slot.'_version_id' => ($ref['type'] ?? null) === 'version' ? $ref['id'] : null,
$slot.'_element_id' => ($ref['type'] ?? null) === 'medium' ? $ref['id'] : null,
];
}
public function headerVersion(): BelongsTo
{
return $this->belongsTo(ContentElementVersion::class, 'header_version_id');
+148
View File
@@ -0,0 +1,148 @@
<?php
namespace App\Domain\Catalog;
use App\Filament\Support\EditorialCatalog;
use Illuminate\Support\Facades\Storage;
/**
* Ein Bild einer PDF-Vorlage (ADR-51, Nachtrag). Es kommt aus dem Medienmanager in einer von zwei Formen, die der gemeinsame
* Picker als Ziel liefert:
*
* - `element:<Fassung>`: ein Bild mit Inhaltsfassung (Datei im abgeleiteten Bestand), gleich in welchem Stand; nur ein gesperrtes Bild nicht,
* - `medium:<Medium>`: ein Bild ohne Inhaltsfassung, also ein Präsentationsmedium oder ein für die Website freigegebenes Bild. Gedruckt wird
* die für die Website freigegebene Datei, sonst die Präsentationsdatei.
*
* Jedes Bild mit vorhandener Datei lässt sich verwenden. Das Bild ist Gestaltung der Vorlage und kein lizenzierter Inhalt; es geht nicht über
* die Rechteprüfung der Mitglieder und braucht keine Inhaltsfassung.
*/
final class TemplateImage
{
/**
* @return array{type: 'version'|'medium', id: int}|null
*/
public static function parse(?string $target): ?array
{
if ($target !== null && preg_match('/^(element|medium):(\d+)$/', $target, $match) === 1) {
return ['type' => $match[1] === 'element' ? 'version' : 'medium', 'id' => (int) $match[2]];
}
return null;
}
/** Die Bilddatei als Bytes, oder null, wenn sie nicht vorliegt. */
public static function bytes(?string $target): ?string
{
$source = self::source($target);
return $source === null ? null : (string) Storage::disk($source['disk'])->get($source['path']);
}
/** Warum sich das Bild nicht verwenden lässt; null, wenn es geht. */
public static function problem(?string $target): ?string
{
$ref = self::parse($target);
if ($ref === null) {
return 'Das ist kein Bild des Medienmanagers.';
}
if ($ref['type'] === 'version') {
$version = ContentElementVersion::query()->with('element')->find($ref['id']);
if (! $version instanceof ContentElementVersion || $version->element === null || $version->element->output_class !== 'bild') {
return 'Das ist kein Bild des Medienmanagers.';
}
// Gestaltung braucht keine Freigabe der Fassung; nur ein bewusst gesperrtes Bild bleibt außen vor.
if ($version->status === ContentVersionStatus::Gesperrt) {
return 'Dieses Bild ist deaktiviert.';
}
} else {
$element = ContentElement::query()->find($ref['id']);
if (! $element instanceof ContentElement || $element->output_class !== 'bild' || $element->archived_at !== null) {
return 'Das ist kein Bild des Medienmanagers.';
}
}
return self::source($target) === null ? 'Die Bilddatei liegt nicht vor.' : null;
}
public static function title(?string $target): string
{
$ref = self::parse($target);
$element = match ($ref['type'] ?? null) {
'version' => ContentElementVersion::query()->with('element')->find($ref['id'])?->element,
'medium' => ContentElement::query()->find($ref['id']),
default => null,
};
return $element === null ? '' : (string) ($element->display_title ?: $element->working_title);
}
/** Adresse zur Ansicht in der Verwaltung (Vorschau im Formular). */
public static function previewUrl(?string $target): ?string
{
$ref = self::parse($target);
if ($ref === null) {
return null;
}
if ($ref['type'] === 'version') {
return EmbeddedImages::staffSource($ref['id']);
}
$element = ContentElement::query()->find($ref['id']);
if (! $element instanceof ContentElement) {
return null;
}
if ($element->website_released_at !== null && $element->website_public_path !== null) {
return route('website.media', $element);
}
return app(EditorialCatalog::class)->cardUrl('element', $element->id, $element->thumbnail_path);
}
/**
* @return array{disk: string, path: string}|null
*/
private static function source(?string $target): ?array
{
$ref = self::parse($target);
if ($ref === null) {
return null;
}
if ($ref['type'] === 'version') {
$version = ContentElementVersion::query()->find($ref['id']);
$file = $version instanceof ContentElementVersion ? EmbeddedImages::file($version) : null;
return $file === null ? null : ['disk' => 'media_derived', 'path' => $file];
}
$element = ContentElement::query()->find($ref['id']);
if (! $element instanceof ContentElement) {
return null;
}
$public = (string) $element->website_public_path;
if ($element->website_released_at !== null && preg_match('#^website/'.$element->id.'/datei\.[a-z0-9]+$#', $public) === 1 && Storage::disk('media_public')->exists($public)) {
return ['disk' => 'media_public', 'path' => $public];
}
$preview = (string) $element->thumbnail_path;
if (str_starts_with($preview, 'vorschau/') && ! str_contains($preview, '..') && Storage::disk('media_derived')->exists($preview)) {
return ['disk' => 'media_derived', 'path' => $preview];
}
return null;
}
}
+37 -59
View File
@@ -2,10 +2,8 @@
namespace App\Filament\Pages;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Catalog\EmbeddedImages;
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;
@@ -46,11 +44,12 @@ class PdfVorlagen extends Page
public bool $showCover = true;
public ?int $headerId = null;
public ?int $footerId = null;
public ?int $coverId = null;
/**
* 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;
@@ -108,16 +107,14 @@ 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 !== '' || $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->headerId !== $template->header_version_id
|| $this->footerId !== $template->footer_version_id
|| $this->coverId !== $template->cover_version_id;
|| $this->bilder !== $this->templateSlots($template);
}
public function save(): void
@@ -130,9 +127,11 @@ public function save(): void
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();
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;
}
@@ -142,9 +141,9 @@ public function save(): void
'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,
...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),
];
@@ -202,12 +201,9 @@ public function openImagePicker(string $slot): void
public function clearImage(string $slot): void
{
match ($slot) {
'header' => $this->headerId = null,
'footer' => $this->footerId = null,
'cover' => $this->coverId = null,
default => null,
};
if (in_array($slot, PdfTemplate::SLOTS, true)) {
$this->bilder[$slot] = null;
}
}
public function pickerAllowsMany(): bool
@@ -229,11 +225,11 @@ 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) {
if ($target === null || $slot === null || ! in_array($slot, PdfTemplate::SLOTS, true)) {
return;
}
$problem = $this->imageProblem((int) $match[1]);
$problem = TemplateImage::problem((string) $target);
if ($problem !== null) {
Notification::make()->danger()->title('Dieses Bild lässt sich nicht verwenden.')->body($problem)->send();
@@ -241,12 +237,7 @@ public function confirmPicker(): void
return;
}
match ($slot) {
'header' => $this->headerId = (int) $match[1],
'footer' => $this->footerId = (int) $match[1],
'cover' => $this->coverId = (int) $match[1],
default => null,
};
$this->bilder[$slot] = (string) $target;
$this->closePicker();
}
@@ -284,16 +275,14 @@ public function pickerReplacing(): ?array
return null;
}
public function imageUrl(?int $versionId): ?string
public function imageUrl(?string $target): ?string
{
return $versionId === null ? null : EmbeddedImages::staffSource($versionId);
return TemplateImage::previewUrl($target);
}
public function imageTitle(?int $versionId): string
public function imageTitle(?string $target): string
{
$version = $versionId === null ? null : ContentElementVersion::query()->with('element')->find($versionId);
return $version?->element === null ? '' : (string) ($version->element->display_title ?: $version->element->working_title);
return TemplateImage::title($target);
}
public function slotLabel(string $slot): string
@@ -306,21 +295,6 @@ public function slotLabel(string $slot): string
};
}
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;
@@ -328,9 +302,15 @@ private function loadTemplate(PdfTemplate $template): void
$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;
$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
@@ -340,9 +320,7 @@ private function clearForm(): void
$this->publisher = '';
$this->copyright = '';
$this->showCover = true;
$this->headerId = null;
$this->footerId = null;
$this->coverId = null;
$this->bilder = ['header' => null, 'footer' => null, 'cover' => null];
}
protected function pickerExceptId(): int
+7 -2
View File
@@ -106,8 +106,13 @@ public function pickerResult(): array
return ['items' => [], 'more' => false];
}
if (in_array($this->pickerPurpose, ['aufbau', 'textbild', 'vorlagenbild'], true)) {
$image = in_array($this->pickerPurpose, ['textbild', 'vorlagenbild'], true);
// Eine PDF-Vorlage nimmt auch Bilder ohne Inhaltsfassung (Präsentationsmedien, Website-Bilder) auf.
if ($this->pickerPurpose === 'vorlagenbild') {
return $this->filterByMaterialart(app(EditorialCatalog::class)->presentationBrowse($this->pickerQuery, 'bild'));
}
if (in_array($this->pickerPurpose, ['aufbau', 'textbild'], true)) {
$image = $this->pickerPurpose === 'textbild';
return $this->filterByMaterialart(app(EditorialCatalog::class)->browse(
$this->pickerQuery,