Loheide-eu-mitarbeiterplatt.../plugins-core/pages/src/PageTree.php
Friederich Loheide f3987e8386
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m2s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m9s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m40s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m29s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m38s
CI / Build-Artefakte aktuell (pull_request) Successful in 55s
Phase 4: Seiten mit Pagebuilder, Redaktion und Freigaben
- Seiten-Plugin: Seitenbaum mit Adressen, Seitennavigation, Brotkrumen und
  optional Hauptnavigation; Sichtbarkeit vererbt sich auf Unterseiten
- Pagebuilder (Block-Registry im Kern, Plugins tragen Blöcke bei): Abschnitt,
  Spalten, Tabs, Akkordeon, Box, Überschrift, Text (TipTap), Zitat,
  Schaltflächen, Symbolliste, Tabelle, Trennlinie, Abstand, Bild, Galerie,
  Video (erst nach Klick), Titelbild, Ansprechpartner, Downloads, Unterseiten,
  Neuigkeiten, Termine; Sichtbarkeit je Block; Bereinigung beim Speichern
- Editor im Portal: Ziehen und Ablegen, Autospeichern als Entwurf,
  Rückgängig, Gerätevorschau, Versionen, Zeitsteuerung, Bearbeitungssperre,
  Freigabe anfordern
- Rechte „inkl. Unterseiten“ werden im Rechte-Cache aufgelöst
- Neuigkeiten: Schreiben im Portal, Freigabe-Workflow mit Benachrichtigungen
- Bereich „Redaktion“, Benachrichtigungen im Portal
- storage/.migrations-fingerprint nicht mehr ausliefern

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 08:52:19 +00:00

204 lines
6 KiB
PHP
Raw 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 Inhaus\Plugins\Pages;
use App\Models\User;
use App\Support\Permissions\Audiences;
use App\Support\Permissions\Permissions;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
/**
* Der Seitenbaum als gecachte Struktur (nur Arrays): Titel, Adressen,
* Reihenfolge und Zielgruppen aller Seiten. Navigation, Seitenleiste,
* Brotkrumen und Sichtbarkeit kommen ohne weitere Abfragen aus – auch bei
* tausenden Seiten.
*
* Sichtbar ist eine Seite, wenn sie veröffentlicht ist und die Person die
* Zielgruppe der Seite und aller übergeordneten Seiten erfüllt.
*/
class PageTree
{
private const CACHE_KEY = 'pages.tree';
/** @var array{pages: array<int, array<string, mixed>>, children: array<int, list<int>>}|null */
private ?array $data = null;
/** @var array<int, array<int, bool>> */
private array $visible = [];
public static function flush(bool $structureChanged = false): void
{
Cache::forget(self::CACHE_KEY);
app(self::class)->data = null;
app(self::class)->visible = [];
// Rechte „inkl. Unterseiten“ hängen am Baum.
if ($structureChanged) {
Permissions::flush();
}
}
/**
* @return array<string, mixed>|null
*/
public function get(int $id): ?array
{
return $this->data()['pages'][$id] ?? null;
}
/**
* @return list<int>
*/
public function childIds(?int $id): array
{
return $this->data()['children'][$id ?? 0] ?? [];
}
/**
* @param list<int> $ids
* @return list<int>
*/
public function descendantIds(array $ids): array
{
$result = [];
$queue = $ids;
while ($queue !== []) {
foreach ($this->childIds(array_shift($queue)) as $child) {
if (! in_array($child, $result, true)) {
$result[] = $child;
$queue[] = $child;
}
}
}
return $result;
}
/**
* Übergeordnete Seiten, beginnend bei der obersten.
*
* @return list<array<string, mixed>>
*/
public function ancestors(int $id): array
{
$result = [];
$parent = $this->get($id)['parent_id'] ?? null;
while ($parent !== null && ($page = $this->get($parent)) && count($result) < 50) {
array_unshift($result, $page);
$parent = $page['parent_id'];
}
return $result;
}
public function isVisible(int $id, ?User $user): bool
{
$key = $user?->id ?? 0;
if (isset($this->visible[$key][$id])) {
return $this->visible[$key][$id];
}
$page = $this->get($id);
$visible = $page !== null
&& $page['published']
&& Audiences::matches($page['audience'], $user)
&& ($page['parent_id'] === null || $this->isVisible($page['parent_id'], $user));
return $this->visible[$key][$id] = $visible;
}
/**
* Sichtbare Unterseiten als verschachtelte Liste.
*
* @return list<array{id: int, title: string, url: string, children: list<mixed>}>
*/
public function visibleTree(?int $rootId, ?User $user, int $maxDepth = 6): array
{
if ($maxDepth <= 0) {
return [];
}
$result = [];
foreach ($this->childIds($rootId) as $id) {
if ($this->isVisible($id, $user)) {
$page = $this->get($id);
$result[] = [
'id' => $id,
'title' => $page['title'],
'url' => '/seiten/'.$page['path'],
'children' => $this->visibleTree($id, $user, $maxDepth - 1),
];
}
}
return $result;
}
/**
* Seiten der obersten Ebene, die in der Hauptnavigation stehen sollen.
*
* @return list<array{id: int, title: string, path: string, sort: int}>
*/
public function navigation(?User $user): array
{
return collect($this->childIds(null))
->map(fn (int $id) => $this->get($id))
->filter(fn (array $page) => $page['in_navigation'] && $this->isVisible($page['id'], $user))
->map(fn (array $page) => ['id' => $page['id'], 'title' => $page['title'], 'path' => $page['path'], 'sort' => $page['sort']])
->values()
->all();
}
/**
* Alle Seiten mit Einrückung – für Auswahllisten.
*
* @return array<int, string>
*/
public function options(?int $rootId = null, int $depth = 0): array
{
$options = [];
foreach ($this->childIds($rootId) as $id) {
$options[$id] = str_repeat('— ', $depth).$this->get($id)['title'];
$options += $this->options($id, $depth + 1);
}
return $options;
}
/**
* @return array{pages: array<int, array<string, mixed>>, children: array<int, list<int>>}
*/
private function data(): array
{
return $this->data ??= Cache::rememberForever(self::CACHE_KEY, function () {
$audiences = Audiences::allOfType('page');
$pages = [];
$children = [];
$rows = DB::table('pages')->orderBy('sort')->orderBy('title')
->get(['id', 'parent_id', 'title', 'path', 'sort', 'in_navigation', 'published_at']);
foreach ($rows as $row) {
$pages[$row->id] = [
'id' => (int) $row->id,
'parent_id' => $row->parent_id !== null ? (int) $row->parent_id : null,
'title' => $row->title,
'path' => $row->path,
'sort' => (int) $row->sort,
'in_navigation' => (bool) $row->in_navigation,
'published' => $row->published_at !== null,
'audience' => $audiences[$row->id] ?? null,
];
$children[(int) ($row->parent_id ?? 0)][] = (int) $row->id;
}
return ['pages' => $pages, 'children' => $children];
});
}
}