Files
laraundfelix/app/Filament/Support/MediumForm.php
LarsandCursor 89c574106f
Deploy Test / test (push) Successful in 26s
Deploy Test / deploy (push) Successful in 28s
Lege Medien über eine gemeinsame Maske mit Upload-Fortschritt an.
Beschreibung, Hinweise und Vorschaubild bleiben danach in derselben Maske bearbeitbar, im Bestand wie im Modul und Programm.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-17 10:56:03 +01:00

242 lines
8.4 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\IdentityProfile;
use App\Jobs\DeriveThumbnail;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Illuminate\Support\Facades\Storage;
class MediumForm
{
/**
* @return list<FileUpload|Select|Textarea|TextInput>
*/
public static function fields(bool $creating, ?ContentElement $element = null): array
{
$fields = [
TextInput::make('working_title')->label('Titel')->required(),
];
if ($creating) {
$fields[] = Select::make('shape')->label('Was ist das?')->options([
'film' => 'Film',
'audio' => 'Hörstück',
'bild' => 'Bild',
'document' => 'Dokument',
'text' => 'Text',
])->required()->live();
$fields[] = Textarea::make('body')->label('Text')->rows(8)
->visible(fn ($get): bool => $get('shape') === 'text')
->required(fn ($get): bool => $get('shape') === 'text');
$fields[] = self::fileField()
->visible(fn ($get): bool => filled($get('shape')) && $get('shape') !== 'text')
->required(fn ($get): bool => filled($get('shape')) && $get('shape') !== 'text');
} elseif ($element?->output_class === 'text') {
$fields[] = Textarea::make('body')->label('Text')->rows(8)->required();
}
return [
...$fields,
Textarea::make('description')->label('Beschreibung')->rows(4)
->helperText('Gilt für das Medium, nicht nur für diese eine Stelle.'),
Textarea::make('usage_note')->label('Hinweise für den Einsatz')->rows(3),
Textarea::make('license_note')->label('Interner Lizenzvermerk')->rows(3)
->helperText('Nur in der Verwaltung. Nicht in der Bibliothek und nicht auf der Website.'),
TextInput::make('thumbnail_alt')->label('Alternativtext zum Vorschaubild'),
self::thumbnailField(),
];
}
public static function fileField(): FileUpload
{
return FileUpload::make('incoming')
->label('Datei')
->disk('cms_incoming')
->directory('inhalte')
->visibility('private')
->maxSize(MediaUploadLimit::KILOBYTES)
->panelLayout('integrated')
->uploadProgressIndicatorPosition('center')
->helperText('Der Balken zeigt den Upload. Film, Hörstück, Bild oder PDF. Die Übernahme läuft danach im Hintergrund.');
}
public static function thumbnailField(): FileUpload
{
return FileUpload::make('thumbnail_upload')
->label('Vorschaubild')
->disk('cms_incoming')
->directory('vorschau')
->visibility('private')
->image()
->panelLayout('integrated')
->uploadProgressIndicatorPosition('center')
->helperText('Erscheint in der Anzeige und als Vorschlag in Modulen und Programmen. Eine Filmstelle wählst du beim Ansehen des Mediums. Speichern legt keine Fassung an.');
}
/**
* @param array<string, mixed> $data
*/
public static function create(array $data, bool $hold): ContentElement
{
$service = app(ContentService::class);
[$kind, $output] = self::shape($data['shape'] ?? null);
$element = $service->createElement($kind, $output, (string) $data['working_title']);
self::remember($element, $data);
if ($output === 'text') {
$body = trim((string) ($data['body'] ?? ''));
if ($body === '') {
throw new DomainRuleException('Der Text braucht einen Inhalt.');
}
$hold
? $service->draftElementVersion($element, $body)
: $service->publishElementVersion($element, $body);
} else {
$incoming = IncomingFile::store($data['incoming'] ?? null);
if ($incoming === null) {
throw new DomainRuleException('Das Medium braucht eine Datei.');
}
$extension = strtolower(pathinfo($incoming, PATHINFO_EXTENSION));
$allowed = match ($data['shape'] ?? '') {
'audio' => ['mp3', 'm4a', 'wav', 'ogg'],
'bild' => ['png', 'jpg', 'jpeg', 'webp', 'gif'],
'document' => ['pdf'],
default => ['mp4', 'webm', 'mov', 'm4v'],
};
if (! in_array($extension, $allowed, true)) {
throw new DomainRuleException('Diese Datei passt nicht zur gewählten Art.');
}
$metadata = ['incoming' => $incoming];
if ($hold) {
$metadata['hold'] = true;
$service->draftElementVersion($element, null, $metadata);
} else {
$service->publishElementVersion($element, null, $metadata);
}
}
self::queuePreview($element, $data);
return $element->fresh() ?? $element;
}
/**
* @param array<string, mixed> $data
*/
public static function update(ContentElement $element, array $data): ?ContentElementVersion
{
self::remember($element, $data);
$revised = null;
if ($element->output_class === 'text') {
$latest = $element->versions()->orderByDesc('number')->first();
$body = trim((string) ($data['body'] ?? ''));
if ($latest instanceof ContentElementVersion && $body !== '') {
$revised = app(ContentService::class)->reviseElementContent($latest, $body);
}
}
self::queuePreview($element, $data);
return $revised;
}
/**
* @return array<string, string|null>
*/
public static function fill(ContentElement $element): array
{
$latest = $element->versions()->orderByDesc('number')->first();
return [
'working_title' => $element->working_title,
'description' => $element->description,
'usage_note' => $element->usage_note,
'license_note' => $element->license_note,
'thumbnail_alt' => $element->thumbnail_alt,
'body' => $latest?->body,
];
}
/**
* @param array<string, mixed> $data
*/
public static function remember(ContentElement $element, array $data): void
{
app(ContentService::class)->describe($element, (string) $data['working_title'], $data['description'] ?? null);
app(IdentityProfile::class)->fill($element, [
'usage_note' => $data['usage_note'] ?? null,
'license_note' => $data['license_note'] ?? null,
'thumbnail_alt' => $data['thumbnail_alt'] ?? null,
], auth()->user());
}
/**
* @param array<string, mixed> $data
*/
public static function queuePreview(ContentElement $element, array $data): void
{
$path = self::previewPath($data['thumbnail_upload'] ?? null);
if ($path === null) {
return;
}
$element->thumbnail_error = null;
$element->save();
DeriveThumbnail::dispatch('element', $element->id, $path);
}
/**
* @return array{0: string, 1: string}
*/
private static function shape(mixed $shape): array
{
return match ($shape) {
'audio' => ['audio', 'audio'],
'bild' => ['bild', 'bild'],
'document' => ['dokument', 'document'],
'text' => ['text', 'text'],
'film' => ['film', 'film'],
default => throw new DomainRuleException('Die Art des Mediums fehlt.'),
};
}
private static function previewPath(mixed $value): ?string
{
if (is_array($value)) {
foreach ($value as $item) {
$stored = self::previewPath($item);
if ($stored !== null) {
return $stored;
}
}
return null;
}
if (! is_string($value) || $value === '' || str_contains($value, '..') || ! str_starts_with($value, 'vorschau/')) {
return null;
}
return Storage::disk('cms_incoming')->exists($value) ? $value : null;
}
}