Files
fewo-stommer/app/Models/Page.php
T
LarsandCursor 8ebc1a3878 Assign the existing site to one landlord so stommer.info can run tenancy.
Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 12:00:11 +02:00

95 lines
2.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use App\Models\Concerns\BelongsToTenant;
use Database\Factories\PageFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;
/**
* A page of a site, composed of sections from the catalogue (App\Themes\Sections).
* sections = list of {type: string, data: array} as stored by the Filament builder.
* The home page has the slug "start" and is served at "/".
*/
#[Fillable(['site_id', 'slug', 'title', 'menu_label', 'menu_location', 'sort_order', 'is_published', 'sections', 'seo_title', 'seo_description'])]
class Page extends Model
{
/** @use HasFactory<PageFactory> */
use BelongsToTenant, 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<string, string>
*/
protected function casts(): array
{
return [
'is_published' => 'boolean',
'sections' => 'array',
];
}
/**
* @return BelongsTo<Site, $this>
*/
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<array{type: string, data?: array<string, mixed>}> $sections
* @return list<array{type: string, data: array<string, mixed>}>
*/
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));
}
}