Loheide-eu-mitarbeiterplatt.../app/Filament/Pages/DemoContent.php
Friederich Loheide 3addc09c38
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:10.6 (pull_request) Has been skipped
CI / PHP 8.3 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.4 / mariadb:11.4 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.0 (pull_request) Has been skipped
CI / PHP 8.3 / mysql:8.4 (pull_request) Has been skipped
CI / PHP 8.4 / mysql:8.4 (pull_request) Has been skipped
CI / Browser-Tests (pull_request) Successful in 8m33s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 9m8s
CI / PHP 8.3 / mysql:8.4-1 (pull_request) Successful in 17m55s
CI / Build-Artefakte aktuell (pull_request) Successful in 9m50s
Frag das Portal durchsucht alles, neue Demo-Inhalte (0.66.0)
- Wissensquellen: Termine, Downloads, Formulare, Stellen, Ideen, Umfragen,
  Personen, eigene IT-Tickets/Geräte (OpenNIT, je Anfrage zwischengespeichert)
- Ranking mit IDF, Abfrage je Begriff, Abkürzungen als ganze Wörter,
  LIKE unabhängig von Groß-/Kleinschreibung (JSON-Spalten), Datum im Prompt
- Demo-Inhalte für einen Träger der Eingliederungshilfe (DemoData),
  mehrseitige PDFs, vorhandene Inhalte je Bereich entfernen

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-07 20:01:00 +00:00

159 lines
4.7 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\Filament\Pages;
use App\Support\Audit;
use App\Support\Demo\DemoContent as Demo;
use BackedEnum;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Support\Icons\Heroicon;
use Illuminate\Support\Str;
use Throwable;
use UnitEnum;
/**
* Demo-Inhalte für Test- und Vorführinstallationen per Klick anlegen und
* wieder entfernen – ohne Shell-Zugang auf dem Server.
*/
class DemoContent extends Page
{
protected string $view = 'filament.pages.demo-content';
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedSparkles;
protected static string|UnitEnum|null $navigationGroup = 'System';
protected static ?int $navigationSort = 90;
protected static ?string $navigationLabel = 'Demo-Inhalte';
protected static ?string $title = 'Demo-Inhalte';
protected static ?string $slug = 'demo-inhalte';
public static function canAccess(): bool
{
return auth()->user()?->can('updates.manage') ?? false;
}
/** @var list<string> Bereiche, deren vorhandene Inhalte entfernt werden sollen */
public array $clearAreas = [];
/** Sicherheitsabfrage: „LÖSCHEN“ eintippen */
public string $clearConfirm = '';
public function clear(Demo $demo): void
{
abort_unless(static::canAccess(), 403);
if (mb_strtoupper(trim($this->clearConfirm)) !== 'LÖSCHEN') {
Notification::make()->warning()->title('Bitte zur Bestätigung „LÖSCHEN“ eintippen.')->send();
return;
}
$areas = array_values(array_intersect($this->clearAreas, array_keys(Demo::clearableAreas())));
if ($areas === []) {
Notification::make()->warning()->title('Bitte mindestens einen Bereich wählen.')->send();
return;
}
@set_time_limit(300);
try {
$deleted = $demo->clear($areas);
} catch (Throwable $e) {
report($e);
Notification::make()->danger()->title('Inhalte nicht entfernt')->body($e->getMessage())->send();
return;
}
Audit::log('demo.cleared', ['areas' => $areas, 'deleted' => $deleted]);
$this->clearAreas = [];
$this->clearConfirm = '';
Notification::make()->success()->title('Vorhandene Inhalte entfernt')
->body(collect($deleted)->map(fn ($count, $area) => $count.' × '.Str::before(Demo::clearableAreas()[$area], ' ('))->implode(', ') ?: 'Es gab nichts zu löschen.')
->send();
}
public function install(Demo $demo): void
{
abort_unless(static::canAccess(), 403);
@set_time_limit(300);
try {
$result = $demo->install(auth()->user());
} catch (Throwable $e) {
report($e);
Notification::make()->danger()->title('Demo-Inhalte nicht angelegt')->body($e->getMessage())->send();
return;
}
Audit::log('demo.installed', ['counts' => $result['counts']]);
Notification::make()->success()->title('Demo-Inhalte angelegt')
->body(collect($result['counts'])->map(fn ($count, $label) => "{$count} {$label}")->implode(', '))
->send();
$this->warn($result['warnings'], 'Einige Bereiche konnten nicht angelegt werden');
}
public function remove(Demo $demo): void
{
abort_unless(static::canAccess(), 403);
@set_time_limit(300);
try {
$result = $demo->remove();
} catch (Throwable $e) {
report($e);
Notification::make()->danger()->title('Demo-Inhalte nicht entfernt')->body($e->getMessage())->send();
return;
}
Audit::log('demo.removed', ['records' => $result['records'], 'files' => $result['files']]);
Notification::make()->success()->title('Demo-Inhalte entfernt')
->body("{$result['records']} Einträge und {$result['files']} Dateien gelöscht.")
->send();
$this->warn($result['warnings'], 'Nicht alles konnte entfernt werden');
}
/**
* @param list<string> $warnings
*/
private function warn(array $warnings, string $title): void
{
if ($warnings === []) {
return;
}
Notification::make()->warning()->title($title)
->body(Str::limit(implode("\n", $warnings), 1000))
->persistent()
->send();
}
/**
* @return array<string, mixed>
*/
protected function getViewData(): array
{
$demo = app(Demo::class);
return [
'installed' => $demo->isInstalled(),
'installedAt' => $demo->installedAt(),
'counts' => $demo->counts(),
'areas' => Demo::clearableAreas(),
];
}
}