Ein Baustein (FormattedText) bereinigt, bringt Werte in die kanonische Form und wandelt Klartext des Altbestands; RichField ist der eine Editor, ManagesRichFields verbindet ihn mit den Arbeitsplätzen. Formatiert sind Texte der Übungen und Textbausteine, Kurzbeschreibung und Beschreibung, Lernziel und Ausstattung. Mitgliederansicht und PDF zeigen fett, kursiv, Listen, Überschrift, Zitat und Link (PDF: schräge Schnitte, nur wenn gebraucht). Das Livewire-Limit für verschachtelte Pfade steigt auf 30, weil Aufzählungen sonst abgelehnt werden. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
104 lines
3.9 KiB
PHP
104 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Support;
|
|
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use App\Domain\Catalog\ContentService;
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
use App\Domain\Catalog\FormattedText;
|
|
use App\Filament\Resources\ContentElements\Pages\EditContentElement;
|
|
use Filament\Forms\Components\Component;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Toggle;
|
|
|
|
class ElementRevision
|
|
{
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public static function publish(ContentElement $record, array $data): string
|
|
{
|
|
$latest = $record->versions()->orderByDesc('number')->first();
|
|
$metadata = [];
|
|
$incoming = IncomingFile::store($data['incoming'] ?? null);
|
|
$dropFile = (bool) ($data['drop_file'] ?? false);
|
|
$previousFile = is_array($latest?->metadata) ? ($latest->metadata['file'] ?? null) : null;
|
|
|
|
if (is_string($incoming) && $incoming !== '') {
|
|
$metadata['incoming'] = $incoming;
|
|
} elseif (! $dropFile && is_string($previousFile) && $previousFile !== '') {
|
|
$metadata['file'] = $previousFile;
|
|
}
|
|
|
|
$decision = is_numeric($data['decision_seconds'] ?? null) ? (int) $data['decision_seconds'] : null;
|
|
|
|
if ($decision !== null) {
|
|
$metadata['decision_seconds'] = $decision;
|
|
}
|
|
|
|
$body = is_string($data['body'] ?? null) ? (FormattedText::canonical($data['body']) ?: null) : null;
|
|
$note = is_string($data['change_note'] ?? null) ? $data['change_note'] : null;
|
|
$previousDecision = is_array($latest?->metadata) && is_numeric($latest->metadata['decision_seconds'] ?? null)
|
|
? (int) $latest->metadata['decision_seconds']
|
|
: null;
|
|
$changed = $incoming !== null
|
|
|| $dropFile
|
|
|| ! FormattedText::same($body, $latest?->body)
|
|
|| $decision !== $previousDecision
|
|
|| (is_string($note) && trim($note) !== '');
|
|
|
|
if (! $changed) {
|
|
return 'Text und Datei sind gleich geblieben, deshalb gibt es keine neue Fassung.';
|
|
}
|
|
|
|
if (trim((string) $body) === '' && $metadata === []) {
|
|
throw new DomainRuleException('Eine neue Fassung braucht einen Text oder eine Datei.');
|
|
}
|
|
|
|
app(ContentService::class)->publishElementVersion($record, $body, $metadata, $note);
|
|
|
|
return $incoming !== null
|
|
? 'Die neue Fassung ist angelegt. Die Datei wird übernommen; die bisherige Fassung bleibt.'
|
|
: 'Die neue Fassung ist angelegt. Die bisherige Fassung bleibt erhalten.';
|
|
}
|
|
|
|
/**
|
|
* @return list<Component>
|
|
*/
|
|
public static function fields(ContentElement $element): array
|
|
{
|
|
$latest = $element->versions()->orderByDesc('number')->first();
|
|
|
|
return [
|
|
RichField::make('body', 'Text', live: false)->default(FormattedText::html($latest?->body)),
|
|
EditContentElement::uploadField(),
|
|
Toggle::make('drop_file')
|
|
->label('Datei in der neuen Fassung nicht mehr verwenden')
|
|
->visible(fn (): bool => is_string(self::fileOf($element))),
|
|
TextInput::make('decision_seconds')
|
|
->label('Entscheidungsmarke in Sekunden')
|
|
->numeric()
|
|
->minValue(0)
|
|
->default(self::decisionOf($latest)),
|
|
TextInput::make('change_note')->label('Änderungsnotiz'),
|
|
];
|
|
}
|
|
|
|
private static function fileOf(ContentElement $element): mixed
|
|
{
|
|
$latest = $element->versions()->orderByDesc('number')->first();
|
|
|
|
return is_array($latest?->metadata) ? ($latest->metadata['file'] ?? null) : null;
|
|
}
|
|
|
|
private static function decisionOf(?ContentElementVersion $version): ?int
|
|
{
|
|
if (! is_array($version?->metadata) || ! is_numeric($version->metadata['decision_seconds'] ?? null)) {
|
|
return null;
|
|
}
|
|
|
|
return (int) $version->metadata['decision_seconds'];
|
|
}
|
|
}
|