, sort: int, mobileSort: int, props: callable, label: string}> */ private array $widgets = []; /** * @param string $component Vue-Komponente, z. B. „core::QuickLinks“ oder „news::Hero“ * @param callable(User, Request): (array|null) $props * @param int|null $mobileSort Reihenfolge in der einspaltigen Mobilansicht (Standard: nach Bereich) * @param string|null $label Name im Admin unter „Startseite“ * @param list $areas Bereiche, in die die Administration die Kachel verschieben darf */ public function add(string $key, string $component, string $area, int $sort, callable $props, ?int $mobileSort = null, ?string $label = null, array $areas = []): void { $areas = array_values(array_unique([$area, ...$areas])); foreach ($areas as $candidate) { if (! in_array($candidate, self::AREAS, true)) { throw new \InvalidArgumentException("Unbekannter Bereich „{$candidate}“ für Kachel {$key}."); } } $mobileSort ??= ['top' => -100, 'hero' => 10, 'feed' => 20, 'aside' => 30, 'main' => 50][$area] + $sort; $label ??= $key; $this->widgets[$key] = compact('component', 'area', 'areas', 'sort', 'mobileSort', 'props', 'label'); } public function remove(string $key): void { unset($this->widgets[$key]); } /** * Alle Kacheln eines Bereichs für den Admin: mitgelieferte und eigene, * in gespeicherter Reihenfolge, mit Sichtbarkeit. * * @return list, visible: bool, custom: bool}> */ public function forAdmin(string $area): array { $layout = $this->layout(); return $this->candidates() ->filter(fn (array $w) => $w['area'] === $area) ->map(fn (array $w, string $key) => [ 'key' => $key, 'label' => $w['label'], 'area' => $w['area'], 'areas' => $w['areas'], 'visible' => $layout[$key]['visible'] ?? true, 'custom' => $w['custom'] ?? false, ]) ->values() ->all(); } /** * Mitgelieferte Kacheln plus eigene Kacheln, sortiert nach gespeicherter Anordnung. * * @return Collection> */ private function candidates(?User $user = null) { $widgets = collect($this->widgets); try { $tiles = DashboardTile::query()->when($user, fn ($q) => $q->visibleTo($user))->get(); } catch (Throwable) { $tiles = collect(); // Tabelle noch nicht angelegt (Aktualisierung läuft) } foreach ($tiles as $tile) { $widgets[$tile->widgetKey()] = [ 'component' => 'core::CustomTile', 'area' => array_key_exists($tile->area, DashboardTile::AREAS) ? $tile->area : 'aside', 'sort' => 1000 + $tile->id, 'mobileSort' => ['top' => -50, 'main' => 45, 'aside' => 32][$tile->area] ?? 32, 'props' => fn () => $tile->toWidgetProps(), 'areas' => [], 'label' => 'Eigene Kachel: '.$tile->name, 'custom' => true, ]; } $layout = $this->layout(); // Von der Administration verschobene Kacheln (nur in erlaubte Bereiche) $widgets = $widgets->map(function (array $w, string $key) use ($layout) { $moved = $layout[$key]['area'] ?? null; if ($moved && in_array($moved, $w['areas'] ?? [], true)) { $w['area'] = $moved; } return $w; }); return $widgets->sortBy(fn (array $w, string $key) => $layout[$key]['sort'] ?? $w['sort']); } /** * Gespeicherte Anordnung: Schlüssel => [sort, visible, area]. * * @return array */ private function layout(): array { return (array) app(Settings::class)->get('dashboard.layout', []); } /** * Seitenleiste der Startseite für andere Seiten (z. B. Termine). * * @param list $except Kacheln, die auf der Seite doppelt wären * @return list}> */ public function sidebar(User $user, Request $request, array $except = []): array { $keep = fn (array $w) => $w['area'] === 'aside' && ! in_array($w['key'], $except, true); return array_values(array_filter($this->resolve($user, $request, $keep), $keep)); } /** * @param (callable(array{key: string, area: string}): bool)|null $only nur diese Kacheln berechnen * @return list}> */ public function resolve(User $user, Request $request, ?callable $only = null): array { $result = []; $layout = $this->layout(); foreach ($this->candidates($user) as $key => $widget) { if (($layout[$key]['visible'] ?? true) === false || ($only && ! $only(['key' => $key, 'area' => $widget['area']])) || ! app(PluginAccess::class)->allows($key, $user)) { continue; } try { $props = ($widget['props'])($user, $request); } catch (Throwable $e) { // Eine fehlerhafte Kachel darf die Startseite nicht lahmlegen. Log::error("Startseiten-Kachel {$key} fehlgeschlagen: ".$e->getMessage(), ['exception' => $e]); continue; } if ($props !== null) { $result[] = ['key' => $key, 'component' => $widget['component'], 'area' => $widget['area'], 'mobileSort' => $widget['mobileSort'], 'props' => $props]; } } return $result; } }