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>
154 lines
4.7 KiB
PHP
154 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Catalog;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
/**
|
|
* Vorlage für das PDF eines Dokuments (ADR-51, Nachtrag): Titelseite, grafische Kopf- und Fußzeile und Standardvermerke.
|
|
* Die Vorlage bestimmt nur das Aussehen. Der Inhalt, auch Ausgabe, Datum und Copyright-Vermerk der Dokumentfassung, gehört
|
|
* zur Fassung ({@see DocumentMeta}); die Vorlage liefert nur den Standard-Vermerk, wenn die Fassung keinen eigenen hat.
|
|
*/
|
|
class PdfTemplate extends Model
|
|
{
|
|
public const KIND_DOCUMENT = 'dokument';
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'kind',
|
|
'show_cover',
|
|
'header_version_id',
|
|
'footer_version_id',
|
|
'cover_version_id',
|
|
'header_element_id',
|
|
'footer_element_id',
|
|
'cover_element_id',
|
|
'header_align',
|
|
'footer_align',
|
|
'header_logo_height',
|
|
'footer_logo_height',
|
|
'toc_mode',
|
|
'toc_depth',
|
|
'publisher',
|
|
'copyright',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return ['show_cover' => 'boolean'];
|
|
}
|
|
|
|
public const SLOTS = ['header', 'footer', 'cover'];
|
|
|
|
/** Anordnung von Kopf- und Fußgrafik: Band in voller Seitenbreite oder Logo links oder rechts. */
|
|
public const ALIGNMENTS = ['full', 'left', 'right'];
|
|
|
|
public const LOGO_MIN = 16;
|
|
|
|
public const LOGO_MAX = 100;
|
|
|
|
public const LOGO_DEFAULT = 40;
|
|
|
|
/** Inhaltsverzeichnis: nur bei mehr als einem Eintrag, immer oder nie. */
|
|
public const TOC_MODES = ['auto', 'always', 'never'];
|
|
|
|
/** Tiefe des Verzeichnisses: 2 = nur Kapitel und Unterkapitel, 3 bis 6 = zusätzlich die Überschriften bis zu dieser Stufe. */
|
|
public const TOC_DEPTH_MIN = 2;
|
|
|
|
public const TOC_DEPTH_MAX = 6;
|
|
|
|
/**
|
|
* Darstellung des gedruckten Inhaltsverzeichnisses.
|
|
*
|
|
* @return array{mode: string, depth: int}
|
|
*/
|
|
public function toc(): array
|
|
{
|
|
return self::normalizeToc((string) $this->getAttribute('toc_mode'), (int) $this->getAttribute('toc_depth'));
|
|
}
|
|
|
|
/**
|
|
* @return array{mode: string, depth: int}
|
|
*/
|
|
public static function normalizeToc(?string $mode, int|string|null $depth): array
|
|
{
|
|
$depth = (int) $depth;
|
|
|
|
return [
|
|
'mode' => in_array($mode, self::TOC_MODES, true) ? (string) $mode : 'auto',
|
|
'depth' => $depth < 1 ? self::TOC_DEPTH_MAX : max(self::TOC_DEPTH_MIN, min(self::TOC_DEPTH_MAX, $depth)),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Anordnung und Logohöhe (Punkt) von Kopf oder Fuß; ungültige Werte fallen auf die Vorgabe zurück.
|
|
*
|
|
* @return array{align: string, height: int}
|
|
*/
|
|
public function layout(string $slot): array
|
|
{
|
|
return self::normalizeLayout((string) $this->getAttribute($slot.'_align'), (int) $this->getAttribute($slot.'_logo_height'));
|
|
}
|
|
|
|
/**
|
|
* @return array{align: string, height: int}
|
|
*/
|
|
public static function normalizeLayout(?string $align, int|string|null $height): array
|
|
{
|
|
$height = (int) $height;
|
|
|
|
return [
|
|
'align' => in_array($align, self::ALIGNMENTS, true) ? (string) $align : 'full',
|
|
'height' => $height < 1 ? self::LOGO_DEFAULT : max(self::LOGO_MIN, min(self::LOGO_MAX, $height)),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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');
|
|
}
|
|
|
|
public function footerVersion(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ContentElementVersion::class, 'footer_version_id');
|
|
}
|
|
|
|
public function coverVersion(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ContentElementVersion::class, 'cover_version_id');
|
|
}
|
|
|
|
public function compositions(): HasMany
|
|
{
|
|
return $this->hasMany(Composition::class);
|
|
}
|
|
}
|