Loheide-eu-mitarbeiterplatt.../app/Pagebuilder/Field.php
Friederich Loheide 694ccf6ae0 Mediathek: zentrale Bildverwaltung mit Verwendungsnachweis und Bildauswahl
- Tabellen media und media_usages, Modelle Media/MediaUsage
- App\Media\MediaLibrary (store/register/replace/delete/rebuild) und
  Registry App\Media\MediaUsages (Quellen: Seiten, Neuigkeiten inkl.
  künftiger content-Spalte, Termine, Startseiten-Kacheln), ImagePaths-Extraktor
- Alle Uploads landen in der Mediathek (Seiten-Editor direkt, übrige über
  Beobachter beim Speichern); Daten-Migration und media:rebuild übernehmen
  bestehende Bilder
- Recht media.manage, Zugang media.use für alle Redaktionsbereiche
- /redaktion/mediathek (Suche, Filter, Upload mit Fortschritt, Details,
  Eingebunden in, Ersetzen, Löschen mit Warnung, Mehrfachauswahl),
  Abschnitt in der Redaktion und Menüpunkt im Admin-Bereich
- MediaPicker (Hochladen/Mediathek) im Pagebuilder: Bildfelder und
  Galerie (Mehrfachauswahl), editor.pickMedia im provide-API
- Bild-URLs mit ?v=Änderungszeit, null bei fehlender Datei; Modelle löschen
  geteilte Bilder nicht mehr selbst

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 20:05:50 +00:00

