*/ public function pages(string $search = '', ?int $exceptId = null): array { $needle = mb_strtolower(trim($search)); return Page::query()->with(['latestRevision', 'publishedRevision'])->orderBy('path')->get() ->reject(fn (Page $page): bool => $page->getKey() === $exceptId) ->map(fn (Page $page): array => [ 'id' => (int) $page->getKey(), 'titel' => $page->latestRevision?->contentTitle() ?: $page->path, 'pfad' => $page->path, 'zustand' => $this->state($page), 'marken' => count($this->anchorsOf($page->latestRevision?->sections() ?? [])), ]) ->filter(fn (array $page): bool => $needle === '' || str_contains(mb_strtolower($page['titel'].' '.$page['pfad']), $needle)) ->values() ->all(); } /** * @return array{label: string, ton: string} */ public function state(Page $page): array { if ($page->published_revision_id === null) { return ['label' => 'Nur Entwurf, öffentlich nicht erreichbar', 'ton' => 'offen']; } return $page->latestRevision !== null && $page->latestRevision->id !== $page->published_revision_id ? ['label' => 'Veröffentlicht · Entwurf neuer', 'ton' => 'gut'] : ['label' => 'Veröffentlicht', 'ton' => 'gut']; } /** * Sprungmarken einer gespeicherten Seite: die des neuesten Entwurfs, je mit dem Hinweis, ob sie schon * veröffentlicht sind und ob der Abschnitt ausgeblendet ist. * * @return list */ public function anchorsForPage(Page $page): array { $published = collect($this->anchorsOf($page->publishedRevision?->sections() ?? [])) ->filter(fn (array $anchor): bool => ! $anchor['hidden']) ->pluck('anchor') ->all(); return array_map( fn (array $anchor): array => $anchor + ['published' => in_array($anchor['anchor'], $published, true)], $this->anchorsOf($page->latestRevision?->sections() ?? $page->publishedRevision?->sections() ?? []), ); } /** * Sprungmarken der Abschnitte im Editor, auch ungespeicherte. Für „diese Seite“. * * @param list> $sections Abschnitte im Modell (nach SectionEditor::fromEditorState) * @return list */ public function anchorsInEditor(array $sections): array { return array_map(fn (array $anchor): array => $anchor + ['published' => false], $this->anchorsOf($sections)); } /** * @param list> $sections * @return list */ public function anchorsOf(array $sections): array { $anchors = []; foreach (array_values($sections) as $position => $section) { $anchor = PageSections::cleanAnchor($section['anchor'] ?? null); if ($anchor === null) { continue; } $name = PageSections::cleanName($section['name'] ?? null); $anchors[] = [ 'anchor' => $anchor, 'label' => $name ?? 'Abschnitt '.($position + 1), 'hidden' => PageSections::isHidden($section['hidden'] ?? false), ]; } return $anchors; } /** * Adresse für ein Linkfeld: „/pfad“, „/pfad#marke“ oder (dieselbe Seite) „#marke“. */ public function href(?string $path, ?string $anchor): ?string { $anchor = $anchor !== null && $anchor !== '' ? PageSections::cleanAnchor($anchor) : null; if ($path === null) { return $anchor !== null ? '#'.$anchor : null; } return $path.($anchor !== null ? '#'.$anchor : ''); } /** * Vollständige öffentliche Adresse mit Sprungmarke, für Verweise von außen. */ public function publicUrl(Page $page, ?string $anchor = null): string { return $page->publicUrl().($anchor !== null && $anchor !== '' ? '#'.$anchor : ''); } }