Files
fewo-stommer/app/Models/Site.php
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

180 lines
5.1 KiB
PHP

<?php
namespace App\Models;
use App\Models\Concerns\BelongsToTenant;
use Carbon\CarbonInterface;
use Database\Factories\SiteFactory;
use DateInterval;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\URL;
/**
* A website: domain, theme with its settings, pages and appearances, and the properties it shows.
* Today there is one site; more sites allow other landlords to use the system under their own domain.
*/
#[Fillable(['name', 'domain', 'is_default', 'theme', 'theme_settings', 'locale'])]
class Site extends Model
{
/** @use HasFactory<SiteFactory> */
use BelongsToTenant, HasFactory;
protected static function booted(): void
{
static::saved(function (Site $site): void {
$site->syncDomainHost();
});
}
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'is_default' => 'boolean',
'theme_settings' => 'array',
];
}
/**
* @return HasMany<SiteDomain, $this>
*/
public function domains(): HasMany
{
return $this->hasMany(SiteDomain::class);
}
/**
* Scheme and host for guest links. Empty while the website has no hostname yet.
*/
public function origin(): ?string
{
$host = $this->domains()->value('host') ?: $this->domain;
if (! is_string($host) || $host === '') {
return null;
}
$scheme = in_array($host, ['localhost', '127.0.0.1'], true) ? 'http' : 'https';
return $scheme.'://'.$host;
}
/**
* @param array<string, mixed> $parameters
*/
public function temporarySignedRoute(string $name, DateTimeInterface|DateInterval|int $expiration, array $parameters = []): string
{
return $this->withOrigin(fn (): string => URL::temporarySignedRoute($name, $expiration, $parameters));
}
public function signedRoute(string $name, mixed $parameters = []): string
{
return $this->withOrigin(fn (): string => URL::signedRoute($name, $parameters));
}
/**
* Keep the single domain field and the host table on the same hostname.
*/
public function syncDomainHost(): void
{
if (! $this->wasRecentlyCreated && ! $this->wasChanged('domain')) {
return;
}
$previous = $this->getOriginal('domain');
if (is_string($previous) && $previous !== '' && $previous !== $this->domain) {
$this->domains()->where('host', mb_strtolower($previous))->delete();
}
if (! is_string($this->domain) || $this->domain === '') {
return;
}
SiteDomain::query()->updateOrCreate(
['host' => mb_strtolower($this->domain)],
['site_id' => $this->id],
);
}
/**
* @param callable(): string $callback
*/
private function withOrigin(callable $callback): string
{
$origin = $this->origin();
if ($origin === null) {
return $callback();
}
$generator = app('url');
$previousRoot = (fn () => $this->forcedRoot)->call($generator);
$previousScheme = (fn () => $this->forceScheme)->call($generator);
URL::forceRootUrl($origin);
URL::forceScheme(parse_url($origin, PHP_URL_SCHEME) ?: 'https');
try {
return $callback();
} finally {
URL::forceRootUrl($previousRoot);
URL::forceScheme(is_string($previousScheme) ? strstr($previousScheme, ':', true) : null);
}
}
/**
* @return HasMany<Page, $this>
*/
public function pages(): HasMany
{
return $this->hasMany(Page::class)->orderBy('sort_order');
}
/**
* @return HasMany<Appearance, $this>
*/
public function appearances(): HasMany
{
return $this->hasMany(Appearance::class);
}
/**
* @return HasMany<Property, $this>
*/
public function properties(): HasMany
{
return $this->hasMany(Property::class);
}
/**
* The property sections show when they do not name one (the first active property).
*/
public function primaryProperty(): ?Property
{
return $this->properties()->where('is_active', true)->orderBy('id')->first()
?? Property::query()->where('is_active', true)->whereNull('site_id')->orderBy('id')->first();
}
/**
* The appearance in effect on $date: the active seasonal one with the highest priority, else the default.
*/
public function appearanceOn(CarbonInterface $date): ?Appearance
{
$appearances = $this->appearances()->where('is_active', true)->get();
return $appearances
->filter(fn (Appearance $appearance): bool => $appearance->covers($date))
->sortByDesc('priority')
->first()
?? $appearances->firstWhere('is_default', true);
}
}