Files
laraundfelix/app/Domain/Cms/Widgets/Support/PremiumFields.php
T
LarsandClaude Sonnet 5.5 6172364bf8 Biete in allen Widget-Linkfeldern einen Dialog für interne Seiten und Sprungmarken an
Zweistufig (Seite, dann Sprungmarke) mit Veröffentlichungszustand und den noch
ungespeicherten Marken der bearbeiteten Seite. Schreibt nur die Adresse ins Feld.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 07:00:57 +01:00

194 lines
7.2 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\Domain\Cms\Widgets\Support;
use App\Domain\Cms\Sections\SectionStyle;
use App\Domain\Cms\Widgets\WidgetErrors;
use Filament\Forms\Components\Slider;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Grid;
/**
* Gemeinsame Bausteine der Premium-Widgets (Einstieg, Bild und Text; Brand-UI-Kit v1.6): Label, Überschriftenebene,
* Bildfokus in Prozent und zwei Schaltflächen. Alle Werte bleiben in data bzw. settings der bestehenden Hülle.
*
* Abweichungen vom Kit (bewusst, dokumentiert in cms-widget-registry.md):
* - Bildfokus als data.focus_x / data.focus_y (ganze Zahlen 0–100), weil settings nur Aufzählungswerte trägt.
* - Überschriftenebene „Automatisch“ statt freier H1: Die Seite hat genau eine H1 (erstes Widget der Seite).
* - Linkziele über SafeUrl des Repositorys (Seitenpfad, #Anker, http und https), nicht nur https.
*/
final class PremiumFields
{
public const HEADING_LEVELS = [
'auto' => 'Automatisch (Seiteneinstieg H1, sonst H2)',
'h2' => 'H2',
'h3' => 'H3',
];
/**
* @return array<string, mixed>
*/
public static function rules(): array
{
return [
'eyebrow' => ['nullable', 'string', 'max:80'],
'cta_label' => ['nullable', 'string', 'max:80', 'required_with:data.cta_url'],
'cta_url' => ['nullable', 'string', 'max:500', 'required_with:data.cta_label'],
'cta2_label' => ['nullable', 'string', 'max:80', 'required_with:data.cta2_url'],
'cta2_url' => ['nullable', 'string', 'max:500', 'required_with:data.cta2_label'],
'alt' => ['nullable', 'string', 'max:300'],
'focus_x' => ['nullable', 'numeric', 'between:0,100'],
'focus_y' => ['nullable', 'numeric', 'between:0,100'],
];
}
public static function eyebrowField(): TextInput
{
return TextInput::make('eyebrow')
->label('Label über der Überschrift (optional)')
->maxLength(80)
->live(onBlur: true)
->helperText('Kurzer Hinweis, zum Beispiel ein Themenbereich. Kein Pflichtfeld.');
}
public static function ctaFields(): Grid
{
$url = fn (string $name, string $label, string $pair) => LinkField::make($name, $label)
->requiredWith($pair)
->live(onBlur: true)
->placeholder('/programme');
return Grid::make(['default' => 1, 'md' => 2])->schema([
TextInput::make('cta_label')->label('Hauptschaltfläche: Beschriftung')->maxLength(80)->live(onBlur: true),
$url('cta_url', 'Hauptschaltfläche: Linkziel', 'cta_label'),
TextInput::make('cta2_label')->label('Zweite Schaltfläche: Beschriftung')->maxLength(80)->live(onBlur: true),
$url('cta2_url', 'Zweite Schaltfläche: Linkziel', 'cta2_label'),
]);
}
/**
* @return list<Slider>
*/
public static function focusFields(int $defaultX = 50, int $defaultY = 50): array
{
// Ohne gespeicherten Wert zeigt der Regler die Vorgabe der Widget-Art, die auch die Darstellung verwendet.
$slider = fn (string $name, string $label, int $default) => Slider::make($name)
->label($label)
->range(0, 100)
->step(1)
->required(false)
->tooltips()
->default($default)
->afterStateHydrated(function (Slider $component, mixed $state) use ($default): void {
if (! is_numeric($state)) {
$component->state($default);
}
})
->live();
return [
$slider('focus_x', 'Bildausschnitt waagerecht (0 = links, 100 = rechts)', $defaultX),
$slider('focus_y', 'Bildausschnitt senkrecht (0 = oben, 100 = unten)', $defaultY),
];
}
public static function altField(): TextInput
{
return TextInput::make('alt')
->label('Alternativtext (optional)')
->maxLength(300)
->live(onBlur: true)
->helperText('Leer lassen, dann gilt der Alternativtext des Mediums aus dem Medienmanager. Nur ausfüllen, wenn das Bild hier etwas anderes aussagen soll.');
}
/**
* Bereinigt Label, Schaltflächen, Alternativtext und Fokus; meldet unsichere Linkziele.
*
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
public static function clean(array $data, WidgetErrors $errors, bool $hasImage): array
{
foreach (['eyebrow', 'cta_label', 'cta_url', 'cta2_label', 'cta2_url', 'alt'] as $key) {
$data[$key] = filled($data[$key] ?? null) ? trim((string) $data[$key]) : null;
}
foreach (['cta_url', 'cta2_url'] as $key) {
if ($data[$key] !== null && ! SafeUrl::isSafe($data[$key])) {
$errors->add("data.$key", SafeUrl::message());
}
}
foreach (['focus_x', 'focus_y'] as $key) {
$value = $data[$key] ?? null;
$data[$key] = is_numeric($value) ? (int) round(max(0, min(100, (float) $value))) : null;
}
return $data;
}
/**
* Schaltflächen mit sicherem Linkziel in Anzeigereihenfolge.
*
* @param array<string, mixed> $data
* @return list<array{label: string, url: string}>
*/
public static function actions(array $data): array
{
$actions = [];
foreach ([['cta_label', 'cta_url'], ['cta2_label', 'cta2_url']] as [$label, $url]) {
if (filled($data[$label] ?? null) && SafeUrl::isSafe($data[$url] ?? null)) {
$actions[] = ['label' => (string) $data[$label], 'url' => (string) $data[$url]];
}
}
return $actions;
}
/**
* Bildfokus in Prozent; ohne Angabe die Vorgabe der Widget-Art.
*
* @param array<string, mixed> $data
* @return array{x: int, y: int}
*/
public static function focus(array $data, int $defaultX = 50, int $defaultY = 50): array
{
$clamp = fn (mixed $value, int $default): int => is_numeric($value) ? (int) round(max(0, min(100, (float) $value))) : $default;
return ['x' => $clamp($data['focus_x'] ?? null, $defaultX), 'y' => $clamp($data['focus_y'] ?? null, $defaultY)];
}
/**
* Übersetzt den früheren Fokuspunkt (Aufzählung wie beim Abschnittshintergrund) in Prozentwerte.
*
* @return array{focus_x: int|null, focus_y: int|null}
*/
public static function focusFromEnum(mixed $focus): array
{
if (! is_string($focus) || ! isset(SectionStyle::FOCUS_POSITIONS[$focus])) {
return ['focus_x' => null, 'focus_y' => null];
}
[$x, $y] = array_map(fn (string $part): int => (int) rtrim($part, '%'), explode(' ', SectionStyle::FOCUS_POSITIONS[$focus]));
return ['focus_x' => $x, 'focus_y' => $y];
}
/**
* Überschriftenebene: „auto“ ergibt H1 für das erste Widget der Seite, sonst H2. Eine weitere H1 gibt es nicht.
*
* @param array<string, mixed> $settings
*/
public static function headingTag(array $settings, bool $first): string
{
$level = $settings['heading_level'] ?? 'auto';
return match (true) {
$first => 'h1',
$level === 'h3' => 'h3',
default => 'h2',
};
}
}