Loheide-eu-mitarbeiterplatt.../app/Filament/Pages/Appearance.php
Friederich Loheide 82bbeef31b
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m7s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m11s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m16s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m18s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m49s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m46s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m41s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m33s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m0s
Phase 5: Erscheinungsbild mit Entwurf, Vorschau und Veröffentlichen
- Theme in den Einstellungen (Entwurf/veröffentlicht), Übernahme der
  Einstellungen aus Phase 0; statische CSS-Datei mit Hash beim Veröffentlichen
- Logos hell/dunkel, Signet, App-Symbol → Favicon, Homescreen-Symbole,
  Web-App-Manifest; SVG-Bereinigung
- Markenfarbe exakt, zweite Akzentfarbe, Kopfleiste hell/Marke/dunkel,
  Kontrastprüfung (WCAG AA) mit automatisch lesbarer Linkfarbe
- Lokale Schriften (Latein), Überschriften-Schrift, eigene WOFF2-Schrift,
  Ecken, Dichte, Dunkelmodus-Regel
- Anmeldeseite (geteilt/zentriert, Bild, Texte), Fußzeile, E-Mails im Design
- Admin: Live-Vorschau (Startseite, Anmeldung, Beispielseite; hell/dunkel),
  Veröffentlichen, Verwerfen, Export/Import, Zurücksetzen, eigenes CSS
- Admin-Bereich übernimmt Logo, Farbe und Favicon; Whitelabel

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

