Files
laraundfelix/app/Filament/Resources/Pages/Pages/EditPage.php
T
LarsandClaude Opus 5.5 c6186fba6e
CI Feature / test (push) Successful in 35s
Deploy Test / test (push) Successful in 30s
Deploy Test / deploy (push) Successful in 29s
Speichere vor dem Veröffentlichen den aktuellen Stand und warne vor Datenverlust.
Gemeldet: Ein im Widget gewähltes oder hochgeladenes Bild erschien nach
„Veröffentlichen“ nicht auf der Website und war nach dem Neuladen weg. Ursache:
„Veröffentlichen“ stellte die zuletzt gespeicherte Revision live; übernommene, aber
nicht gespeicherte Änderungen gingen verloren. Jetzt speichert „Veröffentlichen“
zuerst und veröffentlicht genau diesen Stand, unveränderte Stände erzeugen keine
neue Revision, und der Editor warnt beim Verlassen mit ungespeicherten Änderungen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 22:05:31 +01:00

139 lines
5.4 KiB
PHP

<?php
namespace App\Filament\Resources\Pages\Pages;
use App\Domain\Catalog\PillarService;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Filament\Resources\Pages\PageResource;
use App\Filament\Support\ReportsSectionErrors;
use App\Filament\Support\SectionEditor;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Filament\Support\Enums\Width;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\ValidationException;
class EditPage extends EditRecord
{
use ReportsSectionErrors;
protected static string $resource = PageResource::class;
/** Breite Arbeitsfläche, damit mehrspaltige Raster im Editor erkennbar bleiben. */
protected Width|string|null $maxContentWidth = Width::Full;
/** Warnt vor dem Verlassen oder Neuladen, wenn Änderungen noch nicht gespeichert sind. */
protected ?bool $hasUnsavedDataChangesAlert = true;
protected function getHeaderActions(): array
{
return [
Action::make('preview')
->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<string, mixed> $data
* @return array<string, mixed>
*/
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<string, mixed> $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;
}
}