Loheide-eu-mitarbeiterplatt.../app/Filament/Pages/Appearance/AppearancePage.php
Friederich Loheide f1202bea08
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Sicherheitsupdate nach Audit (0.29.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 11:34:54 +00:00

175 lines
8.1 KiB
PHP
Raw Permalink 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\Appearance;
use App\Filament\Clusters\AppearanceCluster;
use App\Filament\Components\SafeUpload;
use App\Support\Audit;
use App\Support\Branding;
use App\Support\Theme\Contrast;
use App\Support\Theme\Icons;
use App\Support\Theme\Theme;
use App\Support\Theme\ThemeCompiler;
use App\Support\Theme\ThemeTransfer;
use Filament\Actions\Action;
use Filament\Actions\ActionGroup;
use Filament\Forms\Components\Field;
use Filament\Forms\Components\FileUpload;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Component;
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;
/**
* „Erscheinungsbild“ (docs/PLAN.md, Abschnitt 12): Entwurf bearbeiten, in der
* Vorschau ansehen, dann für alle veröffentlichen. Jede Unterseite zeigt nur
* ihre Felder; gespeichert wird immer in denselben Entwurf (saveDraft führt zusammen).
*/
abstract class AppearancePage extends Page
{
protected static ?string $cluster = AppearanceCluster::class;
/** Spalten des Formulars (einspaltig für längere Texte) */
protected static int $formColumns = 2;
/**
* Felder dieser Unterseite.
*
* @return array<int, Component|Field>
*/
abstract protected function fields(): array;
protected static function image(string $name, string $label, string $help): FileUpload
{
return SafeUpload::apply(FileUpload::make($name), [...SafeUpload::IMAGES, 'image/svg+xml'])->label($label)->helperText($help)
->disk('uploads')->directory('theme')->visibility('public')->maxSize(4096);
}
protected string $view = 'filament.pages.appearance';
/** @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
{
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) => '<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'),
];
}
}