Files
Lars StommerandClaude Opus 5.5 7d0bc93abc Offers by their own rules, seasonal photos per section, theme authoring tooling
- Special offers: kinds (rule, fixed dates, gap filler), package price, priority, highlight;
  GapFinder detects calendar gaps (optionally waiving the minimum stay); OfferShowcase decides
  what the website advertises and prices fixed dates/gaps with PriceCalculator; offers section
  shows them side by side, as a list or rotating; hero can show the top offer. Appearances no
  longer carry a featured offer.
- Sections get stable ids; appearances set photos per section ("Bilder je Baustein") and
  sections can be limited to appearances. Photo pickers show thumbnails everywhere.
- Themes: themes:make (scaffold, never overwrites), themes:check (contract, build, markup,
  colours, external resources, WCAG contrast), themes:preview (signed links per page and
  appearance), signed ?theme= preview and preview action in the site form, inherited colour
  tokens in the admin. Base CSS split into theme.css + reusable theme-base.css.
- docs/THEME-ERSTELLUNG.md: binding guide for building additional themes elsewhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 18:51:38 +02:00

170 lines
4.9 KiB
PHP

<?php
namespace App\Themes;
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;
/**
* 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';
}
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 [];
}
}