*/ 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“ */ public function add(string $key, string $component, string $area, int $sort, callable $props, ?int $mobileSort = null, ?string $label = null): void { if (! in_array($area, self::AREAS, true)) { throw new \InvalidArgumentException("Unbekannter Bereich „{$area}“ 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', '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 */ 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'], '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(), 'label' => 'Eigene Kachel: '.$tile->name, 'custom' => true, ]; } $layout = $this->layout(); return $widgets->sortBy(fn (array $w, string $key) => $layout[$key]['sort'] ?? $w['sort']); } /** * Gespeicherte Anordnung: Schlüssel => [sort, visible]. * * @return array */ private function layout(): array { return (array) app(Settings::class)->get('dashboard.layout', []); } /** * @return list}> */ public function resolve(User $user, Request $request): array { $result = []; $layout = $this->layout(); foreach ($this->candidates($user) as $key => $widget) { if (($layout[$key]['visible'] ?? true) === false) { 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; } }