Markierte Quellübungen (Kennung in der Datenbank) */ public array $selected = []; /** auswahl | vorschau | ergebnis */ public string $step = 'auswahl'; /** @var list> */ public array $preview = []; /** @var array> Quellübung => (entfallene Quellvariante => neue Zuordnung) */ public array $reassign = []; /** @var array> Quellübung => (Text oder Feld => `lokal` oder `quelle`) */ public array $textChoice = []; /** @var list> */ public array $results = []; public string $feedback = ''; public bool $feedbackError = false; /** * Die Übernahme ist ein Weg im Übungsbestand („Aus Shinkan übernehmen“) und kein eigener Einstieg. Nur wer die * Anbindung pflegen darf, aber den Übungsbestand nicht sieht, bekommt den Eintrag in der Navigation. */ public static function shouldRegisterNavigation(): bool { return static::canConfigure() && ! ContentElementResource::editor(); } public static function canAccess(): bool { return ContentElementResource::editor() || static::canConfigure(); } /** Den Schlüssel ändern dürfen nur Katalogverwalter und Systemverwalter. */ public static function canConfigure(): bool { $user = Filament::auth()->user(); return $user instanceof User && ($user->hasStaffRole(StaffRole::Katalogverwalter) || $user->hasStaffRole(StaffRole::Systemverwalter)); } public function getHeading(): string|Htmlable|null { return null; } public function getMaxContentWidth(): Width|string|null { return Width::Full; } public function saveKey(): void { abort_unless(static::canConfigure(), 403); try { app(ShinkanConnection::class)->saveKey($this->apiKey, $this->actor()); $this->say('Der Zugangsschlüssel ist gespeichert. Prüfe jetzt die Verbindung. Der Schlüssel wird nie wieder angezeigt.'); } catch (DomainRuleException $exception) { $this->say($exception->getMessage(), true); } $this->apiKey = ''; } public function removeKey(): void { abort_unless(static::canConfigure(), 403); app(ShinkanConnection::class)->forgetKey($this->actor()); $this->say('Der in der Verwaltung hinterlegte Zugangsschlüssel ist entfernt. Ein Schlüssel aus der Umgebung des Servers bleibt davon unberührt.'); } /** * Prüft die Verbindung. Ein eingetragener, noch nicht gespeicherter Schlüssel wird mitgeprüft (nur Verwalter). */ public function check(): void { abort_unless(static::canAccess(), 403); $candidate = static::canConfigure() ? $this->apiKey : ''; $result = app(ShinkanImport::class)->check($candidate); $this->apiKey = ''; $this->say($result['message'], ! $result['ok']); } public function fetch(): void { abort_unless(static::canAccess(), 403); $run = app(ShinkanImport::class)->fetch(); $this->say( $run->status === 'ok' ? 'Abruf abgeschlossen: '.$run->added.' neu, '.$run->updated.' geändert, '.$run->removed.' nicht mehr bereitgestellt. Es wurde noch keine lokale Fassung angelegt.' : (string) $run->error, $run->status !== 'ok', ); } public function review(): void { abort_unless(static::canAccess(), 403); $ids = array_values(array_unique(array_map('intval', $this->selected))); if ($ids === []) { $this->say('Markiere mindestens eine Übung.', true); return; } $this->buildPreview($ids); $this->step = 'vorschau'; $this->say(''); } public function backToSelection(): void { $this->step = 'auswahl'; $this->preview = []; $this->say(''); } public function confirm(): void { abort_unless(static::canAccess(), 403); $import = app(ShinkanImport::class); $changed = []; $open = []; foreach ($this->preview as $entry) { $source = ExerciseSource::query()->find($entry['source']); if ($source === null) { continue; } $latest = $source->latestSnapshot(); if (($source->availability === ExerciseSource::WITHDRAWN) !== (bool) $entry['excluded'] || $latest?->id !== $entry['snapshot']) { $changed[] = $entry['title']; } foreach ($entry['text_conflicts'] as $conflict) { if (! in_array($this->textChoice[$entry['source']][$conflict['key']] ?? 'offen', ['lokal', 'quelle'], true)) { $open[] = $entry['title'].': '.$conflict['label']; } } foreach ($entry['conflicts'] as $conflict) { if (! isset($this->reassign[$entry['source']][$conflict['key']]) || $this->reassign[$entry['source']][$conflict['key']] === 'offen') { $open[] = $entry['title'].': '.implode(', ', $conflict['members']); } } } if ($changed !== []) { $this->buildPreview(array_map(fn (array $entry): int => (int) $entry['source'], $this->preview)); $this->say('Der Quellstand hat sich seit der Vorschau geändert ('.implode(', ', $changed).'). Die Vorschau ist aktualisiert. Prüfe sie und bestätige den neuen Stand bewusst. Es wurde nichts übernommen.', true); return; } if ($open !== []) { $this->say('Lege zuerst fest, was gilt: '.implode('; ', $open).'.', true); return; } $this->results = []; foreach ($this->preview as $entry) { $source = ExerciseSource::query()->findOrFail($entry['source']); $reassign = array_map(fn (string $value): string => $value === 'gemeinsam' ? '' : $value, $this->reassign[$entry['source']] ?? []); $result = $import->adoptPreviewed($source, (int) $entry['snapshot'], $reassign, $this->actor(), $this->textChoice[$entry['source']] ?? []); $source->refresh(); $this->results[] = [ 'title' => $entry['title'], 'status' => $result['status'], 'message' => $result['message'], 'url' => $source->composition === null ? null : PracticeResource::getUrl('edit', ['record' => $source->composition]), ]; } $this->selected = []; $this->preview = []; $this->step = 'ergebnis'; $this->say(''); } public function done(): void { $this->step = 'auswahl'; $this->results = []; $this->say(''); } /** * @return array */ public function daten(): array { $import = app(ShinkanImport::class); $labels = [ 'neu' => ['Neu', 'is-neu'], 'geaendert' => ['Geänderter Quellstand', 'is-geaendert'], 'uebernommen' => ['Übernommen', 'is-eigen'], 'nicht_mehr_bereitgestellt' => ['Nicht mehr bereitgestellt', ''], ]; $needle = mb_strtolower(trim($this->search)); $rows = []; $total = 0; foreach (ExerciseSource::query()->with('composition')->orderBy('title')->get() as $source) { $total++; $state = $import->state($source); if (($this->statusFilter !== 'alle' && $this->statusFilter !== $state) || ($needle !== '' && ! str_contains(mb_strtolower(($source->title ?? '').' '.$source->source_id), $needle))) { continue; } $rows[] = [ 'id' => $source->id, 'titel' => $source->title ?: $source->source_id, 'zustand' => $labels[$state][0], 'ton' => $labels[$state][1], 'state' => $state, 'markierbar' => $state !== 'nicht_mehr_bereitgestellt', 'lokal' => $source->composition === null ? null : PracticeResource::getUrl('edit', ['record' => $source->composition]), 'hinweis' => $state === 'nicht_mehr_bereitgestellt' ? 'Lokale Fassungen und Verwendungen bleiben unverändert. Eine rechtliche Sperre ist ein eigener Vorgang am Medium.' : null, ]; } return [ 'zeilen' => $rows, 'gesamt' => $total, 'optionen' => ['alle' => 'Alle Zustände', 'neu' => 'Neu', 'geaendert' => 'Geänderter Quellstand', 'uebernommen' => 'Übernommen', 'nicht_mehr_bereitgestellt' => 'Nicht mehr bereitgestellt'], 'eingerichtet' => $import->configured(), 'anbindung' => $import->status(), 'darfAendern' => static::canConfigure(), 'letzterLauf' => ExerciseImportRun::query()->orderByDesc('id')->first(), ]; } /** * @param list $ids */ private function buildPreview(array $ids): void { $import = app(ShinkanImport::class); $this->preview = []; foreach (ExerciseSource::query()->whereIn('id', $ids)->orderBy('title')->get() as $source) { $this->preview[] = $import->preview($source); } } private function actor(): ?User { $user = Filament::auth()->user(); return $user instanceof User ? $user : null; } private function say(string $message, bool $error = false): void { $this->feedback = $message; $this->feedbackError = $error; } }