Loheide-eu-mitarbeiterplatt.../app/Dashboard/Widgets.php
Friederich Loheide ee8dde93c0
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m18s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m21s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m40s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m34s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 3m8s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 3m6s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m43s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m47s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m1s
Feste Profilfelder in Formularen, Geburtstage, anpassbare Startseite
- Formulare: Felder fest aus dem Profil (nicht änderbar, Server setzt den
  Profilwert ein), Hinweis bei fehlender Profilangabe
- Geburtstage (optionales Plugin, standardmäßig aus): Tag und Monat freiwillig
  im Profil, Kachel, Liste /geburtstage, Pagebuilder-Block, Glückwunsch am
  eigenen Geburtstag; Sichtbarkeit alle/Abteilung/Standort
- Startseite: Kacheln je Bereich sortieren und ausblenden; eigene Kacheln
  (Bild mit Link, Text, Hinweis) mit Zeitraum und Zielgruppe
- Kern: optionale mitgelieferte Plugins, Startseitenbereich „top“,
  Profilabschnitte für Plugins, Kachel-Bezeichnungen

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 09:51:19 +00:00

149 lines
5.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
*
* 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, 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“
*/
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<array{key: string, label: string, area: 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'],
'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(),
'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<string, array{sort?: int, visible?: bool}>
*/
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;
}
}