Files
laraundfelix/app/Filament/Resources/ContentElements/Pages/EditContentElement.php
T
LarsandCursor af85a94cd5
Deploy Test / test (push) Successful in 15s
Deploy Test / deploy (push) Successful in 22s
Zeige Medienfassungen in der Verwaltung und halte Beschreibungen davon getrennt.
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>
2026-11-15 11:25:08 +01:00

140 lines
5.3 KiB
PHP

<?php
namespace App\Filament\Resources\ContentElements\Pages;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\Pillar;
use App\Domain\Catalog\PillarService;
use App\Filament\Resources\ContentElements\ContentElementResource;
use App\Filament\Support\ElementRevision;
use App\Filament\Support\MediaUploadLimit;
use App\Filament\Support\ReportsDomainRules;
use Filament\Actions\Action;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Filament\Schemas\Schema;
use Filament\Support\Exceptions\Halt;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\HtmlString;
class EditContentElement extends EditRecord
{
use ReportsDomainRules;
protected static string $resource = ContentElementResource::class;
public function getSubheading(): string|Htmlable|null
{
return 'Arbeitstitel, Beschreibung und Säulen gehören zum Medium. Speichern legt keine neue Fassung an. Text und Datei änderst du über eine neue Fassung; die bisherige bleibt erhalten.';
}
public function form(Schema $schema): Schema
{
return $schema->columns(1)->components([
Placeholder::make('open')->label('Medium')->content(function (): HtmlString {
$url = ContentElementResource::getUrl('view', ['record' => $this->getRecord()]);
return new HtmlString('<a href="'.e($url).'">Medium und Fassungen ansehen</a>');
}),
TextInput::make('working_title')->label('Arbeitstitel')->required(),
Textarea::make('description')->label('Beschreibung')->rows(4)
->helperText('Gilt für das Medium, nicht für eine einzelne Fassung.'),
CheckboxList::make('pillars')
->label('Fachliche Säulen')
->options(fn (): array => Pillar::query()->orderBy('name')->pluck('name', 'id')->all())
->helperText('Ein Medium kann mehreren Säulen zugeordnet sein. Weitere Säulen legst du unter Fachliche Säulen an.')
->columns(1),
]);
}
public static function uploadField(): FileUpload
{
return FileUpload::make('incoming')
->label('Datei')
->disk('cms_incoming')
->directory('inhalte')
->visibility('private')
->maxSize(MediaUploadLimit::KILOBYTES)
->helperText('Film, Audio, Bild oder PDF bis 2 GB.');
}
protected function getHeaderActions(): array
{
return [
Action::make('view')
->label('Ansehen')
->url(fn (): string => ContentElementResource::getUrl('view', ['record' => $this->getRecord()])),
Action::make('revise')
->label('Neue Fassung anlegen')
->schema(fn (): array => ElementRevision::fields($this->getRecord()))
->action(function (array $data): void {
$this->report(function () use ($data): string {
/** @var ContentElement $element */
$element = $this->getRecord();
return ElementRevision::publish($element, $data);
}, 'Die neue Fassung ist angelegt.');
$this->getRecord()->unsetRelation('versions');
}),
Action::make('delete')
->label('Löschen')
->color('danger')
->requiresConfirmation()
->visible(fn (): bool => $this->getRecord()->versions()->doesntExist())
->action(function (): void {
$this->getRecord()->delete();
$this->redirect(ContentElementResource::getUrl('index'));
}),
];
}
protected function getSaveFormAction(): Action
{
return parent::getSaveFormAction()->label('Speichern');
}
protected function getSavedNotificationTitle(): ?string
{
return 'Gespeichert. Es gibt keine neue Fassung.';
}
/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
protected function mutateFormDataBeforeFill(array $data): array
{
return [
'working_title' => $this->getRecord()->working_title,
'description' => $this->getRecord()->description,
'pillars' => $this->getRecord()->pillars()->pluck('pillars.id')->all(),
];
}
/**
* @param array<string, mixed> $data
*/
protected function handleRecordUpdate(Model $record, array $data): Model
{
/** @var ContentElement $record */
try {
app(ContentService::class)->describe($record, (string) $data['working_title'], $data['description'] ?? null);
app(PillarService::class)->assign($record, array_map(intval(...), $data['pillars'] ?? []));
} catch (DomainRuleException $exception) {
Notification::make()->title($exception->getMessage())->danger()->send();
throw new Halt;
}
return $record;
}
}