Ein Foto gehoert zu keinem (ganzjaehrig) oder mehreren Erscheinungsbildern und erscheint nur dort, vor den ganzjaehrigen Fotos. Bausteine waehlen "automatisch" oder eine eigene Auswahl; Titelbereich und Abschluss zeigen das erste passende Foto ihrer Liste. Titelbild und "Bilder je Baustein" des Erscheinungsbilds werden in die Bausteine uebernommen und entfallen dort. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
218 lines
6.9 KiB
PHP
218 lines
6.9 KiB
PHP
<?php
|
||
|
||
namespace App\Themes;
|
||
|
||
use App\ImageWorlds\ImageWorldManifest;
|
||
use App\ImageWorlds\MotifRenderer;
|
||
use App\Models\Appearance;
|
||
use App\Models\Page;
|
||
use App\Models\Photo;
|
||
use App\Models\Property;
|
||
use App\Models\Site;
|
||
use Illuminate\Support\Collection;
|
||
use Illuminate\Support\Facades\View;
|
||
use Illuminate\Support\HtmlString;
|
||
|
||
/**
|
||
* The theme context of the current request: site, theme chain and appearance. Activating it registers the
|
||
* view namespace "theme::" with the theme's own views first, then its parents, then the base theme.
|
||
*/
|
||
class Theme
|
||
{
|
||
private ?Property $property = null;
|
||
|
||
private bool $propertyResolved = false;
|
||
|
||
/** @var array<string, Collection<int, Page>> */
|
||
private array $menus = [];
|
||
|
||
public function __construct(
|
||
private ThemeRegistry $registry,
|
||
public readonly Site $site,
|
||
public readonly ?Appearance $appearance,
|
||
public readonly bool $isPreview = false,
|
||
) {}
|
||
|
||
public function activate(): self
|
||
{
|
||
$paths = array_map(fn (ThemeManifest $theme): string => $theme->viewsPath(), $this->chain());
|
||
View::replaceNamespace('theme', $paths);
|
||
View::getFinder()->flush();
|
||
|
||
return $this;
|
||
}
|
||
|
||
public function manifest(): ThemeManifest
|
||
{
|
||
return $this->registry->get($this->site->theme ?: config('themes.base'));
|
||
}
|
||
|
||
/**
|
||
* @return list<ThemeManifest>
|
||
*/
|
||
public function chain(): array
|
||
{
|
||
return $this->registry->chain($this->manifest()->key);
|
||
}
|
||
|
||
/**
|
||
* Colour tokens: theme defaults (base first, then children), site settings, appearance.
|
||
*
|
||
* @return array<string, string>
|
||
*/
|
||
public function tokens(): array
|
||
{
|
||
$tokens = [];
|
||
foreach (array_reverse($this->chain()) as $theme) {
|
||
foreach ($theme->tokens as $key => $token) {
|
||
$tokens[$key] = $token['default'];
|
||
}
|
||
}
|
||
|
||
$overrides = [...($this->site->theme_settings['tokens'] ?? []), ...($this->appearance?->tokens ?? [])];
|
||
foreach ($overrides as $key => $value) {
|
||
if (isset($tokens[$key]) && is_string($value) && preg_match('/^#[0-9a-fA-F]{3,8}$/', $value)) {
|
||
$tokens[$key] = $value;
|
||
}
|
||
}
|
||
|
||
return $tokens;
|
||
}
|
||
|
||
/**
|
||
* ":root{--ground:#F6F2EA;…}" for an inline <style> in the layout.
|
||
*/
|
||
public function cssVariables(): string
|
||
{
|
||
return ':root{'.collect($this->tokens())->map(fn (string $value, string $key): string => "--{$key}:{$value}")->implode(';').'}';
|
||
}
|
||
|
||
/**
|
||
* The appearance's replacement for a text, else $fallback.
|
||
*/
|
||
public function text(string $key, ?string $fallback = null): ?string
|
||
{
|
||
return $this->appearance?->override($key) ?? $fallback;
|
||
}
|
||
|
||
/**
|
||
* Picture for link previews (social media, messengers) and search engines: the first photo of the page's
|
||
* title area that fits the season, else an automatic photo of the apartment.
|
||
*
|
||
* @param list<array<string, mixed>> $sections the page's placed sections
|
||
*/
|
||
public function sharePhoto(array $sections = []): ?Photo
|
||
{
|
||
$chosen = collect($sections)
|
||
->filter(fn (array $section): bool => ($section['type'] ?? null) === 'hero')
|
||
->flatMap(fn (array $section): array => (array) ($section['data']['photo_ids'] ?? []))
|
||
->map(fn ($id): int => (int) $id)
|
||
->all();
|
||
$photos = Photo::query()->published()->whereKey($chosen)->with('media')->get()->keyBy('id');
|
||
$first = collect($chosen)->map(fn (int $id): ?Photo => $photos->get($id))
|
||
->first(fn (?Photo $photo): bool => $photo?->fitsSeason($this->appearance) ?? false);
|
||
|
||
return $first ?? Photo::automatic($this->property(), $this->appearance)->first();
|
||
}
|
||
|
||
public function appearanceKey(): string
|
||
{
|
||
return $this->appearance ? str($this->appearance->name)->slug()->toString() : 'standard';
|
||
}
|
||
|
||
/**
|
||
* A brand graphic of this website (logo, favicon, document logo), or null when none was uploaded.
|
||
* The brand belongs to the landlord; the picture world belongs to the look. Templates that fall back
|
||
* to their own graphic should ask for this first (A14/A22).
|
||
*/
|
||
public function brand(string $collection): ?string
|
||
{
|
||
return $this->site->exists ? $this->site->brandUrl($collection) : null;
|
||
}
|
||
|
||
/**
|
||
* The graphic behind a picture-world slot (divider, closing, vignette.beach …). Templates ask for a
|
||
* slot, never for a file, so the same template works in every picture world (A18/A19).
|
||
*/
|
||
public function motif(string $slot, ?string $class = null, ?string $label = null): HtmlString
|
||
{
|
||
return app(MotifRenderer::class)->render($slot, $class, $label);
|
||
}
|
||
|
||
/**
|
||
* Whether the current picture world fills this slot – for templates that want to leave out a whole
|
||
* decorative block instead of rendering an empty one.
|
||
*/
|
||
public function hasMotif(string $slot): bool
|
||
{
|
||
return app(MotifRenderer::class)->has($slot);
|
||
}
|
||
|
||
public function imageWorld(): ?ImageWorldManifest
|
||
{
|
||
return app(MotifRenderer::class)->world();
|
||
}
|
||
|
||
public function property(): ?Property
|
||
{
|
||
if (! $this->propertyResolved) {
|
||
$this->property = $this->site->exists ? $this->site->primaryProperty() : Property::query()->where('is_active', true)->first();
|
||
$this->propertyResolved = true;
|
||
}
|
||
|
||
return $this->property;
|
||
}
|
||
|
||
/**
|
||
* Run a section with its property, restoring the previous cache even when rendering fails.
|
||
*
|
||
* @template T
|
||
*
|
||
* @param callable(): T $callback
|
||
* @return T
|
||
*/
|
||
public function runForProperty(?Property $property, callable $callback): mixed
|
||
{
|
||
$previousProperty = $this->property;
|
||
$previousResolved = $this->propertyResolved;
|
||
$this->property = $property;
|
||
$this->propertyResolved = true;
|
||
|
||
try {
|
||
return $callback();
|
||
} finally {
|
||
$this->property = $previousProperty;
|
||
$this->propertyResolved = $previousResolved;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Published pages of a menu location ("main" or "footer").
|
||
*
|
||
* @return Collection<int, Page>
|
||
*/
|
||
public function menu(string $location): Collection
|
||
{
|
||
return $this->menus[$location] ??= $this->site->exists
|
||
? $this->site->pages()->where('is_published', true)->where('menu_location', $location)->get()
|
||
: collect();
|
||
}
|
||
|
||
/**
|
||
* Vite entry points of the active theme (a child theme without own assets uses its parent's).
|
||
*
|
||
* @return list<string>
|
||
*/
|
||
public function assets(): array
|
||
{
|
||
foreach ($this->chain() as $theme) {
|
||
$assets = array_values(array_filter([$theme->assets['css'] ?? null, $theme->assets['js'] ?? null]));
|
||
if ($assets !== []) {
|
||
return $assets;
|
||
}
|
||
}
|
||
|
||
return [];
|
||
}
|
||
}
|