Herkunft lokal, Shinkan oder Shinkan lokal ergänzt im Übungsbestand; Übernahmeseite mit Knöpfen, Kapseln, Karten und Meldungen der Verwaltung; Prüfung im Browser (leer, Abruffehler, Suche, Tastatur, Vorschau, Bestätigung, wiederholt, schmal) mit Bildern im Abnahmeordner. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
307 lines
10 KiB
PHP
307 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
use App\Domain\Catalog\ExerciseImportRun;
|
|
use App\Domain\Catalog\ExerciseSource;
|
|
use App\Domain\Catalog\ShinkanConnection;
|
|
use App\Domain\Catalog\ShinkanImport;
|
|
use App\Domain\Identity\StaffRole;
|
|
use App\Filament\Resources\ContentElements\ContentElementResource;
|
|
use App\Filament\Resources\Practices\PracticeResource;
|
|
use App\Models\User;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Pages\Page;
|
|
use Filament\Support\Enums\Width;
|
|
use Illuminate\Contracts\Support\Htmlable;
|
|
|
|
/**
|
|
* Übungen aus Shinkan übernehmen: abrufen, suchen und nach Zustand filtern, markieren, vor der Übernahme prüfen und
|
|
* ausdrücklich bestätigen. Abrufen verändert nur den Quellenstand; erst die Bestätigung legt lokale Fassungen an, und
|
|
* erst eine Produktveröffentlichung setzt sie ein.
|
|
*
|
|
* Den Zugangsschlüssel hinterlegen und ersetzen nur Katalog- und Systemverwalter. Die Redaktion sieht den Zustand der
|
|
* Anbindung und arbeitet mit ihr; der Schlüssel wird nie an den Browser zurückgegeben.
|
|
*/
|
|
class Uebernahme extends Page
|
|
{
|
|
protected static string|\UnitEnum|null $navigationGroup = 'Inhalte';
|
|
|
|
protected static ?string $navigationLabel = 'Übungen aus Shinkan';
|
|
|
|
protected static ?int $navigationSort = 6;
|
|
|
|
protected static ?string $slug = 'uebernahme';
|
|
|
|
protected static ?string $title = 'Übungen aus Shinkan übernehmen';
|
|
|
|
protected string $view = 'filament.pages.uebernahme';
|
|
|
|
public string $apiKey = '';
|
|
|
|
public string $search = '';
|
|
|
|
public string $statusFilter = 'alle';
|
|
|
|
/** @var list<int|string> Markierte Quellübungen (Kennung in der Datenbank) */
|
|
public array $selected = [];
|
|
|
|
/** auswahl | vorschau | ergebnis */
|
|
public string $step = 'auswahl';
|
|
|
|
/** @var list<array<string, mixed>> */
|
|
public array $preview = [];
|
|
|
|
/** @var array<int|string, array<string, string>> Quellübung => (entfallene Quellvariante => neue Zuordnung) */
|
|
public array $reassign = [];
|
|
|
|
/** @var list<array<string, mixed>> */
|
|
public array $results = [];
|
|
|
|
public string $feedback = '';
|
|
|
|
public bool $feedbackError = false;
|
|
|
|
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['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 die neue Zuordnung der lokalen Ergänzungen fest: '.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());
|
|
$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<string, mixed>
|
|
*/
|
|
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<int> $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;
|
|
}
|
|
}
|