*/ abstract protected function fields(): array; protected static function image(string $name, string $label, string $help): FileUpload { return FileUpload::make($name)->label($label)->helperText($help) ->disk('uploads')->directory('theme')->visibility('public')->maxSize(4096); } protected string $view = 'filament.pages.appearance'; /** @var array|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 { return $schema->statePath('data')->columns(static::$formColumns)->components($this->fields()); } protected 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) => '

'.$icons[$note['level']].''.e($note['text']).'

') ->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'), ]; } }