244 lines
14 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\Pages;
use App\Support\Audit;
use App\Support\Branding;
use App\Support\Theme\Contrast;
use App\Support\Theme\Fonts;
use App\Support\Theme\Icons;
use App\Support\Theme\Theme;
use App\Support\Theme\ThemeCompiler;
use App\Support\Theme\ThemeTransfer;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Actions\ActionGroup;
use Filament\Forms\Components\ColorPicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Tabs\Tab;
use Filament\Schemas\Components\Text;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\HtmlString;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Throwable;
use UnitEnum;
/**
* „Erscheinungsbild“ (docs/PLAN.md, Abschnitt 12): Entwurf bearbeiten, in der
* Vorschau ansehen, dann für alle veröffentlichen.
*/
class Appearance extends Page
{
protected string $view = 'filament.pages.appearance';
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedPaintBrush;
protected static string|UnitEnum|null $navigationGroup = 'Portal';
protected static ?string $navigationLabel = 'Erscheinungsbild';
protected static ?string $title = 'Erscheinungsbild';
protected static ?string $slug = 'erscheinungsbild';
/** @var array<string, mixed>|null */
public ?array $data = [];
public static function canAccess(): bool
{
return auth()->user()?->can('appearance.manage') ?? false;
}
public function mount(Theme $theme): void
{
$this->form->fill($theme->working());
}
public function hasDraft(): bool
{
return app(Theme::class)->hasDraft();
}
public function form(Schema $schema): Schema
{
$image = fn (string $name, string $label, string $help) => FileUpload::make($name)->label($label)->helperText($help)
->disk('uploads')->directory('theme')->visibility('public')->maxSize(4096);
return $schema->statePath('data')->components([
Tabs::make()->persistTabInQueryString()->tabs([
Tab::make('Identität')->icon(Heroicon::OutlinedIdentification)->columns(2)->schema([
TextInput::make('portal_name')->label('Name des Portals')->required()->maxLength(60)
->helperText('Erscheint im Kopfbereich, im Browser-Tab, in E-Mails und auf dem Homescreen.')->columnSpanFull(),
$image('logo_light', 'Logo für helle Flächen', 'SVG, PNG oder WebP mit transparentem Hintergrund.')
->acceptedFileTypes(['image/svg+xml', 'image/png', 'image/webp']),
$image('logo_dark', 'Logo für dunkle Flächen', 'Für den Dunkelmodus und farbige Kopfleisten, z. B. in Weiß. Leer = wie hell.')
->acceptedFileTypes(['image/svg+xml', 'image/png', 'image/webp']),
$image('logo_mark', 'Signet (quadratisch)', 'Kurzform für Smartphones, z. B. nur das Bildzeichen.')
->acceptedFileTypes(['image/svg+xml', 'image/png', 'image/webp']),
$image('app_icon', 'App-Symbol', 'Quadratisch, mindestens 512 × 512 Pixel (PNG oder JPG). Daraus entstehen Favicon und Homescreen-Symbole.')
->image()->acceptedFileTypes(['image/png', 'image/jpeg', 'image/webp']),
]),
Tab::make('Farben')->icon(Heroicon::OutlinedSwatch)->columns(2)->schema([
ColorPicker::make('primary_color')->label('Markenfarbe')->required()->regex('/^#[0-9a-fA-F]{6}$/')->live(onBlur: true)
->helperText('Wird exakt übernommen: Schaltflächen, aktive Navigation, Begrüßungskarte. Die Abstufungen werden berechnet.'),
ColorPicker::make('secondary_color')->label('Zweite Akzentfarbe (optional)')->regex('/^#[0-9a-fA-F]{6}$/')->live(onBlur: true)
->helperText('Für Kategorien und Datumsangaben. Leer = Markenfarbe.'),
Radio::make('header_style')->label('Kopfleiste')->options(Theme::HEADER_STYLES)->inline()->required()->columnSpanFull(),
Section::make('Kontrastprüfung (WCAG AA)')->columnSpanFull()->compact()->schema([
Text::make(fn (Get $get) => $this->contrastReport((string) $get('primary_color'), $get('secondary_color'))),
]),
]),
Tab::make('Schrift & Form')->icon(Heroicon::OutlinedLanguage)->columns(2)->schema([
Select::make('font_body')->label('Schrift')->options(fn (Get $get) => Fonts::options((bool) $get('custom_font')))->required()->native(false),
Select::make('font_heading')->label('Schrift für Überschriften')->options(fn (Get $get) => ['same' => 'Wie die Schrift'] + Fonts::options((bool) $get('custom_font')))->required()->native(false),
FileUpload::make('custom_font')->label('Eigene Schrift (WOFF2)')
->helperText('Z. B. die Hausschrift aus dem Corporate Design. Wird lokal ausgeliefert – keine Verbindung zu Google oder anderen Diensten. Bitte die Lizenz für die Nutzung im Web beachten.')
->disk('uploads')->directory('theme/fonts')->visibility('public')->maxSize(2048)
->acceptedFileTypes(['font/woff2', 'application/font-woff2', 'application/octet-stream'])
->rule('extensions:woff2')->live()->columnSpanFull(),
Radio::make('radius')->label('Ecken')->options(collect(Theme::RADII)->map(fn ($r) => $r[0])->all())->inline()->required(),
Radio::make('density')->label('Abstände')->options(collect(Theme::DENSITIES)->map(fn ($d) => $d[0])->all())->inline()->required(),
Radio::make('dark_mode')->label('Dunkelmodus')->options(Theme::DARK_MODES)->required()->columnSpanFull(),
]),
Tab::make('Anmeldeseite')->icon(Heroicon::OutlinedArrowRightOnRectangle)->columns(2)->schema([
Radio::make('login_layout')->label('Aufteilung')->options(Theme::LOGIN_LAYOUTS)->required()->columnSpanFull(),
TextInput::make('login_title')->label('Begrüßung')->maxLength(200)->columnSpanFull(),
Textarea::make('login_text')->label('Text darunter')->rows(2)->maxLength(500)->columnSpanFull(),
$image('login_image', 'Hintergrundbild', 'Querformat, mindestens 1600 Pixel breit. Leer = Fläche in der Markenfarbe.')
->image()->acceptedFileTypes(['image/jpeg', 'image/png', 'image/webp'])->maxSize(8192)->columnSpanFull(),
]),
Tab::make('Fußzeile & E-Mails')->icon(Heroicon::OutlinedEnvelope)->schema([
TextInput::make('footer_text')->label('Text in der Fußzeile')->placeholder('© 2027 Musterstift gGmbH')->maxLength(200),
Repeater::make('footer_links')->label('Links in der Fußzeile')->columns(2)->defaultItems(0)->reorderable()
->addActionLabel('Link hinzufügen')->maxItems(8)
->schema([
TextInput::make('label')->label('Bezeichnung')->required()->maxLength(60)->placeholder('Impressum'),
TextInput::make('url')->label('Adresse')->required()->maxLength(500)->placeholder('https://… oder /seiten/impressum'),
]),
Textarea::make('email_footer')->label('Fußzeile in E-Mails')->rows(3)->maxLength(500)
->helperText('Z. B. Anschrift und Hinweis. Logo und Markenfarbe übernehmen die E-Mails automatisch.'),
]),
Tab::make('Erweitert')->icon(Heroicon::OutlinedCodeBracket)->schema([
Textarea::make('custom_css')->label('Eigenes CSS')->rows(12)->maxLength(50000)
->extraInputAttributes(['class' => 'font-mono text-xs', 'spellcheck' => 'false'])
->helperText('Für Fortgeschrittene. Wird nach dem Portal-CSS geladen und bei Aktualisierungen unverändert übernommen. Klassennamen können sich mit neuen Versionen ändern – bitte nach jedem Update prüfen. Verfügbare Variablen: --brand, --brand-50 … --brand-950, --radius, --font-body, --font-heading.'),
]),
]),
]);
}
private function contrastReport(string $primary, ?string $secondary): HtmlString
{
if (! preg_match('/^#[0-9a-fA-F]{6}$/', $primary)) {
return new HtmlString('');
}
$colors = ['ok' => '#15803d', 'info' => '#0369a1', 'warning' => '#b45309'];
$icons = ['ok' => '✓', 'info' => 'i', 'warning' => '!'];
return new HtmlString(collect(Contrast::report(strtoupper($primary), $secondary && preg_match('/^#[0-9a-fA-F]{6}$/', $secondary) ? strtoupper($secondary) : null))
->map(fn ($note) => '<p style="display:flex;gap:.5rem;margin:.25rem 0"><span style="font-weight:700;color:'.$colors[$note['level']].';width:1rem;text-align:center">'.$icons[$note['level']].'</span><span>'.e($note['text']).'</span></p>')
->implode(''));
}
/**
* Entwurf speichern und die Vorschau aktualisieren.
*/
public function save(Theme $theme, Branding $branding): void
{
$theme->saveDraft($this->form->getState());
session()->put(Branding::PREVIEW_SESSION_KEY, true);
$branding->refresh();
$this->dispatch('theme-preview-reload');
Notification::make()->success()->title('Entwurf gespeichert')->body('Die Vorschau zeigt jetzt den Entwurf. Für alle sichtbar wird er erst mit „Veröffentlichen“.')->send();
}
protected function getHeaderActions(): array
{
return [
Action::make('publish')->label('Veröffentlichen')->icon(Heroicon::OutlinedRocketLaunch)
->requiresConfirmation()
->modalHeading('Erscheinungsbild veröffentlichen?')
->modalDescription('Der gespeicherte Entwurf wird für alle sichtbar. Ungespeicherte Änderungen im Formular werden vorher als Entwurf gespeichert.')
->action(function (Theme $theme, ThemeCompiler $compiler, Icons $icons, Branding $branding) {
$theme->saveDraft($this->form->getState());
$theme->publish($compiler, $icons);
session()->forget(Branding::PREVIEW_SESSION_KEY);
$branding->refresh();
Audit::log('appearance.published');
$this->form->fill($theme->working());
$this->dispatch('theme-preview-reload');
Notification::make()->success()->title('Erscheinungsbild veröffentlicht')->send();
}),
Action::make('discard')->label('Entwurf verwerfen')->icon(Heroicon::OutlinedArrowUturnLeft)->color('gray')
->visible(fn () => $this->hasDraft())
->requiresConfirmation()
->action(function (Theme $theme, Branding $branding) {
$theme->discardDraft();
session()->forget(Branding::PREVIEW_SESSION_KEY);
$branding->refresh();
$this->form->fill($theme->working());
$this->dispatch('theme-preview-reload');
}),
ActionGroup::make([
Action::make('export')->label('Exportieren')->icon(Heroicon::OutlinedArrowDownTray)
->action(fn (ThemeTransfer $transfer): StreamedResponse => response()->streamDownload(
fn () => print (json_encode($transfer->export(app(Theme::class)->working()), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)),
'erscheinungsbild-'.now()->format('Y-m-d').'.json',
['Content-Type' => 'application/json'],
)),
Action::make('import')->label('Importieren')->icon(Heroicon::OutlinedArrowUpTray)
->modalDescription('Erscheinungsbild aus einer exportierten Datei übernehmen – als Entwurf, damit Sie es erst ansehen können.')
->schema([
FileUpload::make('file')->label('Datei (.json)')->disk('local')->directory('theme-import')
->acceptedFileTypes(['application/json', 'text/plain'])->maxSize(20 * 1024)->required(),
])
->action(function (array $data, ThemeTransfer $transfer, Theme $theme) {
try {
$values = $transfer->import((string) Storage::disk('local')->get($data['file']));
} catch (Throwable $e) {
Notification::make()->danger()->title('Import nicht möglich')->body($e->getMessage())->send();
return;
} finally {
Storage::disk('local')->delete($data['file']);
}
$theme->saveDraft($values);
session()->put(Branding::PREVIEW_SESSION_KEY, true);
$this->form->fill($theme->working());
$this->dispatch('theme-preview-reload');
Notification::make()->success()->title('Als Entwurf importiert')->send();
}),
Action::make('reset')->label('Auf Standard zurücksetzen')->icon(Heroicon::OutlinedArrowPath)->color('danger')
->requiresConfirmation()
->modalDescription('Setzt Farben, Schriften, Logos und Texte auf die Standardwerte zurück – als Entwurf. Der Portalname bleibt.')
->action(function (Theme $theme) {
$theme->resetToDefaults();
session()->put(Branding::PREVIEW_SESSION_KEY, true);
$this->form->fill($theme->working());
$this->dispatch('theme-preview-reload');
}),
])->label('Mehr')->icon(Heroicon::OutlinedEllipsisVertical)->button()->color('gray'),
];
}
}