Files
laraundfelix/app/Domain/Cms/NavigationItem.php
T
LarsandClaude Sonnet 5.5 04a9a4f83f Lass Menüeinträge auf eine Sprungmarke der Zielseite zeigen
Zweiter Schritt in „Ziel ändern“, neue Spalte navigation_items.anchor (nullable, abwärtskompatibel).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 07:05:22 +01:00

143 lines
3.9 KiB
PHP

<?php
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;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class NavigationItem extends Model
{
/**
* Plätze im globalen Website-Gerüst. Die drei Fußbereichsgruppen ersetzen eine eigene Footer-Verwaltung.
*/
public const PLACEMENTS = [
'header' => 'Kopfzeile (Hauptnavigation)',
'footer' => 'Fußbereich: Service',
'footer_contact' => 'Fußbereich: Kontakt',
'footer_legal' => 'Fußbereich: Rechtliches',
];
/** Spaltenüberschriften im Fußbereich. */
public const FOOTER_GROUPS = [
'footer' => 'Service',
'footer_contact' => 'Kontakt',
'footer_legal' => 'Rechtliches',
];
protected $fillable = [
'label',
'page_id',
'url',
'anchor',
'placement',
'sort_order',
'is_visible',
'parent_id',
];
protected static function booted(): void
{
// Untermenüs gibt es nur in der Hauptnavigation.
static::saving(function (NavigationItem $item): void {
if ($item->placement !== 'header' && $item->getAttribute('parent_id') !== null) {
$item->parent_id = null;
}
});
}
protected function casts(): array
{
return [
'is_visible' => 'boolean',
];
}
public function page(): BelongsTo
{
return $this->belongsTo(Page::class);
}
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
}
public function children(): HasMany
{
return $this->hasMany(self::class, 'parent_id')->orderBy('sort_order')->orderBy('id');
}
/**
* @param Builder<NavigationItem> $query
*/
public function scopeVisible(Builder $query): void
{
$query->where('is_visible', true);
}
public function href(): ?string
{
// Ein Menüpunkt in den Shop erscheint nicht, solange der Shop nicht sichtbar ist: Er führte ins Leere (404).
if ($this->pointsIntoShop() && ! Shop::visible()) {
return null;
}
if ($this->page !== null) {
$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)) {
return null;
}
if (str_starts_with($this->url, '/')) {
return url($this->url);
}
return $this->url;
}
/** Ziel ist der Shop selbst oder eine seiner Seiten: Übersicht, Produktseiten, Hinweisseite, Warenkorb, Kasse. */
private function pointsIntoShop(): bool
{
$path = $this->targetPath();
if ($path === null) {
return false;
}
foreach (['/shop', '/warenkorb', '/kasse', '/kauf-noch-nicht-moeglich'] as $prefix) {
if ($path === $prefix || str_starts_with($path, $prefix.'/')) {
return true;
}
}
return false;
}
/**
* Pfad des Ziels innerhalb der Website (für die Kennzeichnung des aktiven Eintrags); null bei externen Zielen.
*/
public function targetPath(): ?string
{
if ($this->page !== null) {
return $this->page->path;
}
if (is_string($this->url) && str_starts_with($this->url, '/') && ! str_starts_with($this->url, '//')) {
$path = parse_url($this->url, PHP_URL_PATH);
return is_string($path) ? '/'.trim($path, '/') : null;
}
return null;
}
}