*/ use BelongsToTenant, HasFactory, InteractsWithMedia; 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 main host for guest links (the domain field; further addresses redirect there, see * ResolveTheme). Empty while the website has no hostname yet. */ public function origin(): ?string { $host = $this->domain ?: $this->domains()->orderBy('id')->value('host'); 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. */ /** * The brand of this website: the graphics that identify the landlord, not the decoration of the theme. * Kept per website so a second landlord never inherits the first one's logo in their contract (A14/A22). */ public const BRAND_LOGO = 'logo'; public const BRAND_FAVICON = 'favicon'; public const BRAND_DOCUMENT_LOGO = 'document_logo'; public function registerMediaCollections(): void { // The brand is shown on the public website and in mails, so it lives on the public disk. $this->addMediaCollection(self::BRAND_LOGO) ->useDisk('public') ->singleFile() ->acceptsMimeTypes(['image/svg+xml', 'image/png', 'image/webp']); $this->addMediaCollection(self::BRAND_FAVICON) ->useDisk('public') ->singleFile() ->acceptsMimeTypes(['image/svg+xml', 'image/png', 'image/x-icon']); // Mail clients and the PDF renderer do not draw SVG, so this one is a pixel image. $this->addMediaCollection(self::BRAND_DOCUMENT_LOGO) ->useDisk('public') ->singleFile() ->acceptsMimeTypes(['image/png', 'image/jpeg']); } /** * The URL of a brand graphic, or null when this website has none. */ public function brandUrl(string $collection): ?string { $media = $this->getFirstMedia($collection); return $media?->getUrl() ?: null; } /** * The absolute path of a brand graphic – mails and the contract PDF embed the file, they cannot fetch * a URL from a server that may not be reachable from where the PDF is rendered. */ public function brandPath(string $collection): ?string { $path = $this->getFirstMedia($collection)?->getPath(); return is_string($path) && is_file($path) ? $path : null; } 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); } }