210 lines
7.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Pagebuilder;
use App\Media\ImagePaths;
use App\Support\Icons;
use App\Support\RichText;
use Closure;
/**
* Ein Eingabefeld eines Blocks. Beschreibt zugleich das Formular im Editor
* (toArray) und die Bereinigung beim Speichern (sanitize) – Plugin-Blöcke
* bekommen so Editor und Absicherung ohne eigenen Code.
*/
final class Field
{
/**
* @param array<string, mixed> $options
*/
private function __construct(
public readonly string $name,
public readonly string $type,
public readonly string $label,
private readonly array $options = [],
) {}
public static function text(string $name, string $label, int $max = 200, string $default = '', ?string $placeholder = null): self
{
return new self($name, 'text', $label, ['max' => $max, 'default' => $default, 'placeholder' => $placeholder]);
}
public static function textarea(string $name, string $label, int $max = 2000, string $default = ''): self
{
return new self($name, 'textarea', $label, ['max' => $max, 'default' => $default]);
}
/** Formatierter Text – im Editor direkt im Block bearbeitet (TipTap). */
public static function html(string $name, string $label): self
{
return new self($name, 'html', $label, ['default' => '']);
}
/**
* @param array<string, string>|Closure(): array<string, string> $choices
*/
public static function select(string $name, string $label, array|Closure $choices, ?string $default = null): self
{
return new self($name, 'select', $label, ['choices' => $choices, 'default' => $default]);
}
public static function toggle(string $name, string $label, bool $default = false): self
{
return new self($name, 'toggle', $label, ['default' => $default]);
}
public static function number(string $name, string $label, int $min, int $max, int $default): self
{
return new self($name, 'number', $label, ['min' => $min, 'max' => $max, 'default' => $default]);
}
public static function url(string $name, string $label, string $default = ''): self
{
return new self($name, 'url', $label, ['default' => $default]);
}
/** Hochgeladenes Bild bzw. Bild aus der Mediathek (Pfad im Upload-Speicher). */
public static function image(string $name, string $label): self
{
return new self($name, 'image', $label, ['default' => null]);
}
/** Personen aus dem Verzeichnis (IDs). */
public static function people(string $name, string $label, int $max = 24): self
{
return new self($name, 'people', $label, ['max' => $max, 'default' => []]);
}
/** Einfache Tabelle: Zeilen aus Textzellen. */
public static function table(string $name, string $label): self
{
return new self($name, 'table', $label, ['default' => [['', ''], ['', '']]]);
}
/** Symbol aus dem Symbolsatz der Schnellzugriffe. */
public static function icon(string $name, string $label, string $default = 'link'): self
{
return new self($name, 'icon', $label, ['default' => $default, 'choices' => Icons::choices()]);
}
/** Liste gleichartiger Einträge, z. B. Schaltflächen oder Galeriebilder. */
public static function repeater(string $name, string $label, array $fields, int $max = 20, string $itemLabel = 'Eintrag'): self
{
return new self($name, 'repeater', $label, ['fields' => $fields, 'max' => $max, 'default' => [], 'itemLabel' => $itemLabel]);
}
/** Beliebige Liste von IDs, deren Einträge der Block selbst verwaltet (z. B. Dateien). */
public static function ids(string $name, string $label, string $kind, int $max = 50): self
{
return new self($name, 'ids', $label, ['kind' => $kind, 'max' => $max, 'default' => []]);
}
public function default(): mixed
{
if ($this->type === 'repeater') {
return [];
}
return $this->options['default'] ?? null;
}
public function help(string $help): self
{
return new self($this->name, $this->type, $this->label, [...$this->options, 'help' => $help]);
}
/**
* @return array<string, mixed> Beschreibung für das Editor-Formular
*/
public function toArray(): array
{
$options = $this->options;
if (isset($options['choices'])) {
$choices = $options['choices'] instanceof Closure ? ($options['choices'])() : $options['choices'];
$options['choices'] = collect($choices)->map(fn ($label, $value) => ['value' => (string) $value, 'label' => $label])->values()->all();
}
if (isset($options['fields'])) {
$options['fields'] = array_map(fn (Field $field) => $field->toArray(), $options['fields']);
}
return ['name' => $this->name, 'type' => $this->type, 'label' => $this->label, ...$options];
}
public function sanitize(mixed $value): mixed
{
return match ($this->type) {
'text', 'textarea' => mb_substr(trim(is_scalar($value) ? (string) $value : ''), 0, $this->options['max']),
'html' => app(RichText::class)->sanitize(is_string($value) ? mb_substr($value, 0, 200_000) : ''),
'select' => $this->sanitizeChoice($value),
'toggle' => (bool) $value,
'number' => max($this->options['min'], min($this->options['max'], is_numeric($value) ? (int) $value : $this->options['default'])),
'url' => self::safeUrl($value),
'image' => ImagePaths::isCandidate($value) ? $value : null,
'people', 'ids' => array_values(array_slice(array_unique(array_filter(array_map('intval', is_array($value) ? $value : []))), 0, $this->options['max'])),
'table' => $this->sanitizeTable($value),
'icon' => Icons::has($value) ? $value : $this->options['default'],
'repeater' => $this->sanitizeRepeater($value),
default => null,
};
}
public static function safeUrl(mixed $value): string
{
$url = trim(is_string($value) ? $value : '');
if ($url === '' || mb_strlen($url) > 2000) {
return '';
}
// Relative Adressen im Portal, Anker oder http(s)/mailto/tel – nichts anderes (kein javascript:).
if (preg_match('#^(/(?!/)|\#)#', $url) || preg_match('#^(https?://|mailto:|tel:)#i', $url)) {
return $url;
}
return preg_match('#^[a-z0-9.-]+\.[a-z]{2,}(/|$)#i', $url) ? 'https://'.$url : '';
}
private function sanitizeChoice(mixed $value): ?string
{
$choices = $this->options['choices'] instanceof Closure ? ($this->options['choices'])() : $this->options['choices'];
$value = is_scalar($value) ? (string) $value : null;
return $value !== null && array_key_exists($value, $choices) ? $value : $this->options['default'];
}
/**
* @return list<list<string>>
*/
private function sanitizeTable(mixed $value): array
{
$rows = array_slice(is_array($value) ? array_values($value) : [], 0, 100);
$width = min(10, max(1, ...array_map(fn ($row) => is_array($row) ? count($row) : 1, $rows ?: [[]])));
return array_map(function ($row) use ($width) {
$cells = array_slice(is_array($row) ? array_values($row) : [], 0, $width);
return array_pad(array_map(fn ($cell) => mb_substr(is_scalar($cell) ? (string) $cell : '', 0, 1000), $cells), $width, '');
}, $rows);
}
/**
* @return list<array<string, mixed>>
*/
private function sanitizeRepeater(mixed $value): array
{
$items = array_slice(is_array($value) ? array_values($value) : [], 0, $this->options['max']);
return array_map(function ($item) {
$item = is_array($item) ? $item : [];
$clean = [];
foreach ($this->options['fields'] as $field) {
$clean[$field->name] = $field->sanitize($item[$field->name] ?? $field->default());
}
return $clean;
}, $items);
}
}