Werte aus der Umgebung des Servers haben Vorrang, sonst gilt der in der Verwaltung gepflegte. Der Schlüssel liegt verschlüsselt in site_settings und wird nie angezeigt. Änderung nur für Katalog- und Systemverwalter. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
178 lines
6.6 KiB
PHP
178 lines
6.6 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\ExerciseSourceSnapshot;
|
|
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;
|
|
|
|
/**
|
|
* Übernahme aus Shinkan: Welche Übungen stellt die Quelle bereit, was ist neu oder geändert, was ist übernommen und was
|
|
* stellt die Quelle nicht mehr bereit? Abrufen verändert nur den Quellenstand; erst „Übernehmen“ legt eine lokale
|
|
* Fassung an, und erst eine Produktveröffentlichung setzt sie ein.
|
|
*/
|
|
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 $url = '';
|
|
|
|
public string $apiKey = '';
|
|
|
|
public string $feedback = '';
|
|
|
|
public bool $feedbackError = false;
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
return ContentElementResource::editor() || static::canConfigure();
|
|
}
|
|
|
|
/** Adresse und 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 mount(): void
|
|
{
|
|
$this->url = app(ShinkanConnection::class)->source('url') === ShinkanConnection::ADMIN ? (string) app(ShinkanConnection::class)->url() : '';
|
|
}
|
|
|
|
public function saveConnection(): void
|
|
{
|
|
abort_unless(static::canConfigure(), 403);
|
|
|
|
try {
|
|
$user = Filament::auth()->user();
|
|
app(ShinkanConnection::class)->save($this->url, $this->apiKey, $user instanceof User ? $user : null);
|
|
$this->apiKey = '';
|
|
$this->feedbackError = false;
|
|
$this->feedback = 'Die Anbindung ist gespeichert. Werte aus der Umgebung des Servers haben weiterhin Vorrang. Prüfe die Verbindung, bevor du den Katalog abrufst.';
|
|
} catch (DomainRuleException $exception) {
|
|
$this->feedbackError = true;
|
|
$this->feedback = $exception->getMessage();
|
|
}
|
|
}
|
|
|
|
public function removeKey(): void
|
|
{
|
|
abort_unless(static::canConfigure(), 403);
|
|
|
|
$user = Filament::auth()->user();
|
|
app(ShinkanConnection::class)->forgetKey($user instanceof User ? $user : null);
|
|
$this->feedbackError = false;
|
|
$this->feedback = 'Der in der Verwaltung gepflegte Zugangsschlüssel ist entfernt.';
|
|
}
|
|
|
|
public function getHeading(): string|Htmlable|null
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function getMaxContentWidth(): Width|string|null
|
|
{
|
|
return Width::Full;
|
|
}
|
|
|
|
public function fetch(): void
|
|
{
|
|
abort_unless(static::canAccess(), 403);
|
|
|
|
$run = app(ShinkanImport::class)->fetch();
|
|
$this->feedbackError = $run->status !== 'ok';
|
|
$this->feedback = $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;
|
|
}
|
|
|
|
public function check(): void
|
|
{
|
|
abort_unless(static::canAccess(), 403);
|
|
|
|
$result = app(ShinkanImport::class)->check();
|
|
$this->feedbackError = ! $result['ok'];
|
|
$this->feedback = $result['message'];
|
|
}
|
|
|
|
public function adopt(int $snapshotId): void
|
|
{
|
|
abort_unless(static::canAccess(), 403);
|
|
|
|
try {
|
|
$user = Filament::auth()->user();
|
|
$version = app(ShinkanImport::class)->adopt(ExerciseSourceSnapshot::query()->findOrFail($snapshotId), $user instanceof User ? $user : null);
|
|
$this->feedbackError = false;
|
|
$this->feedback = 'Fassung '.$version->number.' ist lokal angelegt und unveränderlich. Produkte verwenden sie erst, wenn du sie dort bewusst einsetzt und das Produkt veröffentlichst.';
|
|
} catch (DomainRuleException $exception) {
|
|
$this->feedbackError = true;
|
|
$this->feedback = $exception->getMessage();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function daten(): array
|
|
{
|
|
$import = app(ShinkanImport::class);
|
|
$labels = [
|
|
'neu' => ['Neu bei der Quelle', 'offen'],
|
|
'geaendert' => ['Neue Quellfassung zur Übernahme', 'offen'],
|
|
'uebernommen' => ['Übernommen', 'gut'],
|
|
'nicht_mehr_bereitgestellt' => ['An der Quelle nicht mehr bereitgestellt', 'leise'],
|
|
];
|
|
$rows = [];
|
|
|
|
foreach (ExerciseSource::query()->with('composition')->orderBy('title')->get() as $source) {
|
|
$state = $import->state($source);
|
|
$latest = $source->latestSnapshot();
|
|
$payload = $latest?->payload ?? [];
|
|
$rows[] = [
|
|
'titel' => $source->title ?: $source->source_id,
|
|
'zustand' => $labels[$state][0],
|
|
'ton' => $labels[$state][1],
|
|
'snapshot' => $latest !== null && $latest->adopted_version_id === null ? $latest->id : null,
|
|
'lokal' => $source->composition === null ? null : PracticeResource::getUrl('edit', ['record' => $source->composition]),
|
|
'varianten' => count($payload['variants'] ?? []),
|
|
'quellmedien' => array_map(fn (array $media): string => ($media['title'] ?? '').' ('.($media['media_type'] ?? '').')', (array) ($payload['media'] ?? [])),
|
|
'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,
|
|
'eingerichtet' => app(ShinkanImport::class)->configured(),
|
|
'anbindung' => app(ShinkanImport::class)->status(),
|
|
'darfAendern' => static::canConfigure(),
|
|
'letzterLauf' => ExerciseImportRun::query()->orderByDesc('id')->first(),
|
|
];
|
|
}
|
|
}
|