Files
Lars Stommer abf0e9004b
Deploy / test (push) Failing after 2m47s
Deploy / deploy (push) Skipped
Wohnung je Baustein wählen und Gitea-Runner-Kompatibilität herstellen
2026-10-26 08:23:14 +01:00

141 lines
5.1 KiB
PHP

<?php
namespace App\Themes;
use App\Models\Appearance;
use App\Models\Property;
use App\Themes\Sections\Section;
use Filament\Forms\Components\Builder\Block;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Select;
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, data.appearances (appearance ids; empty = always shown), and an optional data.property_id
* (an active property of this website; empty = primary property).
*/
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(?int $siteId = null): 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('property_id')
->label('Wohnung')
->placeholder('Hauptwohnung')
->helperText('Leer = Hauptwohnung dieser Website.')
->options(fn (): array => Property::query()
->where('site_id', $siteId)
->whereNotNull('site_id')
->where('is_active', true)
->orderBy('name')
->pluck('name', 'id')
->all()),
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));
}
/**
* Prepares the stored sections for deferred rendering. Unknown types, missing views, hidden sections
* and unavailable property assignments are skipped.
*
* @param list<array{type: string, data?: array<string, mixed>}>|null $sections
* @return list<SectionView>
*/
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;
}
$data = $stored['data'] ?? [];
$property = filled($data['property_id'] ?? null)
? $theme->site->properties()->where('is_active', true)->whereKey($data['property_id'])->first()
: $theme->property();
if (filled($data['property_id'] ?? null) && $property === null) {
continue;
}
$views[] = $theme->runForProperty($property, fn (): SectionView => new SectionView(
$theme,
$property,
ViewFactory::make($section->view(), [
...$section->data($theme, $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(' · ');
}
}