Loheide-eu-mitarbeiterplatt.../app/Dashboard/Homepage.php
Friederich Loheide 2c96fcce1c
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m21s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m55s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m40s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m42s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m25s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m57s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m34s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
Startseite frei gestalten, Neuigkeiten-Block mit Kategorien-Filter (0.26.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 09:38:18 +00:00

162 lines
5.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Dashboard;
use App\Models\User;
use App\Pagebuilder\Content;
use App\Support\Audit;
use App\Support\Settings;
use Illuminate\Support\Str;
/**
* Frei gestaltete Startseite: Inhalt aus Pagebuilder-Blöcken (inkl. aller Kacheln
* als Block „Kachel“). Alternativ bleibt die Startseite aus Kacheln (Widgets).
*
* Gespeichert in den Einstellungen: veröffentlichter Stand, Entwurf und die letzten Versionen.
*/
class Homepage
{
public const MODE = 'homepage.mode'; // widgets | builder
public const PUBLISHED = 'homepage.content';
public const DRAFT = 'homepage.draft';
public const REVISIONS = 'homepage.revisions';
public const MAX_REVISIONS = 20;
public const WIDGET_BLOCK = 'dashboard.widget';
public function __construct(private readonly Settings $settings, private readonly Content $content) {}
public function isBuilder(): bool
{
return $this->settings->get(self::MODE) === 'builder' && $this->published() !== null;
}
public function setMode(string $mode): void
{
$this->settings->set(self::MODE, $mode === 'builder' ? 'builder' : 'widgets');
}
public function mode(): string
{
return $this->settings->get(self::MODE) === 'builder' ? 'builder' : 'widgets';
}
/**
* @return array{blocks: list<array<string, mixed>>}|null
*/
public function published(): ?array
{
$content = $this->settings->get(self::PUBLISHED);
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(self::DRAFT);
return is_array($draft) && is_array($draft['content'] ?? null) ? $draft : null;
}
/**
* Arbeitsstand für den Editor: Entwurf, sonst veröffentlichter Stand, sonst die heutige Kachel-Anordnung.
*
* @return array{blocks: list<array<string, mixed>>}
*/
public function working(): array
{
return $this->draft()['content'] ?? $this->published() ?? $this->fromWidgets();
}
/**
* @param array<mixed> $content
*/
public function saveDraft(array $content, User $user): void
{
$this->settings->set(self::DRAFT, ['content' => $this->content->sanitize($content), 'updated_at' => now()->toIso8601String(), 'user' => $user->name]);
}
public function discardDraft(): void
{
$this->settings->set(self::DRAFT, null);
}
public function publish(User $user): void
{
$content = $this->working();
$revisions = (array) $this->settings->get(self::REVISIONS, []);
array_unshift($revisions, ['id' => Str::random(8), 'content' => $content, 'at' => now()->toIso8601String(), 'user' => $user->name]);
$this->settings->setMany([
self::PUBLISHED => $content,
self::DRAFT => null,
self::REVISIONS => array_slice($revisions, 0, self::MAX_REVISIONS),
]);
Audit::log('homepage.published', ['blocks' => count($content['blocks'] ?? [])]);
}
/**
* @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(self::REVISIONS, []));
}
/**
* Frühere Fassung als Entwurf zurückholen.
*/
public function restore(string $id, User $user): bool
{
foreach ((array) $this->settings->get(self::REVISIONS, []) as $revision) {
if (($revision['id'] ?? null) === $id) {
$this->saveDraft($revision['content'], $user);
return true;
}
}
return false;
}
/**
* Anzeige für eine Person (veröffentlicht oder – in der Vorschau – der Entwurf).
*
* @return array{blocks: list<array<string, mixed>>}
*/
public function render(User $user, bool $draft = false): array
{
return $this->content->render($draft ? $this->working() : ($this->published() ?? ['blocks' => []]), $user, ['homepage' => true]);
}
/**
* Die heutige Kachel-Startseite als Blöcke: oben die Banner, darunter zwei Spalten
* (Hauptbereich und Seitenleiste).
*
* @return array{blocks: list<array<string, mixed>>}
*/
public function fromWidgets(): array
{
$areas = app(Widgets::class)->arrangement();
$block = fn (string $key) => ['id' => Str::random(12), 'type' => self::WIDGET_BLOCK, 'data' => ['widget' => $key]];
$main = array_map($block, [...array_slice($areas['hero'] ?? [], 0, 1), ...($areas['feed'] ?? []), ...($areas['main'] ?? [])]);
$aside = array_map($block, $areas['aside'] ?? []);
return $this->content->sanitize(['blocks' => [
...array_map($block, $areas['top'] ?? []),
['id' => Str::random(12), 'type' => 'columns', 'data' => ['layout' => '2-1', 'stack' => 'md', 'gap' => 'normal', 'align' => 'start'], 'items' => [
['id' => Str::random(12), 'blocks' => $main],
['id' => Str::random(12), 'blocks' => $aside],
]],
]]);
}
}