*/ 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 */ protected function casts(): array { return [ 'is_default' => 'boolean', 'theme_settings' => 'array', ]; } /** * @return HasMany */ 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 $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 */ public function pages(): HasMany { return $this->hasMany(Page::class)->orderBy('sort_order'); } /** * @return HasMany */ public function appearances(): HasMany { return $this->hasMany(Appearance::class); } /** * @return HasMany */ 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); } }