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>
95 lines
3 KiB
PHP
95 lines
3 KiB
PHP
<?php
|
||
|
||
namespace App\Dashboard;
|
||
|
||
use App\Models\User;
|
||
use App\Pagebuilder\Content;
|
||
use App\Pagebuilder\StoredDocument;
|
||
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
|
||
{
|
||
use StoredDocument;
|
||
|
||
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';
|
||
}
|
||
|
||
protected function prefix(): string
|
||
{
|
||
return 'homepage';
|
||
}
|
||
|
||
/**
|
||
* @return array{blocks: list<array<string, mixed>>}
|
||
*/
|
||
protected function initial(): array
|
||
{
|
||
return $this->fromWidgets();
|
||
}
|
||
|
||
/**
|
||
* 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],
|
||
]],
|
||
]]);
|
||
}
|
||
}
|