Ein Foto gehoert zu keinem (ganzjaehrig) oder mehreren Erscheinungsbildern und erscheint nur dort, vor den ganzjaehrigen Fotos. Bausteine waehlen "automatisch" oder eine eigene Auswahl; Titelbereich und Abschluss zeigen das erste passende Foto ihrer Liste. Titelbild und "Bilder je Baustein" des Erscheinungsbilds werden in die Bausteine uebernommen und entfallen dort. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
5.5 KiB
PHP
121 lines
5.5 KiB
PHP
<?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 bzw. kein Banner.')
|
||
->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;
|
||
}
|
||
}
|