Loheide-eu-mitarbeiterplatt.../app/Filament/Pages/DashboardSettings.php
Friederich Loheide 3a5796aaf8
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m7s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m13s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m18s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m27s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 4m12s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 4m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 3m40s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 3m41s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Startseite: acht Bereichs-Beiträge, Schnellzugriffe wahlweise in der Seitenleiste
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 17:44:32 +00:00

142 lines
5.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;
use App\Dashboard\Widgets;
use App\Filament\Resources\DashboardTiles\DashboardTileResource;
use App\Support\Settings;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use UnitEnum;
/**
* Anordnung der Startseite: Kacheln je Bereich sortieren und ausblenden –
* mitgelieferte wie eigene.
*/
class DashboardSettings extends Page
{
protected string $view = 'filament.pages.settings-form';
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedHome;
protected static string|UnitEnum|null $navigationGroup = 'Portal';
protected static ?int $navigationSort = 10;
protected static ?string $navigationLabel = 'Startseite';
protected static ?string $title = 'Startseite';
protected static ?string $slug = 'startseite';
private const AREAS = [
'top' => ['Ganz oben (volle Breite)', 'Hinweise und Banner über allem.'],
'hero' => ['Groß links oben', 'Nur die erste sichtbare Kachel wird gezeigt.'],
'feed' => ['Neben dem großen Bereich', ''],
'main' => ['Hauptspalte', ''],
'aside' => ['Seitenleiste (rechts)', ''],
];
/** @var array<string, mixed>|null */
public ?array $data = [];
public static function canAccess(): bool
{
return auth()->user()?->can('appearance.manage') ?? false;
}
public function mount(Widgets $widgets): void
{
$this->form->fill(collect(self::AREAS)->mapWithKeys(fn ($info, $area) => [$area => $widgets->forAdmin($area)])->all());
}
/**
* Erlaubte Bereiche einer Kachel (mehr als einer = verschiebbar).
*
* @return list<string>
*/
private static function areasOf(?string $key): array
{
foreach (array_keys(self::AREAS) as $area) {
foreach (app(Widgets::class)->forAdmin($area) as $widget) {
if ($widget['key'] === $key) {
return $widget['areas'];
}
}
}
return [];
}
public function form(Schema $schema): Schema
{
return $schema->statePath('data')->components(
collect(self::AREAS)->map(fn ($info, $area) => Section::make($info[0])
->description($info[1] ?: null)
->visible(fn () => app(Widgets::class)->forAdmin($area) !== [])
->schema([
Repeater::make($area)
->hiddenLabel()
->reorderable()
->reorderableWithDragAndDrop()
->addable(false)
->deletable(false)
->itemLabel(fn (array $state) => $state['label'] ?? $state['key'])
->columns(2)
->schema([
Hidden::make('key'),
Hidden::make('label'),
Toggle::make('visible')->label('Anzeigen')->inline(false),
Select::make('area')
->label('Platzierung')
->options(fn (Get $get) => collect(self::areasOf($get('key')))->mapWithKeys(fn ($a) => [$a => self::AREAS[$a][0]])->all())
->selectablePlaceholder(false)
->visible(fn (Get $get) => count(self::areasOf($get('key'))) > 1)
->helperText('Wird nach dem Speichern in den gewählten Bereich verschoben.'),
]),
]))->values()->all()
);
}
public function save(Settings $settings): void
{
$layout = [];
foreach (array_keys(self::AREAS) as $area) {
foreach (array_values($this->data[$area] ?? []) as $index => $item) {
$layout[$item['key']] = ['sort' => $index * 10, 'visible' => (bool) ($item['visible'] ?? true)];
if (in_array($item['area'] ?? null, self::areasOf($item['key']), true)) {
$layout[$item['key']]['area'] = $item['area'];
}
}
}
$settings->set('dashboard.layout', $layout);
// Verschobene Kacheln im Formular in ihren neuen Bereich umsortieren
$this->mount(app(Widgets::class));
Notification::make()->success()->title('Startseite gespeichert')->send();
}
protected function getHeaderActions(): array
{
return [
Action::make('tiles')->label('Eigene Kacheln')->icon(Heroicon::OutlinedRectangleGroup)->color('gray')
->url(fn () => DashboardTileResource::getUrl()),
Action::make('preview')->label('Startseite ansehen')->icon(Heroicon::OutlinedEye)->color('gray')
->url('/')->openUrlInNewTab(),
];
}
}