Files
laraundfelix/app/Domain/Cms/Widgets/Support/PremiumFields.php
T
LarsandClaude Sonnet 5.5 9353e2e38a Biete Bildausschnitt und Zoom für Bilder und Videos in Widgets an (ADR-70)
Redaktionelle Karten erhalten je Karte Fokus und Zoom, Einstieg, Bild und Text sowie
Premium-Karten zusätzlich einen Zoom (100 bis 250 Prozent, darunter entstünden leere
Ränder). Die Darstellung ist reines CSS um den Fokuspunkt; Video und Clip folgen dem
Ausschnitt. Die Regler zeigen beim Laden die Vorgabe statt 0 (FramingSlider).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-12-01 12:15:38 +01:00

182 lines
6.6 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'],
] + ImageFraming::rules();
}
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'),
]);
}
/**
* Fokus waagerecht und senkrecht sowie Zoom (ImageFraming).
*
* @return list<Slider>
*/
public static function focusFields(int $defaultX = 50, int $defaultY = 50): array
{
return ImageFraming::sliders($defaultX, $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 ImageFraming::cleanZoom($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)];
}
/** Zoom in Prozent (100 bis 250); ohne Angabe 100. */
public static function zoom(array $data): int
{
return ImageFraming::zoom($data['zoom'] ?? null);
}
/**
* Ü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',
};
}
}