Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 8m25s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 8m46s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 11m36s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 11m31s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 11m34s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 12m12s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 12m14s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 46s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 11m18s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m15s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
244 lines
11 KiB
PHP
244 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Filament\Clusters\HomepageCluster;
|
|
use App\Models\Role;
|
|
use App\Support\Audit;
|
|
use App\Support\Icons;
|
|
use App\Support\Navigation;
|
|
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\TextInput;
|
|
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\Components\Utilities\Set;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Support\Icons\Heroicon;
|
|
use Illuminate\Support\HtmlString;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Hauptmenü des Portals selbst gestalten: Bereiche, Seiten, Links und Gruppen,
|
|
* jeweils mit Unterpunkten, sortierbar und auf Rollen einschränkbar.
|
|
*/
|
|
class MenuSettings extends Page
|
|
{
|
|
protected string $view = 'filament.pages.settings-form';
|
|
|
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedBars3;
|
|
|
|
protected static ?string $cluster = HomepageCluster::class;
|
|
|
|
protected static ?int $navigationSort = 3;
|
|
|
|
protected static ?string $navigationLabel = 'Menü';
|
|
|
|
protected static ?string $title = 'Menü';
|
|
|
|
protected static ?string $slug = 'menue';
|
|
|
|
private const URL_PATTERN = '#^(/|https?://|mailto:|tel:)#i';
|
|
|
|
/** @var array<string, mixed>|null */
|
|
public ?array $data = [];
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
return auth()->user()?->can('appearance.manage') ?? false;
|
|
}
|
|
|
|
public function getSubheading(): ?string
|
|
{
|
|
return app(Settings::class)->get(Navigation::MENU_SETTING)
|
|
? 'Eigenes Menü aktiv. Punkte, die jemand nicht sehen darf (Rolle, Recht, Seite), werden für diese Person ausgeblendet.'
|
|
: 'Zurzeit gilt das automatische Menü aus allen Bereichen und den Seiten „in der Hauptnavigation“. Mit dem ersten Speichern gilt das Menü unten.';
|
|
}
|
|
|
|
public function mount(Settings $settings, Navigation $navigation): void
|
|
{
|
|
$menu = $settings->get(Navigation::MENU_SETTING);
|
|
$this->form->fill(['items' => is_array($menu) && $menu !== [] ? $menu : $this->fromAutomatic($navigation)]);
|
|
}
|
|
|
|
public function form(Schema $schema): Schema
|
|
{
|
|
return $schema->statePath('data')->components([
|
|
Section::make('Menüpunkte')
|
|
->description('Reihenfolge per Ziehen. Punkte mit Unterpunkten öffnen in der Kopfzeile ein Aufklappmenü. Passt nicht alles in die Kopfzeile, erscheinen die letzten Punkte unter „Mehr“.')
|
|
->schema([
|
|
Repeater::make('items')
|
|
->hiddenLabel()
|
|
->schema($this->itemFields(nested: false))
|
|
->columns(4)
|
|
->reorderableWithDragAndDrop()
|
|
->collapsible()
|
|
->collapsed()
|
|
->cloneable()
|
|
->itemLabel(fn (array $state) => $this->itemLabel($state))
|
|
->addActionLabel('Menüpunkt hinzufügen')
|
|
->defaultItems(0),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return list<mixed>
|
|
*/
|
|
private function itemFields(bool $nested): array
|
|
{
|
|
$navigation = app(Navigation::class);
|
|
$types = ['module' => 'Bereich (z. B. Neuigkeiten)', ...$navigation->sourceTypes(), 'link' => 'Link'];
|
|
|
|
if (! $nested) {
|
|
$types['group'] = 'Gruppe (nur Überschrift mit Unterpunkten)';
|
|
}
|
|
|
|
$fields = [
|
|
Hidden::make('id')->default(fn () => (string) Str::uuid()),
|
|
Select::make('type')->label('Art')->options($types)->default('module')->required()->live()->native(false)->selectablePlaceholder(false),
|
|
Select::make('module')->label('Bereich')->required()->native(false)
|
|
->options(fn (Get $get) => $navigation->modules() + (filled($get('module')) && ! isset($navigation->modules()[$get('module')]) ? [$get('module') => $get('module').' (Plugin deaktiviert)'] : []))
|
|
// Symbol des Bereichs vorschlagen, solange noch keines gewählt ist
|
|
->live()->afterStateUpdated(fn (?string $state, Get $get, Set $set) => $state && blank($get('icon')) ? $set('icon', $navigation->iconFor($state)) : null)
|
|
->visible(fn (Get $get) => $get('type') === 'module'),
|
|
Select::make('target')->label(fn (Get $get) => $navigation->sourceTypes()[$get('type')] ?? 'Ziel')
|
|
->options(fn (Get $get) => $navigation->sourceOptions((string) $get('type')))
|
|
->searchable()->required()->native(false)
|
|
->visible(fn (Get $get) => array_key_exists((string) $get('type'), $navigation->sourceTypes())),
|
|
TextInput::make('url')->label('Adresse')->placeholder('/seiten/… oder https://…')->required()->maxLength(500)
|
|
->regex(self::URL_PATTERN)->validationMessages(['regex' => 'Bitte mit / (im Portal) oder https:// beginnen.'])
|
|
->visible(fn (Get $get) => $get('type') === 'link'),
|
|
TextInput::make('label')->label('Bezeichnung')->maxLength(60)
|
|
->placeholder(fn (Get $get) => in_array($get('type'), ['link', 'group'], true) ? '' : 'wie Bereich bzw. Seite')
|
|
->required(fn (Get $get) => in_array($get('type'), ['link', 'group'], true)),
|
|
Select::make('icon')->label('Symbol')->placeholder('Ohne')->native(false)
|
|
->options(Icons::options())->allowHtml()->searchable()
|
|
->getSearchResultsUsing(fn (string $search) => Icons::search($search))
|
|
->getOptionLabelUsing(fn (?string $value) => $value ? Icons::option($value) : null)
|
|
->helperText('Alle 1.800+ Symbole über die Suche (englische Namen, z. B. „calendar“).'),
|
|
Toggle::make('subpages')->label('Unterseiten als Untermenü')->inline(false)
|
|
->visible(fn (Get $get) => $get('type') === 'page'),
|
|
Toggle::make('new_tab')->label('In neuem Tab öffnen')->inline(false)
|
|
->visible(fn (Get $get) => $get('type') === 'link'),
|
|
Select::make('roles')->label('Nur sichtbar für')->multiple()->placeholder('Alle')->native(false)
|
|
->options(fn () => Role::query()->orderBy('name')->pluck('name', 'id')->all())
|
|
->columnSpan(2),
|
|
];
|
|
|
|
if (! $nested) {
|
|
$fields[] = Repeater::make('children')
|
|
->label('Unterpunkte')
|
|
->schema($this->itemFields(nested: true))
|
|
->columns(4)
|
|
->reorderableWithDragAndDrop()
|
|
->collapsible()
|
|
->itemLabel(fn (array $state) => $this->itemLabel($state))
|
|
->addActionLabel('Unterpunkt hinzufügen')
|
|
->defaultItems(0)
|
|
->columnSpanFull();
|
|
}
|
|
|
|
return $fields;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $state
|
|
*/
|
|
private function itemLabel(array $state): HtmlString
|
|
{
|
|
$navigation = app(Navigation::class);
|
|
$type = (string) ($state['type'] ?? '');
|
|
$label = trim((string) ($state['label'] ?? ''));
|
|
|
|
if ($label === '') {
|
|
$label = match ($type) {
|
|
'module' => $navigation->modules()[$state['module'] ?? ''] ?? (($state['module'] ?? 'Bereich').' (Plugin deaktiviert)'),
|
|
'link' => $state['url'] ?? 'Link',
|
|
'group' => 'Gruppe',
|
|
default => trim($navigation->sourceOptions($type)[$state['target'] ?? ''] ?? ($navigation->sourceTypes()[$type] ?? ''), '— '),
|
|
};
|
|
}
|
|
|
|
$kind = ['module' => 'Bereich', 'link' => 'Link', 'group' => 'Gruppe'][$type] ?? ($navigation->sourceTypes()[$type] ?? '');
|
|
$children = count((array) ($state['children'] ?? []));
|
|
|
|
$text = e($label).' · '.e($kind).($children ? " · {$children} Unterpunkte" : '').(! empty($state['roles']) ? ' · eingeschränkt' : '');
|
|
|
|
return new HtmlString('<span style="display:inline-flex;align-items:center;gap:8px">'
|
|
.(Icons::has($state['icon'] ?? null) ? Icons::svg($state['icon'], 16) : '').$text.'</span>');
|
|
}
|
|
|
|
/**
|
|
* Startpunkt für den Editor: das bisherige automatische Menü.
|
|
*
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
private function fromAutomatic(Navigation $navigation): array
|
|
{
|
|
$modules = $navigation->modules();
|
|
$items = [];
|
|
|
|
foreach ($navigation->automatic(request(), auth()->user()) as $item) {
|
|
if (isset($modules[$item['key']])) {
|
|
$items[] = ['id' => (string) Str::uuid(), 'type' => 'module', 'module' => $item['key'], 'label' => null, 'icon' => $item['icon'], 'roles' => [], 'children' => []];
|
|
} elseif (preg_match('/^page-(\d+)$/', $item['key'], $m) && array_key_exists('page', $navigation->sourceTypes())) {
|
|
$items[] = ['id' => (string) Str::uuid(), 'type' => 'page', 'target' => $m[1], 'subpages' => true, 'label' => null, 'icon' => $item['icon'], 'roles' => [], 'children' => []];
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
public function save(Settings $settings): void
|
|
{
|
|
$items = $this->form->getState()['items'] ?? [];
|
|
$clean = fn (array $item) => array_filter([
|
|
'id' => $item['id'] ?? (string) Str::uuid(),
|
|
'type' => $item['type'] ?? 'module',
|
|
'module' => $item['module'] ?? null,
|
|
'target' => isset($item['target']) ? (string) $item['target'] : null,
|
|
'url' => $item['url'] ?? null,
|
|
'label' => filled($item['label'] ?? null) ? trim($item['label']) : null,
|
|
'icon' => Icons::has($item['icon'] ?? null) ? $item['icon'] : null,
|
|
'subpages' => ! empty($item['subpages']),
|
|
'new_tab' => ! empty($item['new_tab']),
|
|
'roles' => array_values(array_map('intval', (array) ($item['roles'] ?? []))),
|
|
], fn ($value) => $value !== null);
|
|
|
|
$menu = array_values(array_map(fn (array $item) => [
|
|
...$clean($item),
|
|
'children' => array_values(array_map($clean, array_values((array) ($item['children'] ?? [])))),
|
|
], array_values($items)));
|
|
|
|
$settings->set(Navigation::MENU_SETTING, $menu);
|
|
Audit::log('navigation.menu_saved', ['items' => count($menu)]);
|
|
|
|
Notification::make()->success()->title('Menü gespeichert')->send();
|
|
}
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('automatic')->label('Automatisches Menü verwenden')->icon(Heroicon::OutlinedArrowUturnLeft)->color('gray')
|
|
->visible(fn () => (bool) app(Settings::class)->get(Navigation::MENU_SETTING))
|
|
->requiresConfirmation()
|
|
->modalDescription('Das eigene Menü wird gelöscht. Danach erscheinen wieder alle Bereiche und die Seiten „in der Hauptnavigation“.')
|
|
->action(function (Settings $settings, Navigation $navigation) {
|
|
$settings->set(Navigation::MENU_SETTING, null);
|
|
Audit::log('navigation.menu_reset');
|
|
$this->form->fill(['items' => $this->fromAutomatic($navigation)]);
|
|
Notification::make()->success()->title('Automatisches Menü aktiv')->send();
|
|
}),
|
|
Action::make('preview')->label('Portal ansehen')->icon(Heroicon::OutlinedEye)->color('gray')
|
|
->url('/')->openUrlInNewTab(),
|
|
];
|
|
}
|
|
}
|