Files
fewo-stommer/app/Themes/SectionRegistry.php
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

116 lines
3.9 KiB
PHP

<?php
namespace App\Themes;
use App\Models\Appearance;
use App\Themes\Sections\Section;
use Filament\Forms\Components\Builder\Block;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Select;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\View as ViewFactory;
/**
* The section catalogue from config/themes.php: builder blocks for the admin and rendering for the website.
*
* Every placed section carries data.id (a stable UUID, see Page::assignSectionIds) so appearances can
* address it, and data.appearances (appearance ids; empty = always shown).
*/
class SectionRegistry
{
/**
* @return array<string, class-string<Section>>
*/
public function all(): array
{
return collect(config('themes.sections'))
->mapWithKeys(fn (string $class): array => [$class::key() => $class])
->all();
}
public function get(string $key): ?Section
{
$class = $this->all()[$key] ?? null;
return $class ? app($class) : null;
}
/**
* @return list<Block>
*/
public function blocks(): array
{
return array_values(array_map(
fn (string $class): Block => Block::make($class::key())
->label(fn (?array $state): string => $this->blockLabel($class, $state))
->icon($class::icon())
->schema([
Hidden::make('id'),
...$class::schema(),
Select::make('appearances')
->label('Nur anzeigen im Erscheinungsbild')
->helperText('Leer = immer anzeigen. Z. B. ein Weihnachts-Baustein nur im Erscheinungsbild „Weihnachten“.')
->options(fn (): array => Appearance::query()->orderByDesc('is_default')->orderBy('name')->pluck('name', 'id')->all())
->multiple(),
]),
$this->all(),
));
}
/**
* Whether a placed section is shown with the active appearance.
*
* @param array<string, mixed> $data
*/
public function isVisible(Theme $theme, array $data): bool
{
$appearances = array_map('intval', array_filter((array) ($data['appearances'] ?? [])));
return $appearances === [] || ($theme->appearance !== null && in_array($theme->appearance->id, $appearances, true));
}
/**
* Renders the stored sections of a page; unknown or failing types are skipped (and reported),
* so a page never breaks because of one section.
*
* @param list<array{type: string, data?: array<string, mixed>}>|null $sections
* @return list<View>
*/
public function render(Theme $theme, ?array $sections): array
{
$views = [];
foreach ($sections ?? [] as $index => $stored) {
$section = $this->get($stored['type'] ?? '');
if ($section === null || ! ViewFactory::exists($section->view()) || ! $this->isVisible($theme, $stored['data'] ?? [])) {
continue;
}
$views[] = ViewFactory::make($section->view(), [
...$section->data($theme, $stored['data'] ?? []),
'theme' => $theme,
'sectionId' => $section::key().'-'.$index,
]);
}
return $views;
}
/**
* "Text mit Bild · Ihr Zuhause an der Ostsee · nur: Winter" for the collapsed builder item.
*
* @param class-string<Section> $class
* @param array<string, mixed>|null $state
*/
private function blockLabel(string $class, ?array $state): string
{
if ($state === null) {
return $class::label();
}
$appearances = array_filter((array) ($state['appearances'] ?? []));
$only = $appearances === [] ? null : 'nur: '.Appearance::query()->whereKey($appearances)->pluck('name')->implode(', ');
return collect([$class::label(), $class::summary($state), $only])->filter()->implode(' · ');
}
}