label('Vorschau') ->icon('heroicon-o-device-phone-mobile') ->color('gray') ->tooltip('Zuletzt gespeicherten Entwurf in Smartphone-, Tablet- und Desktopbreite ansehen') ->url(function (): string { $revision = $this->record->latestRevision; return $revision === null ? '#' : route('pages.preview.devices', $revision); }) ->openUrlInNewTab(), Action::make('publish') ->label('Veröffentlichen') ->icon('heroicon-o-globe-alt') ->requiresConfirmation() ->modalHeading('Seite veröffentlichen?') ->modalDescription('Der aktuelle Stand im Editor wird als Entwurf gespeichert und genau dieser Stand wird veröffentlicht. Enthält er Fehler, wird nichts veröffentlicht.') ->modalSubmitActionLabel('Speichern und veröffentlichen') ->visible(fn (): bool => auth()->user()?->can('publish', $this->record) ?? false) ->action(function (PageService $service): void { // Ungespeicherte Änderungen (zum Beispiel ein eben gewähltes Bild) gingen sonst beim Veröffentlichen verloren. $this->save(shouldRedirect: false, shouldSendSavedNotification: false); $this->record->unsetRelation('latestRevision'); $revision = $this->record->latestRevision; abort_if($revision === null, 404); $service->publish($this->record, $revision); $this->record->refresh(); Notification::make()->title('Seite veröffentlicht')->success()->send(); }), Action::make('open') ->label('Live-Seite') ->icon('heroicon-o-arrow-top-right-on-square') ->color('gray') ->visible(fn (): bool => $this->record->isPublished()) ->url(fn (): string => $this->record->publicUrl()) ->openUrlInNewTab(), ]; } protected function getSaveFormAction(): Action { return parent::getSaveFormAction()->label('Entwurf speichern'); } protected function getSavedNotificationTitle(): ?string { return 'Entwurf gespeichert. Die Live-Seite bleibt unverändert, bis Sie veröffentlichen.'; } /** * Nach dem Speichern den Editor aus der gespeicherten Revision neu befüllen, damit duplizierte * Abschnitte ihre beim Speichern vergebene Kennung behalten. */ protected function afterSave(): void { $this->record->unsetRelation('latestRevision'); $this->fillForm(); } /** * @param array $data * @return array */ protected function mutateFormDataBeforeFill(array $data): array { $revision = $this->record->latestRevision ?? $this->record->publishedRevision; $data['sections'] = SectionEditor::toEditorState($revision?->sections() ?? []); $data['seo_title'] = $revision?->seo_title; $data['seo_description'] = $revision?->seo_description; $data['pillars'] = $this->record->pillars()->pluck('pillars.id')->all(); return $data; } /** * @param array $data */ protected function handleRecordUpdate(Model $record, array $data): Model { /** @var Page $record */ $service = app(PageService::class); $content = SectionEditor::fromEditorState($data['sections'] ?? []); try { $path = $service->availablePath($data['path'], $record); $service->saveRevision($record, $content, $data['seo_title'] ?? null, $data['seo_description'] ?? null, auth()->user()); } catch (ValidationException $exception) { SectionEditor::notifyErrors($exception->errors(), $this->data['sections'] ?? []); throw ValidationException::withMessages( SectionEditor::mapErrors($exception->errors(), $this->data['sections'] ?? []), ); } $record->update([ 'path' => $path, ]); app(PillarService::class)->assignPages($record, array_map(intval(...), $data['pillars'] ?? [])); return $record; } }