Menü-Editor, farbige Initialen, Seitenköpfe, Favoriten, Vorschaubilder
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m55s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m44s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m25s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m39s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 3m47s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 3m51s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 3m16s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 3m25s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s

- Admin → Portal → Menü: Bereiche, Seiten (mit Unterseiten), Links und
  Gruppen mit Unterpunkten, sortierbar, je Rolle einschränkbar;
  Kopfzeile misst den Platz und legt Überzähliges unter „Mehr“
- Initialen in fester Farbe je Person, Abteilungsfarben im Organigramm
- PageHeader für alle Übersichtsseiten
- Dokumente: Favoriten (auch als Startseiten-Kachel), „Häufig genutzt“,
  Vorschaubilder (Bilder, erste PDF-Seite per pdf.js im Browser)
- Platzhalter beim Nachladen

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-26 13:56:57 +00:00
parent 114811e6c5
commit dda250aaad
140 changed files with 1993 additions and 427 deletions

View file

@ -0,0 +1,230 @@
<?php
namespace App\Filament\Pages;
use App\Models\Role;
use App\Support\Audit;
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\Schema;
use Filament\Support\Icons\Heroicon;
use Illuminate\Support\Str;
use UnitEnum;
/**
* 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|UnitEnum|null $navigationGroup = 'Portal';
protected static ?int $navigationSort = 5;
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)'] : []))
->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)),
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): string
{
$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'] ?? []));
return $label.' · '.$kind.($children ? " · {$children} Unterpunkte" : '').(! empty($state['roles']) ? ' · eingeschränkt' : '');
}
/**
* 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, '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, '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,
'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(),
];
}
}

View file

@ -17,6 +17,10 @@ use Illuminate\Http\Request;
* app(Navigation::class)->provider('pages', fn (Request $request, ?User $user) => [
* ['key' => 'page-3', 'label' => 'MAV', 'href' => '/seiten/mav', 'active' => true, 'sort' => 40],
* ]);
*
* Hat die Administration unter „Menü“ ein eigenes Menü gespeichert, gilt dieses:
* Bereiche (die Einträge oben), Links, Gruppen und Arten aus Plugins (z. B. „Seite“,
* registriert mit source()), jeweils mit Unterpunkten.
*/
class Navigation
{
@ -26,6 +30,14 @@ class Navigation
/** @var array<string, Closure> */
private array $providers = [];
public const MENU_SETTING = 'navigation.menu';
/** @var array<string, array{label: string, options: Closure, resolve: Closure}> */
private array $sources = [];
/** @var list<int>|null Rollen der aktuellen Person (für „Sichtbar für“) */
private ?array $roleIds = null;
public function __construct()
{
$this->add('home', 'Startseite', '/', active: 'dashboard', sort: 0);
@ -55,9 +67,66 @@ class Navigation
}
/**
* @return list<array{key: string, label: string, href: string, active: bool}>
* Weitere Art von Menüpunkten für den Menü-Editor, z. B. „Seite“.
*
* @param Closure(): array<int|string, string> $options Auswahl im Editor
* @param Closure(string, array<string, mixed>, Request, User|null): (array{label: string, href: string, active: bool, children?: list<array{label: string, href: string, active: bool}>}|null) $resolve
* null = nicht (mehr) vorhanden oder nicht sichtbar
*/
public function source(string $type, string $label, Closure $options, Closure $resolve): void
{
$this->sources[$type] = compact('label', 'options', 'resolve');
}
/**
* @return array<string, string> Art => Bezeichnung
*/
public function sourceTypes(): array
{
return array_map(fn (array $source) => $source['label'], $this->sources);
}
/**
* @return array<int|string, string>
*/
public function sourceOptions(string $type): array
{
return isset($this->sources[$type]) ? ($this->sources[$type]['options'])() : [];
}
/**
* Bereiche für den Menü-Editor.
*
* @return array<string, string> Schlüssel => Bezeichnung
*/
public function modules(): array
{
return collect($this->items)->sortBy('sort')->map(fn (array $item) => $item['label'])->all();
}
/**
* Menü für die Kopfzeile. Einträge können Unterpunkte (children) haben.
*
* @return list<array{key: string, label: string, href: string|null, active: bool, new_tab: bool, children: list<array<string, mixed>>}>
*/
public function items(Request $request, ?User $user = null): array
{
$this->roleIds = null; // je Aufruf neu – die Registry lebt über Requests hinweg
$menu = app(Settings::class)->get(self::MENU_SETTING);
$items = is_array($menu) && $menu !== []
? $this->fromMenu($menu, $request, $user)
: $this->automatic($request, $user);
return Hooks::apply('navigation.items', $items, $request, $user);
}
/**
* Das automatische Menü aus Kern, Plugins und Seiten „in der Hauptnavigation“.
*
* @return list<array<string, mixed>>
*/
public function automatic(Request $request, ?User $user = null): array
{
$items = collect($this->items)
->filter(fn (array $item) => $item['ability'] === null || ($user?->can($item['ability']) ?? false))
@ -74,11 +143,115 @@ class Navigation
$items = $items->merge($provider($request, $user));
}
$items = $items->sortBy('sort')
->map(fn (array $item) => ['key' => $item['key'], 'label' => $item['label'], 'href' => $item['href'], 'active' => (bool) $item['active']])
return $items->sortBy('sort')
->map(fn (array $item) => ['key' => $item['key'], 'label' => $item['label'], 'href' => $item['href'], 'active' => (bool) $item['active'], 'new_tab' => false, 'children' => []])
->values()
->all();
}
return Hooks::apply('navigation.items', $items, $request, $user);
/**
* @param list<array<string, mixed>> $menu
* @return list<array<string, mixed>>
*/
private function fromMenu(array $menu, Request $request, ?User $user): array
{
return array_values(array_filter(array_map(fn ($item) => is_array($item) ? $this->resolve($item, $request, $user, true) : null, $menu)));
}
/**
* Einen gespeicherten Menüpunkt auflösen – null, wenn er für diese Person nicht
* sichtbar ist (Rolle, Recht, Plugin deaktiviert, Seite gelöscht, Gruppe leer).
*
* @param array<string, mixed> $item
* @return array<string, mixed>|null
*/
private function resolve(array $item, Request $request, ?User $user, bool $topLevel): ?array
{
$roles = array_map('intval', (array) ($item['roles'] ?? []));
if ($roles !== [] && array_intersect($roles, $this->roleIds($user)) === []) {
return null;
}
$type = (string) ($item['type'] ?? '');
$base = match ($type) {
'module' => $this->module((string) ($item['module'] ?? ''), $request, $user),
'link' => $this->link((string) ($item['url'] ?? ''), $request),
'group' => ['label' => '', 'href' => null, 'active' => false],
default => isset($this->sources[$type]) && filled($item['target'] ?? null)
? ($this->sources[$type]['resolve'])((string) $item['target'], $item, $request, $user)
: null,
};
if ($base === null) {
return null;
}
$children = [];
if ($topLevel) {
foreach ((array) ($item['children'] ?? []) as $child) {
if (is_array($child) && ($resolved = $this->resolve($child, $request, $user, false))) {
$children[] = $resolved;
}
}
foreach ((array) ($base['children'] ?? []) as $child) {
$children[] = ['key' => ($item['id'] ?? $type).'-'.md5($child['href']), 'label' => $child['label'], 'href' => $child['href'], 'active' => (bool) $child['active'], 'new_tab' => false, 'children' => []];
}
}
$label = trim((string) ($item['label'] ?? '')) ?: $base['label'];
if ($label === '' || ($type === 'group' && $children === [])) {
return null;
}
return [
'key' => (string) ($item['id'] ?? md5(json_encode($item))),
'label' => $label,
'href' => $base['href'],
'active' => $base['active'] || collect($children)->contains('active', true),
'new_tab' => $type === 'link' && ! empty($item['new_tab']),
'children' => $children,
];
}
/**
* @return array{label: string, href: string, active: bool}|null
*/
private function module(string $key, Request $request, ?User $user): ?array
{
$item = $this->items[$key] ?? null;
if (! $item
|| ($item['ability'] !== null && ! ($user?->can($item['ability']) ?? false))
|| ($item['visible'] !== null && ($user === null || ! ($item['visible'])($user)))) {
return null;
}
return ['label' => $item['label'], 'href' => $item['href'], 'active' => $item['active'] !== [] && $request->routeIs(...$item['active'])];
}
/**
* @return array{label: string, href: string, active: bool}|null
*/
private function link(string $url, Request $request): ?array
{
if (! preg_match('#^(/|https?://|mailto:|tel:)#i', $url)) {
return null;
}
$path = str_starts_with($url, '/') ? (parse_url($url, PHP_URL_PATH) ?: '/') : null;
return ['label' => '', 'href' => $url, 'active' => $path !== null && $path !== '/' && '/'.ltrim($request->path(), '/') === rtrim($path, '/')];
}
/**
* @return list<int>
*/
private function roleIds(?User $user): array
{
return $this->roleIds ??= $user ? $user->roles()->pluck('roles.id')->map(fn ($id) => (int) $id)->all() : [];
}
}

View file

@ -9,5 +9,5 @@ namespace App;
*/
final class Version
{
public const VERSION = '0.9.1';
public const VERSION = '0.10.0';
}