Loheide-eu-mitarbeiterplatt.../app/Dashboard/Widgets.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

170 lines
6.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\Dashboard;
use App\Models\DashboardTile;
use App\Models\User;
use App\Support\Settings;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Throwable;
/**
* Kacheln der Startseite als Registry.
*
* Bereiche (siehe resources/js/pages/Dashboard.vue):
* top – volle Breite über allem (Hinweise, z. B. Geburtstagsgruß)
* hero – groß links oben (z. B. Slider) feed – neben hero
* main – volle Breite der linken Spalte aside – rechte Spalte
*
* Kacheln mit mehreren erlaubten Bereichen ($areas) kann die Administration
* unter „Portal → Startseite“ zwischen diesen Bereichen verschieben.
*
* Die Props-Funktion liefert die Daten der Kachel oder null, wenn sie für
* diese Person nichts anzuzeigen hat.
*/
class Widgets
{
public const AREAS = ['top', 'hero', 'feed', 'main', 'aside'];
/** @var array<string, array{component: string, area: string, areas: list<string>, 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<string, mixed>|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<string> $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<array{key: string, label: string, area: string, areas: list<string>, 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<string, array<string, mixed>>
*/
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<string, array{sort?: int, visible?: bool, area?: string}>
*/
private function layout(): array
{
return (array) app(Settings::class)->get('dashboard.layout', []);
}
/**
* @return list<array{key: string, component: string, area: string, mobileSort: int, props: array<string, mixed>}>
*/
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;
}
}