Eine Fassung bleibt nummeriert und kann deaktiviert werden. Beschreibung und fachliche Säulen gehören zum Medium und legen keine neue Fassung an. Co-authored-by: Cursor <cursoragent@cursor.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\Filament\Resources\ContentElements\Pages\EditContentElement;
|
|
use Filament\Forms\Components\Component;
|
|
use Filament\Forms\Components\Textarea;
|
|
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) ? $data['body'] : 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
|
|
|| trim((string) $body) !== trim((string) $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 [
|
|
Textarea::make('body')->label('Text')->rows(8)->default($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'];
|
|
}
|
|
}
|