Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 10m3s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 9m26s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 10m59s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 11m24s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m26s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m54s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m26s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 49s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m6s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
117 lines
3.6 KiB
PHP
117 lines
3.6 KiB
PHP
<?php
|
||
|
||
namespace App\Pagebuilder;
|
||
|
||
use App\Models\User;
|
||
use App\Support\Audit;
|
||
use App\Support\Settings;
|
||
use Illuminate\Support\Str;
|
||
|
||
/**
|
||
* Baukasten-Inhalt, der in den Einstellungen liegt (Startseite, Fußzeile): veröffentlichter
|
||
* Stand, Entwurf und die letzten Versionen.
|
||
*
|
||
* @property-read Settings $settings
|
||
* @property-read Content $content
|
||
*/
|
||
trait StoredDocument
|
||
{
|
||
/** Schlüssel-Präfix in den Einstellungen, z. B. „homepage“ */
|
||
abstract protected function prefix(): string;
|
||
|
||
/**
|
||
* Ausgangspunkt, solange nichts veröffentlicht ist.
|
||
*
|
||
* @return array{blocks: list<array<string, mixed>>}
|
||
*/
|
||
abstract protected function initial(): array;
|
||
|
||
/**
|
||
* @return array{blocks: list<array<string, mixed>>}|null
|
||
*/
|
||
public function published(): ?array
|
||
{
|
||
$content = $this->settings->get($this->prefix().'.content');
|
||
|
||
return is_array($content) ? $content : null;
|
||
}
|
||
|
||
/**
|
||
* @return array{content: array<string, mixed>, updated_at: string, user: ?string}|null
|
||
*/
|
||
public function draft(): ?array
|
||
{
|
||
$draft = $this->settings->get($this->prefix().'.draft');
|
||
|
||
return is_array($draft) && is_array($draft['content'] ?? null) ? $draft : null;
|
||
}
|
||
|
||
/**
|
||
* Arbeitsstand für den Editor: Entwurf, sonst veröffentlichter Stand, sonst der Ausgangspunkt.
|
||
*
|
||
* @return array{blocks: list<array<string, mixed>>}
|
||
*/
|
||
public function working(): array
|
||
{
|
||
return $this->draft()['content'] ?? $this->published() ?? $this->initial();
|
||
}
|
||
|
||
/**
|
||
* @param array<mixed> $content
|
||
*/
|
||
public function saveDraft(array $content, User $user): void
|
||
{
|
||
$this->settings->set($this->prefix().'.draft', ['content' => $this->content->sanitize($content), 'updated_at' => now()->toIso8601String(), 'user' => $user->name]);
|
||
}
|
||
|
||
public function discardDraft(): void
|
||
{
|
||
$this->settings->set($this->prefix().'.draft', null);
|
||
}
|
||
|
||
public function publish(User $user): void
|
||
{
|
||
$content = $this->working();
|
||
$revisions = (array) $this->settings->get($this->prefix().'.revisions', []);
|
||
array_unshift($revisions, ['id' => Str::random(8), 'content' => $content, 'at' => now()->toIso8601String(), 'user' => $user->name]);
|
||
|
||
$this->settings->setMany([
|
||
$this->prefix().'.content' => $content,
|
||
$this->prefix().'.draft' => null,
|
||
$this->prefix().'.revisions' => array_slice($revisions, 0, self::MAX_REVISIONS),
|
||
$this->prefix().'.published_at' => now()->toIso8601String(),
|
||
]);
|
||
|
||
Audit::log($this->prefix().'.published', ['blocks' => count($content['blocks'] ?? [])]);
|
||
}
|
||
|
||
/** Wann zuletzt veröffentlicht – z. B. als Cache-Schlüssel */
|
||
public function version(): string
|
||
{
|
||
return (string) ($this->settings->get($this->prefix().'.published_at') ?? '0');
|
||
}
|
||
|
||
/**
|
||
* @return list<array{id: string, at: string, user: ?string, blocks: int}>
|
||
*/
|
||
public function revisions(): array
|
||
{
|
||
return array_map(fn (array $r) => ['id' => $r['id'], 'at' => $r['at'], 'user' => $r['user'] ?? null, 'blocks' => count($r['content']['blocks'] ?? [])], (array) $this->settings->get($this->prefix().'.revisions', []));
|
||
}
|
||
|
||
/**
|
||
* Frühere Fassung als Entwurf zurückholen.
|
||
*/
|
||
public function restore(string $id, User $user): bool
|
||
{
|
||
foreach ((array) $this->settings->get($this->prefix().'.revisions', []) as $revision) {
|
||
if (($revision['id'] ?? null) === $id) {
|
||
$this->saveDraft($revision['content'], $user);
|
||
|
||
return true;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
}
|