user()?->can('updates.manage') ?? false; } /** @var list 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 $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 */ protected function getViewData(): array { $demo = app(Demo::class); return [ 'installed' => $demo->isInstalled(), 'installedAt' => $demo->installedAt(), 'counts' => $demo->counts(), 'areas' => Demo::clearableAreas(), ]; } }