- Auffangroute und Pfadprüfung nehmen beliebig tiefe Adressen (/kurse/kinder) an; die Elternseite muss nicht existieren. - ReservedPaths sperrt jeden ersten Pfadteil fester Routen und des Ordners public/ (zur Laufzeit aus dem Router); frei bleiben nur /, /shop und /angebote. Eine bestehende Seite mit unverändertem Pfad bleibt speicherbar. - Menü: Eintrag im aktiven Pfad, wenn die aktuelle Seite unter seinem Ziel liegt. - Verwaltung: Hilfetext und Hinweis am Adressfeld, Sortierung nach Pfadteilen in Webpräsenz und Seitenauswahlen, Chip "Unterseite von ...". - ShopSwitchTest: Ein POST auf die entfernte Kaufadresse ergibt nun 405 statt 404, weil die Auffangroute die mehrteilige Adresse trifft (bei einteiligen Adressen schon bisher so). Keine Bestellung entsteht weiterhin; Begründung in ADR-72. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
118 lines
3.9 KiB
PHP
118 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Cms;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
/**
|
|
* Baut Kopf- und Fußnavigation aus dem CMS-Navigationsmodell. Keine fest codierten Menüpunkte.
|
|
*
|
|
* Die Ausgabe hängt nur von der Navigation und dem angefragten Pfad ab, nie vom Anmeldestatus,
|
|
* damit öffentliche Seiten für alle Besucher gleich ausgeliefert werden können (ADR-32).
|
|
*/
|
|
final class SiteNavigation
|
|
{
|
|
/**
|
|
* @param Collection<int, NavigationItem> $items sichtbare Einträge, sortiert
|
|
*/
|
|
public function __construct(private readonly Collection $items, private readonly string $currentPath) {}
|
|
|
|
/**
|
|
* @param Collection<int, NavigationItem> $items
|
|
*/
|
|
public static function forPath(Collection $items, string $path): self
|
|
{
|
|
return new self($items, '/'.trim($path, '/'));
|
|
}
|
|
|
|
/**
|
|
* Hauptnavigation mit einer Ebene Untermenü. Untereinträge eines ausgeblendeten Eintrags sind ebenfalls verborgen.
|
|
*
|
|
* @return list<array{id: int, label: string, href: ?string, current: bool, activeTrail: bool, children: list<array{id: int, label: string, href: string, current: bool}>}>
|
|
*/
|
|
public function header(): array
|
|
{
|
|
$topLevel = $this->items->where('placement', 'header')->whereNull('parent_id');
|
|
$entries = [];
|
|
|
|
foreach ($topLevel as $item) {
|
|
$children = [];
|
|
$childBelow = false;
|
|
|
|
foreach ($this->items->where('parent_id', $item->id) as $child) {
|
|
$href = $child->href();
|
|
if ($href !== null) {
|
|
$childBelow = $childBelow || $this->isBelow($child);
|
|
$children[] = [
|
|
'id' => $child->id,
|
|
'label' => $child->label,
|
|
'href' => $href,
|
|
'current' => $this->isCurrent($child),
|
|
];
|
|
}
|
|
}
|
|
|
|
$href = $item->href();
|
|
if ($href === null && $children === []) {
|
|
continue;
|
|
}
|
|
|
|
$current = $href !== null && $this->isCurrent($item);
|
|
|
|
$entries[] = [
|
|
'id' => $item->id,
|
|
'label' => $item->label,
|
|
'href' => $href,
|
|
'current' => $current,
|
|
'activeTrail' => $current || $childBelow || in_array(true, array_column($children, 'current'), true) || ($href !== null && $this->isBelow($item)),
|
|
'children' => $children,
|
|
];
|
|
}
|
|
|
|
return $entries;
|
|
}
|
|
|
|
/**
|
|
* Spalten des Fußbereichs in fester Reihenfolge; Gruppen ohne Einträge entfallen.
|
|
*
|
|
* @return list<array{key: string, heading: string, links: list<array{label: string, href: string, current: bool}>}>
|
|
*/
|
|
public function footerGroups(): array
|
|
{
|
|
$groups = [];
|
|
|
|
foreach (NavigationItem::FOOTER_GROUPS as $placement => $heading) {
|
|
$links = [];
|
|
|
|
foreach ($this->items->where('placement', $placement) as $item) {
|
|
$href = $item->href();
|
|
if ($href !== null) {
|
|
$links[] = ['label' => $item->label, 'href' => $href, 'current' => $this->isCurrent($item)];
|
|
}
|
|
}
|
|
|
|
if ($links !== []) {
|
|
$groups[] = ['key' => $placement, 'heading' => $heading, 'links' => $links];
|
|
}
|
|
}
|
|
|
|
return $groups;
|
|
}
|
|
|
|
private function isCurrent(NavigationItem $item): bool
|
|
{
|
|
return $item->targetPath() === $this->currentPath;
|
|
}
|
|
|
|
/**
|
|
* Die aktuelle Seite liegt unterhalb des Ziels ("/kurse" → "/kurse/kinder"), ganze Pfadteile. Die Startseite ist
|
|
* ausgenommen, sonst läge jede Seite unter ihr.
|
|
*/
|
|
private function isBelow(NavigationItem $item): bool
|
|
{
|
|
$target = $item->targetPath();
|
|
|
|
return $target !== null && $target !== '/' && PagePath::isDescendantOf($this->currentPath, $target);
|
|
}
|
|
}
|