Loheide-eu-mitarbeiterplatt.../app/Dashboard/Widgets.php
Friederich Loheide 1877a0922f
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m45s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m39s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m25s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m21s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m49s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m59s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m21s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m30s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s
Rollen ausschließen: bei Inhalten (Zielgruppen) und für ganze Module
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 19:30:11 +00:00

185 lines
6.9 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\Plugins\PluginAccess;
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', []);
}
/**
* Seitenleiste der Startseite für andere Seiten (z. B. Termine).
*
* @param list<string> $except Kacheln, die auf der Seite doppelt wären
* @return list<array{key: string, component: string, area: string, mobileSort: int, props: array<string, mixed>}>
*/
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<array{key: string, component: string, area: string, mobileSort: int, props: array<string, mixed>}>
*/
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;
}
}