From 04a9a4f83fb9b87a2264d4db6c71acfdb1100136 Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 27 Nov 2026 07:05:22 +0100 Subject: [PATCH] =?UTF-8?q?Lass=20Men=C3=BCeintr=C3=A4ge=20auf=20eine=20Sp?= =?UTF-8?q?rungmarke=20der=20Zielseite=20zeigen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Zweiter Schritt in „Ziel ändern“, neue Spalte navigation_items.anchor (nullable, abwärtskompatibel). Co-Authored-By: Claude Sonnet 5.5 --- app/Domain/Cms/NavigationItem.php | 6 +- app/Filament/Pages/Webpraesenz.php | 71 +++++++- ..._100000_add_anchor_to_navigation_items.php | 26 +++ .../views/filament/cms/ziel-auswahl.blade.php | 26 ++- tests/Feature/WebpraesenzAnchorTest.php | 152 ++++++++++++++++++ 5 files changed, 274 insertions(+), 7 deletions(-) create mode 100644 database/migrations/2026_12_02_100000_add_anchor_to_navigation_items.php create mode 100644 tests/Feature/WebpraesenzAnchorTest.php diff --git a/app/Domain/Cms/NavigationItem.php b/app/Domain/Cms/NavigationItem.php index c0b8ec3..acf2dfa 100644 --- a/app/Domain/Cms/NavigationItem.php +++ b/app/Domain/Cms/NavigationItem.php @@ -2,6 +2,7 @@ namespace App\Domain\Cms; +use App\Domain\Cms\Sections\PageSections; use App\Domain\Cms\Widgets\Support\SafeUrl; use App\Domain\Commerce\Shop; use Illuminate\Database\Eloquent\Builder; @@ -32,6 +33,7 @@ class NavigationItem extends Model 'label', 'page_id', 'url', + 'anchor', 'placement', 'sort_order', 'is_visible', @@ -86,7 +88,9 @@ public function href(): ?string } if ($this->page !== null) { - return $this->page->path === '/' ? url('/') : url($this->page->path); + $anchor = PageSections::cleanAnchor($this->anchor); + + return ($this->page->path === '/' ? url('/') : url($this->page->path)).($anchor !== null ? '#'.$anchor : ''); } if ($this->url === null || $this->url === '' || ! SafeUrl::isSafe($this->url)) { diff --git a/app/Filament/Pages/Webpraesenz.php b/app/Filament/Pages/Webpraesenz.php index ae4a153..79e48e8 100644 --- a/app/Filament/Pages/Webpraesenz.php +++ b/app/Filament/Pages/Webpraesenz.php @@ -2,9 +2,11 @@ namespace App\Filament\Pages; +use App\Domain\Cms\LinkTargets; use App\Domain\Cms\NavigationItem; use App\Domain\Cms\Page as CmsPage; use App\Domain\Cms\PageService; +use App\Domain\Cms\Sections\PageSections; use App\Domain\Cms\Widgets\Support\SafeUrl; use App\Filament\Resources\Pages\PageResource; use App\Filament\Support\Bestand; @@ -74,7 +76,7 @@ class Webpraesenz extends Page /** Ausgewählter Eintrag (Schlüssel „e{id}“ oder „n{zähler}“ für neue Einträge). */ public ?string $auswahl = null; - /** @var array */ + /** @var array */ public array $eintraege = []; /** Globale Reihenfolge der Schlüssel; entspricht sort_order. @var list */ @@ -103,6 +105,9 @@ class Webpraesenz extends Page /** Eintrag, dessen Ziel gerade gewählt wird. */ public ?string $zielwahl = null; + /** Zweiter Schritt der Zielwahl: gewählte Seite, deren Sprungmarke noch zu wählen ist. */ + public ?int $zielSeite = null; + public string $zielSuche = ''; public static function canAccess(): bool @@ -327,6 +332,7 @@ public function ladeMenues(): void 'label' => (string) $item->label, 'page_id' => $item->page_id, 'url' => (string) ($item->url ?? ''), + 'anchor' => (string) ($item->anchor ?? ''), 'placement' => (string) $item->placement, 'parent' => $item->parent_id !== null ? 'e'.$item->parent_id : null, 'is_visible' => (bool) $item->is_visible, @@ -364,6 +370,7 @@ private function bereichsStaende(): array trim($e['label']), $e['page_id'] !== null ? (int) $e['page_id'] : null, trim($e['url']), + trim($e['anchor'] ?? ''), (bool) $e['is_visible'], $this->elternVon($k), ]; @@ -562,6 +569,7 @@ public function eintragHinzufuegen(): void 'label' => '', 'page_id' => null, 'url' => '', + 'anchor' => '', 'placement' => $this->menue, 'parent' => null, 'is_visible' => true, @@ -595,7 +603,7 @@ public function updatedEintraege(mixed $value, string $pfad): void match ($feld) { 'placement' => $this->bereichGeaendert($k), 'parent' => $this->elternGeaendert($k), - 'page_id' => $this->eintraege[$k]['page_id'] = blank($value) ? null : (int) $value, + 'page_id' => $this->seiteGeaendert($k, $value), 'is_visible' => $this->eintraege[$k]['is_visible'] = (bool) $value, default => null, }; @@ -673,10 +681,18 @@ private function ansEnde(string $k): void $this->folge = [...array_values(array_filter($this->folge, fn (string $x): bool => $x !== $k)), $k]; } + /** Eine andere Zielseite hat eigene Sprungmarken: Die bisherige Marke entfällt. */ + private function seiteGeaendert(string $k, mixed $value): void + { + $this->eintraege[$k]['page_id'] = blank($value) ? null : (int) $value; + $this->eintraege[$k]['anchor'] = ''; + } + public function zielAdresseNutzen(string $k): void { if (isset($this->eintraege[$k])) { $this->eintraege[$k]['page_id'] = null; + $this->eintraege[$k]['anchor'] = ''; $this->js("requestAnimationFrame(() => document.getElementById('menue-adresse')?.focus())"); } } @@ -692,6 +708,7 @@ public function oeffneZielwahl(string $k): void public function schliesseZielwahl(): void { $this->zielwahl = null; + $this->zielSeite = null; } public function waehleZiel(int $seiteId): void @@ -702,8 +719,48 @@ public function waehleZiel(int $seiteId): void $this->eintraege[$this->zielwahl]['page_id'] = $seiteId; $this->eintraege[$this->zielwahl]['url'] = ''; + $this->eintraege[$this->zielwahl]['anchor'] = ''; $this->resetErrorBag("eintraege.{$this->zielwahl}.url"); - $this->zielwahl = null; + + // Hat die Seite Sprungmarken, folgt als zweiter Schritt die Wahl der Marke; sonst ist die Auswahl fertig. + if ($this->zielMarken($seiteId) !== []) { + $this->zielSeite = $seiteId; + + return; + } + + $this->schliesseZielwahl(); + } + + /** + * Zweiter Schritt von „Ziel ändern“: Sprungmarke der gewählten Seite übernehmen (leer = Seitenanfang). + */ + public function waehleMarke(?string $marke = null): void + { + if ($this->zielwahl === null || $this->zielSeite === null || ! isset($this->eintraege[$this->zielwahl])) { + return; + } + + $marke = filled($marke) ? $marke : null; + + if ($marke !== null && ! in_array($marke, array_column($this->zielMarken($this->zielSeite), 'anchor'), true)) { + return; + } + + $this->eintraege[$this->zielwahl]['anchor'] = $marke ?? ''; + $this->schliesseZielwahl(); + } + + /** + * Sprungmarken einer Seite für den zweiten Schritt. + * + * @return list + */ + public function zielMarken(int $seiteId): array + { + $seite = $this->seitenCache()->get($seiteId); + + return $seite === null ? [] : app(LinkTargets::class)->anchorsForPage($seite); } /** @@ -755,7 +812,7 @@ public function zielVon(string $k): array return $seite === null ? ['art' => 'fehlt', 'titel' => null, 'pfad' => null, 'zustand' => ['label' => 'Zielseite nicht mehr vorhanden', 'ton' => 'achtung'], 'seite' => null] - : ['art' => 'seite', 'titel' => $seite->latestRevision?->contentTitle() ?: $seite->path, 'pfad' => $seite->path, 'zustand' => $this->seitenZustand($seite), 'seite' => $seite]; + : ['art' => 'seite', 'titel' => $seite->latestRevision?->contentTitle() ?: $seite->path, 'pfad' => $seite->path.(trim($e['anchor'] ?? '') !== '' ? '#'.trim($e['anchor']) : ''), 'zustand' => $this->seitenZustand($seite), 'seite' => $seite]; } if (trim($e['url']) !== '') { @@ -919,6 +976,7 @@ private function pruefen(): array || $alt->label !== $e['label'] || $alt->page_id !== ($e['page_id'] !== null ? (int) $e['page_id'] : null) || (string) ($alt->url ?? '') !== $e['url'] + || (string) ($alt->anchor ?? '') !== trim($e['anchor'] ?? '') || $alt->placement !== $e['placement'] || ($alt->parent_id !== null ? 'e'.$alt->parent_id : null) !== $this->elternVon($k) || (bool) $alt->is_visible !== (bool) $e['is_visible'] @@ -936,16 +994,19 @@ private function pruefen(): array 'placement' => $e['placement'], 'page_id' => $e['page_id'], 'url' => trim($e['url']) === '' ? null : trim($e['url']), + 'anchor' => trim($e['anchor'] ?? '') === '' ? null : trim($e['anchor']), ], [ 'label' => ['required', 'string', 'max:80'], 'placement' => ['required', 'in:'.implode(',', array_keys(self::BEREICHE))], 'page_id' => ['nullable', 'integer', 'exists:pages,id'], 'url' => ['nullable', 'string', 'max:500', SafeUrl::rule()], + 'anchor' => ['nullable', 'string', 'max:'.PageSections::MAX_ANCHOR, 'regex:'.PageSections::ANCHOR_PATTERN], ], [ 'label.required' => 'Bitte einen Menütext eingeben.', 'label.max' => 'Der Menütext hat höchstens 80 Zeichen.', 'url.max' => 'Die Adresse hat höchstens 500 Zeichen.', 'page_id.exists' => 'Diese Seite gibt es nicht mehr.', + 'anchor.regex' => 'Die Sprungmarke ist ungültig. Bitte über „Ziel ändern“ eine vorhandene wählen.', ]); $meldungen = $validator->errors()->toArray(); @@ -1044,6 +1105,8 @@ private function schreiben(array $geaendert): array 'label' => trim($e['label']), 'page_id' => $e['page_id'] !== null ? (int) $e['page_id'] : null, 'url' => trim($e['url']) === '' ? null : trim($e['url']), + // Eine Sprungmarke gehört zur Zielseite; ohne Seite (freie Adresse) entfällt sie. + 'anchor' => $e['page_id'] !== null && trim($e['anchor'] ?? '') !== '' ? trim($e['anchor']) : null, 'placement' => $e['placement'], 'parent_id' => $eltern !== null ? ($ids[$eltern] ?? $this->eintraege[$eltern]['id']) : null, 'is_visible' => (bool) $e['is_visible'], diff --git a/database/migrations/2026_12_02_100000_add_anchor_to_navigation_items.php b/database/migrations/2026_12_02_100000_add_anchor_to_navigation_items.php new file mode 100644 index 0000000..4ce569c --- /dev/null +++ b/database/migrations/2026_12_02_100000_add_anchor_to_navigation_items.php @@ -0,0 +1,26 @@ +string('anchor', 60)->nullable(); + }); + } + + public function down(): void + { + Schema::table('navigation_items', function (Blueprint $table) { + $table->dropColumn('anchor'); + }); + } +}; diff --git a/resources/views/filament/cms/ziel-auswahl.blade.php b/resources/views/filament/cms/ziel-auswahl.blade.php index 1e89d69..53b0b99 100644 --- a/resources/views/filament/cms/ziel-auswahl.blade.php +++ b/resources/views/filament/cms/ziel-auswahl.blade.php @@ -3,6 +3,9 @@ $e = $this->eintraege[$this->zielwahl]; $seiten = $this->zielSeiten(); $aktuell = $e['page_id'] !== null ? (int) $e['page_id'] : null; + $zweiter = $this->zielSeite !== null; + $marken = $zweiter ? $this->zielMarken($this->zielSeite) : []; + $aktuelleMarke = trim($e['anchor'] ?? ''); @endphp