Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m29s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m56s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m2s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m20s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m22s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m42s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m41s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m18s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
187 lines
8.9 KiB
PHP
187 lines
8.9 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Pages;
|
||
|
||
use App\Models\AuditLog;
|
||
use App\Models\User;
|
||
use App\Plugins\PluginManager;
|
||
use App\Support\SiteHealth;
|
||
use App\Support\Update\InstalledVersion;
|
||
use Filament\Pages\Dashboard;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Str;
|
||
use Throwable;
|
||
|
||
/**
|
||
* Übersicht im Backend: Kennzahlen, offene Aufgaben, Schnellzugriffe und die
|
||
* letzten Änderungen – statt einer leeren Startseite.
|
||
*/
|
||
class AdminDashboard extends Dashboard
|
||
{
|
||
protected string $view = 'filament.pages.admin-dashboard';
|
||
|
||
protected static ?string $title = 'Übersicht';
|
||
|
||
protected static ?string $navigationLabel = 'Übersicht';
|
||
|
||
/** Lesbare Bezeichnungen für Einträge im Protokoll */
|
||
private const ACTIONS = [
|
||
'appearance.published' => 'Erscheinungsbild veröffentlicht',
|
||
'demo.installed' => 'Demo-Inhalte angelegt',
|
||
'demo.removed' => 'Demo-Inhalte entfernt',
|
||
'downloads.uploaded' => 'Download hochgeladen',
|
||
'downloads.version_added' => 'Neue Download-Version',
|
||
'downloads.deleted' => 'Download gelöscht',
|
||
'forms.fields_updated' => 'Formular geändert',
|
||
'forms.exported' => 'Formular-Ergebnisse exportiert',
|
||
'identity.provider_created' => 'Anmeldeverfahren angelegt',
|
||
'identity.provider_updated' => 'Anmeldeverfahren geändert',
|
||
'identity.user_created' => 'Person bei Anmeldung angelegt',
|
||
'identity.user_deactivated' => 'Person deaktiviert',
|
||
'license.saved' => 'Lizenz gespeichert',
|
||
'mail.settings_updated' => 'E-Mail-Versand geändert',
|
||
'media.replaced' => 'Bild ersetzt',
|
||
'media.deleted' => 'Bild gelöscht',
|
||
'media.bulk_deleted' => 'Bilder aufgeräumt',
|
||
'navigation.menu_saved' => 'Menü gespeichert',
|
||
'news.published' => 'Beitrag veröffentlicht',
|
||
'news.submitted' => 'Beitrag zur Freigabe eingereicht',
|
||
'news.returned' => 'Beitrag zurückgegeben',
|
||
'news.deleted' => 'Beitrag gelöscht',
|
||
'news.comment.deleted' => 'Kommentar moderiert',
|
||
'page.created' => 'Seite angelegt',
|
||
'page.published' => 'Seite veröffentlicht',
|
||
'page.scheduled' => 'Seite geplant',
|
||
'page.unpublished' => 'Seite offline genommen',
|
||
'page.deleted' => 'Seite gelöscht',
|
||
'page.review_requested' => 'Freigabe einer Seite angefordert',
|
||
'plugin.activated' => 'Plugin aktiviert',
|
||
'plugin.deactivated' => 'Plugin deaktiviert',
|
||
'plugin.deleted' => 'Plugin gelöscht',
|
||
'plugin.installed' => 'Plugin installiert',
|
||
'plugin.access' => 'Zugang zu einem Modul geändert',
|
||
'qm.entry_created' => 'QM-Dokument angelegt',
|
||
'qm.entry_updated' => 'QM-Dokument geändert',
|
||
'qm.entry_deleted' => 'QM-Dokument gelöscht',
|
||
'role.created' => 'Rolle angelegt',
|
||
'role.updated' => 'Rolle geändert',
|
||
'speiseplan.uploaded' => 'Speiseplan hochgeladen',
|
||
'update.installed' => 'Aktualisierung installiert',
|
||
'update.failed' => 'Aktualisierung fehlgeschlagen',
|
||
'wordpress.imported' => 'WordPress-Import',
|
||
];
|
||
|
||
/** Anmeldungen und ähnliche Routine gehören nicht in die Übersicht */
|
||
private const HIDDEN = ['identity.login', 'identity.roles_synced', 'feedback.sent'];
|
||
|
||
public function getColumns(): int|array
|
||
{
|
||
return 1;
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
protected function getViewData(): array
|
||
{
|
||
$user = auth()->user();
|
||
$plugins = app(PluginManager::class);
|
||
$all = $plugins->all();
|
||
$version = app(InstalledVersion::class);
|
||
|
||
return [
|
||
'user' => $user,
|
||
'stats' => array_values(array_filter([
|
||
['label' => 'Aktive Personen', 'value' => User::query()->active()->count(), 'hint' => User::query()->active()->where('created_at', '>=', now()->subDays(30))->count().' neu in 30 Tagen', 'icon' => 'heroicon-o-users', 'url' => $user->can('users.manage') ? '/admin/benutzer' : null],
|
||
$plugins->isLoaded('news') ? ['label' => 'Neuigkeiten', 'value' => $this->count('news_posts', fn ($q) => $q->whereNotNull('published_at')->where('published_at', '>=', now()->subDays(30))), 'hint' => 'veröffentlicht in 30 Tagen', 'icon' => 'heroicon-o-newspaper', 'url' => '/admin/neuigkeiten'] : null,
|
||
['label' => 'Plugins', 'value' => count(array_filter(array_keys($all), fn ($slug) => $plugins->isEnabled($slug))).' / '.count($all), 'hint' => 'aktiv', 'icon' => 'heroicon-o-puzzle-piece', 'url' => $user->can('plugins.manage') ? '/admin/plugins' : null],
|
||
['label' => 'Version', 'value' => $version->release(), 'hint' => $version->short() ? 'Stand '.$version->short() : 'Aktualisierung', 'icon' => 'heroicon-o-arrow-path', 'url' => $user->can('updates.manage') ? '/admin/aktualisierung' : null],
|
||
])),
|
||
'tasks' => $this->tasks($user),
|
||
'shortcuts' => array_values(array_filter([
|
||
$plugins->isLoaded('news') ? ['label' => 'Neuer Beitrag', 'icon' => 'heroicon-o-pencil-square', 'url' => '/redaktion/neuigkeiten/neu'] : null,
|
||
$plugins->isLoaded('pages') ? ['label' => 'Seiten', 'icon' => 'heroicon-o-document-text', 'url' => '/admin/seiten'] : null,
|
||
$user->can('users.manage') ? ['label' => 'Person anlegen', 'icon' => 'heroicon-o-user-plus', 'url' => '/admin/benutzer/create'] : null,
|
||
$user->can('appearance.manage') ? ['label' => 'Erscheinungsbild', 'icon' => 'heroicon-o-paint-brush', 'url' => '/admin/erscheinungsbild'] : null,
|
||
$user->can('appearance.manage') ? ['label' => 'Startseite & Menü', 'icon' => 'heroicon-o-home', 'url' => '/admin/startseite-menue'] : null,
|
||
$user->can('roles.manage') ? ['label' => 'Rechte prüfen', 'icon' => 'heroicon-o-shield-check', 'url' => '/admin/rechte-pruefen'] : null,
|
||
])),
|
||
'activity' => $user->can('updates.manage') ? $this->activity() : [],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return list<array{label: string, detail: string, url: ?string, tone: string}>
|
||
*/
|
||
private function tasks(User $user): array
|
||
{
|
||
$tasks = [];
|
||
|
||
if (app(PluginManager::class)->isLoaded('news') && ($user->can('news.manage') || $user->can('news.post.publish'))) {
|
||
$pending = $this->count('news_posts', fn ($q) => $q->where('status', 'pending'));
|
||
|
||
if ($pending > 0) {
|
||
$tasks[] = ['label' => $pending === 1 ? '1 Beitrag wartet auf Freigabe' : "{$pending} Beiträge warten auf Freigabe", 'detail' => 'In der Redaktion prüfen und veröffentlichen', 'url' => '/redaktion', 'tone' => 'warning'];
|
||
}
|
||
}
|
||
|
||
if ($user->can('updates.manage')) {
|
||
try {
|
||
$problems = Cache::remember('admin.health-problems', 300, fn () => collect(app(SiteHealth::class)->all())->flatten(1)
|
||
->filter(fn ($check) => in_array($check['status'], [SiteHealth::WARNING, SiteHealth::ERROR], true))
|
||
->map(fn ($check) => ['label' => $check['label'], 'status' => $check['status']])->values()->all());
|
||
} catch (Throwable) {
|
||
$problems = [];
|
||
}
|
||
|
||
foreach (array_slice($problems, 0, 4) as $problem) {
|
||
$tasks[] = ['label' => $problem['label'], 'detail' => $problem['status'] === SiteHealth::ERROR ? 'Fehler im Systemzustand' : 'Hinweis im Systemzustand', 'url' => '/admin/systemzustand', 'tone' => $problem['status'] === SiteHealth::ERROR ? 'danger' : 'warning'];
|
||
}
|
||
}
|
||
|
||
return $tasks;
|
||
}
|
||
|
||
/**
|
||
* @return list<array{label: string, who: ?string, when: string, ago: string}>
|
||
*/
|
||
private function activity(): array
|
||
{
|
||
return AuditLog::query()->with('user')->whereNotIn('action', self::HIDDEN)->latest('id')->limit(10)->get()
|
||
->map(fn (AuditLog $log) => [
|
||
'label' => self::ACTIONS[$log->action] ?? Str::of($log->action)->replace(['.', '_'], ' ')->ucfirst()->toString(),
|
||
'detail' => $this->detail((array) $log->data),
|
||
'who' => $log->user?->name,
|
||
'when' => $log->created_at?->format('d.m.Y H:i') ?? '',
|
||
'ago' => $log->created_at?->diffForHumans() ?? '',
|
||
])->all();
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
*/
|
||
private function detail(array $data): ?string
|
||
{
|
||
if (is_string($data['plugin'] ?? null) && ($plugin = app(PluginManager::class)->find($data['plugin']))) {
|
||
return $plugin->name;
|
||
}
|
||
|
||
foreach (['title', 'plugin', 'name', 'sha', 'channel'] as $key) {
|
||
if (is_scalar($data[$key] ?? null) && (string) $data[$key] !== '') {
|
||
return Str::limit((string) $data[$key], 60);
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private function count(string $table, callable $scope): int
|
||
{
|
||
try {
|
||
return $scope(DB::table($table))->count();
|
||
} catch (Throwable) {
|
||
return 0;
|
||
}
|
||
}
|
||
}
|