columns(1)->components([ TextInput::make('working_title')->label('Arbeitstitel')->required(), Placeholder::make('history')->label('Bisherige Fassungen')->content(function (): HtmlString { /** @var ContentElement $element */ $element = $this->getRecord(); $lines = app(CatalogReading::class)->elementVersions($element); $html = collect($lines)->map(fn (string $line): string => e($line))->implode('
'); $links = $element->versions()->orderByDesc('number')->get()->map(function (ContentElementVersion $version): string { return 'Fassung '.$version->number.' ansehen'; })->implode(' · '); return new HtmlString($html.($links !== '' ? '
'.$links : '')); }), Placeholder::make('usage')->label('Verwendung')->content(function (): HtmlString { /** @var ContentElement $element */ $element = $this->getRecord(); $lines = app(CatalogReading::class)->elementUsage($element); return new HtmlString(collect($lines)->map(fn (string $line): string => e($line))->implode('
')); }), Section::make('Neue Fassung')->schema([ Textarea::make('body')->label('Text')->rows(8), self::uploadField(), Toggle::make('drop_file') ->label('Datei in der neuen Fassung nicht mehr verwenden') ->visible(fn (): bool => is_string($this->latest()?->metadata['file'] ?? null)), TextInput::make('decision_seconds')->label('Entscheidungsmarke in Sekunden')->numeric()->minValue(0), TextInput::make('change_note')->label('Änderungsnotiz'), ]), ]); } public static function uploadField(): FileUpload { return FileUpload::make('incoming') ->label('Datei') ->disk('cms_incoming') ->directory('inhalte') ->visibility('private') ->maxSize(64 * 1024) ->helperText('Film, Audio, Bild oder PDF bis 64 MB.'); } protected function getHeaderActions(): array { return [ Action::make('preview') ->label('Aktuelle Fassung ansehen') ->url(fn (): ?string => ($version = $this->latest()) === null ? null : route('content.preview', $version)) ->visible(fn (): bool => $this->latest() !== null) ->openUrlInNewTab(), Action::make('block') ->label('Fassung sperren') ->visible(fn (): bool => $this->getRecord()->versions()->where('status', ContentVersionStatus::Bereit)->exists()) ->schema([ Select::make('version')->label('Fassung')->options(function (): array { return $this->getRecord()->versions() ->where('status', ContentVersionStatus::Bereit) ->orderByDesc('number') ->pluck('number', 'id') ->map(fn ($number): string => 'Fassung '.$number) ->all(); })->required(), Textarea::make('reason')->label('Begründung')->required(), ]) ->action(function (array $data): void { $this->report(function () use ($data): void { $version = ContentElementVersion::query()->findOrFail($data['version']); app(ContentService::class)->block($version, $data['reason']); }, 'Die Fassung ist gesperrt.'); $this->getRecord()->unsetRelation('versions'); $this->fillForm(); }), 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('Neue Fassung anlegen'); } protected function getSavedNotificationTitle(): ?string { return $this->savedTitle ?? 'Die neue Fassung ist angelegt.'; } /** * @param array $data * @return array */ protected function mutateFormDataBeforeFill(array $data): array { $latest = $this->latest(); $metadata = is_array($latest?->metadata) ? $latest->metadata : []; return [ 'working_title' => $this->getRecord()->working_title, 'body' => $latest?->body, 'decision_seconds' => $metadata['decision_seconds'] ?? null, 'drop_file' => false, 'change_note' => null, ]; } /** * @param array $data */ protected function handleRecordUpdate(Model $record, array $data): Model { /** @var ContentElement $record */ try { $record->working_title = trim((string) $data['working_title']); $record->save(); $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) { $this->savedTitle = 'Der Arbeitstitel ist geändert. Text und Datei sind gleich geblieben, deshalb gibt es keine neue Fassung.'; return $record; } 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); $this->savedTitle = $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.'; } catch (DomainRuleException $exception) { Notification::make()->title($exception->getMessage())->danger()->send(); throw new Halt; } return $record; } protected function afterSave(): void { $this->getRecord()->unsetRelation('versions'); $this->getRecord()->refresh(); $this->fillForm(); } private function latest(): ?ContentElementVersion { /** @var ContentElement $element */ $element = $this->getRecord(); return $element->versions()->orderByDesc('number')->first(); } }