>, children: array>}|null */ private ?array $data = null; /** @var array> */ 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|null */ public function get(int $id): ?array { return $this->data()['pages'][$id] ?? null; } /** * @return list */ public function childIds(?int $id): array { return $this->data()['children'][$id ?? 0] ?? []; } /** * @param list $ids * @return list */ 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> */ 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}> */ 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 */ 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 */ 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>, children: array>} */ 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]; }); } }