Die Theme-Sitzung wartete darauf: Seebad ruft schon ortsneutrale Plätze über `partials/motif` auf und behilft sich mit einem eigenen `motifs/`- Ordner. Der Kommentar dort sagt es selbst – „so a later picture-world system can replace the mapping below". Das System gibt es jetzt. **Bildwelten** (A18–A20) sind Pakete wie Themes: `resources/image-worlds/ <key>/` mit `world.json` und `svg/`. Eine Website wählt eine Bildwelt, eine Wohnung darf eine andere tragen – das ist der Platz für die Bergwohnung auf einer Küsten-Website. Vorlagen fragen über `$theme->motif($slot)` nach einem Platz, nie nach einer Datei. Die Render-Logik ist aus Seebads Partial in die Anwendung gewandert, damit jedes Theme sie bekommt: Inline-SVG (nimmt die Textfarbe an, kein zweiter Request), Titel und ARIA entfernt, und die internen Ids je Aufruf eindeutig gemacht – zwei Kopien derselben Grafik auf einer Seite würden sonst auf denselben Verlauf zeigen und eine davon verlöre ihre Farben. Ein leerer Platz ist kein Fehler: die Vorlage bekommt schlicht keine Dekoration. Weil das leicht zu übersehen ist, meldet `php artisan image-worlds:check`, welcher Platz ohne Datei dasteht. **Marke je Website** (A14/A22): Logo, Favicon und ein Logo für E-Mail und Vertrag als Medien der Website. Der Mietvertrag bettet die Datei ein statt eine URL zu holen – er wird auf dem Server gerendert und darf nicht davon abhängen, dass dieser sich selbst erreicht. Ohne Logo bleibt der Vertrag wie bisher. `resources/themes/**` ist unberührt: die Ostsee-Grafiken sind kopiert, nicht verschoben. Was die Theme-Sitzung nun tun kann, steht in ZUSAMMENARBEIT.md §6; ihr Wunsch aus §5 ist als erledigt vermerkt. 272 Feature-Tests grün. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
206 lines
6.2 KiB
PHP
206 lines
6.2 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;
|
||
}
|
||
|
||
public function heroPhoto(?Photo $fallback = null): ?Photo
|
||
{
|
||
return $this->appearance?->heroPhoto ?? $fallback;
|
||
}
|
||
|
||
/**
|
||
* Photo ids the active appearance sets for a slot of a placed section ("Bilder je Baustein").
|
||
*
|
||
* @return list<int>
|
||
*/
|
||
public function sectionPhotoIds(?string $sectionId, string $slot): array
|
||
{
|
||
if (blank($sectionId) || $this->appearance === null) {
|
||
return [];
|
||
}
|
||
|
||
$ids = $this->appearance->section_photos[$sectionId][$slot] ?? null;
|
||
|
||
return array_values(array_map('intval', array_filter((array) $ids)));
|
||
}
|
||
|
||
/**
|
||
* Whether seasonal photos replace the automatic photos of a section ('replace') or come first ('prepend').
|
||
*/
|
||
public function sectionPhotoMode(?string $sectionId): string
|
||
{
|
||
$mode = $sectionId ? ($this->appearance?->section_photos[$sectionId]['mode'] ?? null) : null;
|
||
|
||
return $mode === 'prepend' ? 'prepend' : 'replace';
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
/**
|
||
* 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 [];
|
||
}
|
||
}
|