Files
laraundfelix/app/Domain/Catalog/PdfTemplate.php
T
LarsandClaude Sonnet 5.5 9f9370dbf8
Deploy Test / test (push) Successful in 3m8s
Deploy Test / deploy (push) Successful in 26s
Erlaube in Kopf und Fuß einer PDF-Vorlage ein Logo links oder rechts
Kopf und Fuß haben je eine Anordnung: Band in voller Seitenbreite (Vorgabe, wie
bisher), Logo links oder Logo rechts am Textrand. Die Logohöhe ist einstellbar
(16 bis 100 Punkt, Vorgabe 40); das Logo ist höchstens halb so breit wie der Text,
der Text beginnt darunter beziehungsweise endet darüber. Die Titelseite bleibt
unberührt. Additive Migration mit Vorgaben; der vorherige Programmstand zeigt jede
Grafik weiter als Band.

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

121 lines
3.6 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',
'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;
/**
* 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);
}
}