- Auffangroute und Pfadprüfung nehmen beliebig tiefe Adressen (/kurse/kinder) an; die Elternseite muss nicht existieren. - ReservedPaths sperrt jeden ersten Pfadteil fester Routen und des Ordners public/ (zur Laufzeit aus dem Router); frei bleiben nur /, /shop und /angebote. Eine bestehende Seite mit unverändertem Pfad bleibt speicherbar. - Menü: Eintrag im aktiven Pfad, wenn die aktuelle Seite unter seinem Ziel liegt. - Verwaltung: Hilfetext und Hinweis am Adressfeld, Sortierung nach Pfadteilen in Webpräsenz und Seitenauswahlen, Chip "Unterseite von ...". - ShopSwitchTest: Ein POST auf die entfernte Kaufadresse ergibt nun 405 statt 404, weil die Auffangroute die mehrteilige Adresse trifft (bei einteiligen Adressen schon bisher so). Keine Bestellung entsteht weiterhin; Begründung in ADR-72. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
175 lines
6.7 KiB
PHP
175 lines
6.7 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;
|
|
use InvalidArgumentException;
|
|
|
|
class PageService
|
|
{
|
|
public function __construct(private readonly PageSections $sections, private readonly ReservedPaths $reserved) {}
|
|
|
|
/**
|
|
* 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 er keiner festen Funktion der Anwendung gehört und keine andere Seite ihn verwendet.
|
|
*/
|
|
public function availablePath(string $path, ?Page $except = null): string
|
|
{
|
|
$path = $this->normalizePath($path);
|
|
|
|
// Eine bestehende Seite mit unverändertem Pfad bleibt speicherbar, auch wenn ihr Pfad später reserviert wurde
|
|
// (sie ist dann nur nicht erreichbar); geprüft wird, wenn der Pfad neu vergeben oder geändert wird.
|
|
if ($except?->path !== $path && $this->reserved->blocks($path)) {
|
|
throw ValidationException::withMessages([
|
|
'path' => 'Diese Adresse gehört zu einer festen Funktion der Anwendung ('.PagePath::segments($path)[0].') und kann keine Seite sein.',
|
|
]);
|
|
}
|
|
|
|
$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
|
|
{
|
|
try {
|
|
return PagePath::normalize($path);
|
|
} catch (InvalidArgumentException $exception) {
|
|
throw ValidationException::withMessages(['path' => $exception->getMessage()]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adresse der übergeordneten Stufe, an der keine Seite liegt (für einen Hinweis am Adressfeld); null, wenn dort eine
|
|
* Seite liegt oder es keine Zwischenstufe gibt. Die Zwischenstufe muss keine Seite sein, sie ergibt dann 404.
|
|
*/
|
|
public function missingParent(string $path): ?string
|
|
{
|
|
$parent = PagePath::parent($path);
|
|
|
|
if ($parent === null || $parent === '/') {
|
|
return null;
|
|
}
|
|
|
|
return Page::query()->where('path', $parent)->exists() ? null : $parent;
|
|
}
|
|
|
|
/**
|
|
* @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);
|
|
|
|
// „Geändert“ in der Seitenliste zeigt die letzte Bearbeitung, nicht das Anlagedatum.
|
|
$page->touch();
|
|
|
|
foreach ($this->sections->mediaPaths($revision->sections()) as $path) {
|
|
PublishPublicMedia::dispatch($path);
|
|
}
|
|
|
|
return $revision;
|
|
}
|
|
}
|