Files
fewo-stommer/app/Filament/Resources/Appearances/Schemas/AppearanceForm.php
T
LarsandClaude Opus 5.5 d2b1a853dc Banner zentral: je Website, Saison und Prioritaet, auch fuer Sonderangebote
Hinweise werden zu Bannern mit Website, Erscheinungsbildern, Prioritaet und
optionalem Sonderangebot (Text, Termin und Link aus dem Angebot, sichtbar nur
solange es beworben wird). Sonderangebote lassen sich mit einem Klick als Banner
zeigen; die Liste nennt das heute sichtbare Banner. Banner-Texte der
Erscheinungsbilder werden zu saisonalen Bannern.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 19:01:48 +01:00

121 lines
5.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\Filament\Resources\Appearances\Schemas;
use App\Models\Appearance;
use App\Models\Photo;
use App\Models\Site;
use App\Themes\ThemeRegistry;
use Closure;
use Filament\Forms\Components\ColorPicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Text;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;
class AppearanceForm
{
public static function configure(Schema $schema): Schema
{
return $schema
->columns(2)
->components([
Section::make('Zeitraum')
->description('Wiederholt sich jedes Jahr. Überschneiden sich Zeiträume, gilt die höhere Priorität (z. B. Silvester vor Winter). Ohne Zeitraum greift das Standard-Erscheinungsbild.')
->columns(2)
->schema([
TextInput::make('name')->label('Name')->placeholder('z. B. Weihnachten')->required()->maxLength(60),
Select::make('site_id')
->label('Website')
->options(fn (): array => Site::query()->pluck('name', 'id')->all())
->default(fn (): ?int => Site::query()->where('is_default', true)->value('id'))
->required(),
self::dayField('starts_md', 'Von (TT.MM.)'),
self::dayField('ends_md', 'Bis einschließlich (TT.MM.)'),
TextInput::make('priority')->label('Priorität')->integer()->minValue(0)->default(10),
Toggle::make('is_default')->label('Standard (wenn kein Zeitraum passt)')->inline(false),
Toggle::make('is_active')->label('Aktiv')->default(true)->inline(false),
]),
Section::make('Bilder')
->description('Welche Fotos in diesem Erscheinungsbild erscheinen, legen Sie beim Foto fest: Webseite › Fotos › „Zeigen in“. Ein Foto kann zu mehreren Erscheinungsbildern gehören; Fotos ohne Angabe erscheinen ganzjährig.')
->schema([
Text::make(fn (?Appearance $record): string => self::photoSummary($record)),
]),
Section::make('Texte')
->description('Leer lassen = Text der Seite. Banner pflegen Sie unter Webseite › Banner & Hinweise – dort auch nur für dieses Erscheinungsbild.')
->columns(2)
->collapsible()
->schema(collect(Appearance::OVERRIDABLE)
->map(fn (string $label, string $key) => TextInput::make('overrides.'.$key)->label($label)->maxLength(255))
->values()
->all()),
Section::make('Farben')
->description('Nur ändern, was anders aussehen soll; leere Felder übernehmen die Farben des Themes.')
->columns(3)
->collapsible()
->collapsed()
->schema(fn (Get $get): array => self::tokenFields($get('site_id'))),
]);
}
/**
* How many photos are made for this appearance.
*/
public static function photoSummary(?Appearance $appearance): string
{
if ($appearance === null) {
return 'Nach dem Speichern können Sie Fotos diesem Erscheinungsbild zuordnen.';
}
$count = Photo::query()->get(['id', 'appearance_ids'])->filter(fn (Photo $photo): bool => $photo->isMadeFor($appearance))->count();
return match ($count) {
0 => 'Noch keine Fotos zugeordnet – es erscheinen die ganzjährigen Fotos.',
1 => '1 Foto ist diesem Erscheinungsbild zugeordnet.',
default => "{$count} Fotos sind diesem Erscheinungsbild zugeordnet.",
};
}
/**
* @return list<ColorPicker>
*/
public static function tokenFields(int|string|null $siteId, string $statePath = 'tokens', ?string $themeKey = null): array
{
$theme = $themeKey ?: (Site::find($siteId)?->theme ?? config('themes.base'));
return collect(app(ThemeRegistry::class)->tokens($theme))
->map(fn (array $token, string $key): ColorPicker => ColorPicker::make("{$statePath}.{$key}")->label($token['label'])->placeholder($token['default']))
->values()
->all();
}
/**
* Day of the year entered as "TT.MM." and stored as "MM-DD".
*/
private static function dayField(string $name, string $label): TextInput
{
return TextInput::make($name)
->label($label)
->placeholder('24.12.')
->formatStateUsing(fn (?string $state): ?string => $state ? substr($state, 3, 2).'.'.substr($state, 0, 2).'.' : null)
->dehydrateStateUsing(fn (?string $state): ?string => self::toMonthDay($state))
->rule(fn (): Closure => function (string $attribute, mixed $value, Closure $fail): void {
if (filled($value) && self::toMonthDay((string) $value) === null) {
$fail('Bitte als TT.MM. eingeben, z. B. 24.12.');
}
});
}
private static function toMonthDay(?string $value): ?string
{
if (blank($value) || ! preg_match('/^\s*(\d{1,2})\.(\d{1,2})\.?\s*$/', $value, $match)) {
return null;
}
return checkdate((int) $match[2], (int) $match[1], 2024) ? sprintf('%02d-%02d', $match[2], $match[1]) : null;
}
}