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>
158 lines
5.6 KiB
PHP
158 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Cms;
|
|
|
|
use App\Domain\Cms\Sections\PageSections;
|
|
use App\Jobs\PublishPublicMedia;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class PageService
|
|
{
|
|
public function __construct(private readonly PageSections $sections) {}
|
|
|
|
/**
|
|
* Speichert einen Entwurf als neue Revision. Veröffentlicht nichts.
|
|
*
|
|
* @param array<int|string, mixed> $content Abschnitte mit Slots; flache Widgets (Altformat oder Hülle)
|
|
* werden als einspaltige Abschnitte übernommen
|
|
*/
|
|
public function saveRevision(Page $page, array $content, ?string $seoTitle, ?string $seoDescription, ?User $author): PageRevision
|
|
{
|
|
$sections = $this->sections->normalize($content);
|
|
$seoTitle = $seoTitle !== null && $seoTitle !== '' ? $seoTitle : null;
|
|
$seoDescription = $seoDescription !== null && $seoDescription !== '' ? $seoDescription : null;
|
|
|
|
// Unveränderter Stand: keine weitere Revision (zum Beispiel „Veröffentlichen“ direkt nach dem Speichern).
|
|
$latest = $page->revisions()->first();
|
|
if ($latest !== null && $latest->sections === $sections && $latest->seo_title === $seoTitle && $latest->seo_description === $seoDescription) {
|
|
return $latest;
|
|
}
|
|
|
|
return $this->storeRevision($page, $this->sections->flatten($sections), $sections, $seoTitle, $seoDescription, $author);
|
|
}
|
|
|
|
public function publish(Page $page, PageRevision $revision): void
|
|
{
|
|
if ($revision->page_id !== $page->id) {
|
|
throw ValidationException::withMessages([
|
|
'revision' => 'Die Revision gehört zu einer anderen Seite.',
|
|
]);
|
|
}
|
|
|
|
$page->published_revision_id = $revision->id;
|
|
$page->save();
|
|
}
|
|
|
|
/**
|
|
* Legt den Stand einer früheren Revision unverändert als neue Revision an. Abschnitte und Bausteine
|
|
* werden nicht umgeschrieben; ältere Formate werden erst beim Lesen angehoben.
|
|
*/
|
|
public function restore(Page $page, PageRevision $source, ?User $author): PageRevision
|
|
{
|
|
if ($source->page_id !== $page->id) {
|
|
throw ValidationException::withMessages([
|
|
'revision' => 'Die Revision gehört zu einer anderen Seite.',
|
|
]);
|
|
}
|
|
|
|
return $this->storeRevision(
|
|
$page,
|
|
$source->blocks ?? [],
|
|
$source->sections,
|
|
$source->seo_title,
|
|
$source->seo_description,
|
|
$author,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Legt eine neue, unveröffentlichte Seite mit dem zuletzt gespeicherten Stand einer vorhandenen Seite an.
|
|
* Die Vorlage bleibt unverändert; spätere Änderungen wirken nicht auf die Kopie.
|
|
*/
|
|
public function duplicate(Page $source, string $path, ?User $author): Page
|
|
{
|
|
$revision = $source->latestRevision ?? $source->publishedRevision;
|
|
$page = Page::query()->create(['path' => $this->availablePath($path)]);
|
|
|
|
$this->saveRevision(
|
|
$page,
|
|
$revision?->sections() ?? [],
|
|
$revision?->seo_title,
|
|
$revision?->seo_description,
|
|
$author,
|
|
);
|
|
|
|
return $page;
|
|
}
|
|
|
|
/**
|
|
* Normalisiert den Pfad und prüft, dass keine andere Seite ihn verwendet.
|
|
*/
|
|
public function availablePath(string $path, ?Page $except = null): string
|
|
{
|
|
$path = $this->normalizePath($path);
|
|
|
|
$taken = Page::query()
|
|
->where('path', $path)
|
|
->when($except !== null, fn ($query) => $query->whereKeyNot($except->getKey()))
|
|
->exists();
|
|
|
|
if ($taken) {
|
|
throw ValidationException::withMessages([
|
|
'path' => 'Diesen Pfad verwendet bereits eine andere Seite.',
|
|
]);
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
|
|
public function normalizePath(string $path): string
|
|
{
|
|
$path = '/'.trim($path, '/');
|
|
|
|
if ($path === '/') {
|
|
return '/';
|
|
}
|
|
|
|
$path = mb_strtolower($path);
|
|
|
|
if (preg_match('#^/[a-z0-9]+(?:-[a-z0-9]+)*$#', $path) !== 1) {
|
|
throw ValidationException::withMessages([
|
|
'path' => 'Der Pfad darf nur Kleinbuchstaben, Ziffern und Bindestriche enthalten.',
|
|
]);
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
|
|
/**
|
|
* @param array<int|string, mixed> $blocks flache Widget-Liste (Rollback-Fassung)
|
|
* @param array<int|string, mixed>|null $sections Abschnitte; null bei Revisionen aus der Zeit vor dem Raster
|
|
*/
|
|
private function storeRevision(Page $page, array $blocks, ?array $sections, ?string $seoTitle, ?string $seoDescription, ?User $author): PageRevision
|
|
{
|
|
$attributes = [
|
|
'blocks' => array_values($blocks),
|
|
'seo_title' => $seoTitle !== null && $seoTitle !== '' ? $seoTitle : null,
|
|
'seo_description' => $seoDescription !== null && $seoDescription !== '' ? $seoDescription : null,
|
|
'created_by' => $author?->id,
|
|
];
|
|
|
|
// Die Erstbefüllung der Website läuft innerhalb einer frühen Migration, bevor die Spalte existiert.
|
|
// Solche Revisionen haben keine Abschnitte und werden beim Lesen als einspaltige Abschnitte gelesen.
|
|
if ($sections !== null && Schema::hasColumn('page_revisions', 'sections')) {
|
|
$attributes['sections'] = array_values($sections);
|
|
}
|
|
|
|
$revision = $page->revisions()->create($attributes);
|
|
|
|
foreach ($this->sections->mediaPaths($revision->sections()) as $path) {
|
|
PublishPublicMedia::dispatch($path);
|
|
}
|
|
|
|
return $revision;
|
|
}
|
|
}
|