Files
laraundfelix/app/Filament/Support/ElementRevision.php
T
LarsandClaude Sonnet 5.5 c57a506f85 Biete Tabellen und Bilder im gemeinsamen Texteditor an und setze sie in Ansicht und PDF
Tabellen und Bilder gehören zur festen Auswahl von FormattedText. Ein Bild
ist ein Verweis auf eine freigegebene Bildfassung und kommt per Picker aus
dem Medienbestand; der Stand schreibt die Bildfassung mit fest, die Rechte
entscheidet DecideAccess. Der PDF-Satz (PdfFlow) ist für Übung und kommende
Dokumente gemeinsam; PdfDocument kann zusätzlich Bilder, Flächen, Verweise
und Lesezeichen, Belege bleiben byte-gleich.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-24 11:31:03 +01:00

105 lines
4.0 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\EmbeddedImages;
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(EmbeddedImages::forEditor($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'];
}
}