Zweistufig (Seite, dann Sprungmarke) mit Veröffentlichungszustand und den noch ungespeicherten Marken der bearbeiteten Seite. Schreibt nur die Adresse ins Feld. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
132 lines
4.9 KiB
PHP
132 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Cms;
|
|
|
|
use App\Domain\Cms\Sections\PageSections;
|
|
|
|
/**
|
|
* Interne Linkziele für die Auswahl im CMS: Seiten mit ihrem Veröffentlichungszustand und die Sprungmarken ihrer
|
|
* Abschnitte. Rein lesend; das Ergebnis ist eine Adresse, die in ein Linkfeld oder einen Menüeintrag geschrieben wird.
|
|
*
|
|
* Adressformen: „/pfad“, „/pfad#marke“ (andere Seite) und „#marke“ (dieselbe Seite). Sie bestehen SafeUrl.
|
|
*/
|
|
class LinkTargets
|
|
{
|
|
public function __construct(private readonly PageSections $sections) {}
|
|
|
|
/**
|
|
* Seiten für die Auswahl, nach Adresse sortiert. Suche in Titel und Adresse.
|
|
*
|
|
* @return list<array{id: int, titel: string, pfad: string, zustand: array{label: string, ton: string}, marken: int}>
|
|
*/
|
|
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<array{anchor: string, label: string, hidden: bool, published: bool}>
|
|
*/
|
|
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<array<string, mixed>> $sections Abschnitte im Modell (nach SectionEditor::fromEditorState)
|
|
* @return list<array{anchor: string, label: string, hidden: bool, published: bool}>
|
|
*/
|
|
public function anchorsInEditor(array $sections): array
|
|
{
|
|
return array_map(fn (array $anchor): array => $anchor + ['published' => false], $this->anchorsOf($sections));
|
|
}
|
|
|
|
/**
|
|
* @param list<array<string, mixed>> $sections
|
|
* @return list<array{anchor: string, label: string, hidden: bool}>
|
|
*/
|
|
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 : '');
|
|
}
|
|
}
|