*/ use HasFactory; public const HOME = 'start'; /** Menu locations a page can appear in. */ public const MENU_LOCATIONS = ['main' => 'Hauptmenü', 'footer' => 'Fußzeile', 'none' => 'Nicht im Menü']; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'is_published' => 'boolean', 'sections' => 'array', ]; } /** * @return BelongsTo */ public function site(): BelongsTo { return $this->belongsTo(Site::class); } public function isHome(): bool { return $this->slug === self::HOME; } public function url(): string { return $this->isHome() ? route('home') : route('page', ['slug' => $this->slug]); } public function label(): string { return $this->menu_label ?: $this->title; } protected static function booted(): void { static::saving(function (Page $page): void { $page->sections = self::assignSectionIds($page->sections ?? []); }); } /** * Gives every placed section a stable id (data.id) – new and duplicated sections get a fresh one – * so appearances can address sections independently of their order. * * @param list}> $sections * @return list}> */ public static function assignSectionIds(array $sections): array { $seen = []; return array_values(array_map(function (array $section) use (&$seen): array { $data = $section['data'] ?? []; $id = $data['id'] ?? null; if (! is_string($id) || $id === '' || isset($seen[$id])) { $id = (string) Str::uuid(); } $seen[$id] = true; return [...$section, 'data' => [...$data, 'id' => $id]]; }, $sections)); } }