$blocks Bausteine im Altformat oder als Widget-Hülle */ public function saveRevision(Page $page, array $blocks, ?string $seoTitle, ?string $seoDescription, ?User $author): PageRevision { return $this->storeRevision($page, $this->widgets->normalize($blocks), $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. Die gespeicherten 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->seo_title, $source->seo_description, $author, ); } /** * 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 $blocks */ private function storeRevision(Page $page, array $blocks, ?string $seoTitle, ?string $seoDescription, ?User $author): PageRevision { $revision = $page->revisions()->create([ 'blocks' => array_values($blocks), 'seo_title' => $seoTitle !== null && $seoTitle !== '' ? $seoTitle : null, 'seo_description' => $seoDescription !== null && $seoDescription !== '' ? $seoDescription : null, 'created_by' => $author?->id, ]); foreach ($this->widgets->mediaPaths($revision->blocks) as $path) { PublishPublicMedia::dispatch($path); } return $revision; } }