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

189 lines
5.4 KiB
PHP

<?php
namespace App\Themes\Sections;
use App\Filament\Forms\Components\PhotoSelect;
use App\Models\Page;
use App\Models\Photo;
use App\Themes\Theme;
use Filament\Forms\Components\RichEditor\RichContentRenderer;
use Filament\Forms\Components\Select;
use Filament\Schemas\Components\Component;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\HtmlString;
/**
* A building block of pages. A section prepares data; how it looks is up to the theme's
* view "theme::sections.<key>". Register new sections in config/themes.php.
*/
abstract class Section
{
/** One photo (field holds a photo id). */
public const SLOT_SINGLE = 'single';
/** Several chosen photos (field holds a list of ids). */
public const SLOT_MULTIPLE = 'multiple';
/** Photos picked automatically (e.g. by category); an appearance may replace or precede them. */
public const SLOT_COLLECTION = 'collection';
abstract public static function key(): string;
abstract public static function label(): string;
/**
* Heroicon name shown in the page builder.
*/
public static function icon(): string
{
return 'heroicon-o-square-3-stack-3d';
}
/**
* Admin fields of the section (builder block schema). Field names are keys of $data in data().
*
* @return list<Component>
*/
public static function schema(): array
{
return [];
}
/**
* Photo places an appearance can fill with seasonal photos ("Bilder je Baustein").
*
* @return array<string, array{label: string, type: string, max?: int}>
*/
public static function photoSlots(): array
{
return [];
}
/**
* Short text identifying a placed section in admin lists, e.g. its heading.
*
* @param array<string, mixed> $data
*/
public static function summary(array $data): ?string
{
$text = $data['heading'] ?? $data['title'] ?? null;
return filled($text) ? str(strip_tags((string) $text))->replace('*', '')->limit(50)->toString() : null;
}
/**
* Variables for the view.
*
* @param array<string, mixed> $data what the admin entered
* @return array<string, mixed>
*/
abstract public function data(Theme $theme, array $data): array;
public function view(): string
{
return 'theme::sections.'.static::key();
}
/**
* Sanitised HTML of a rich text field.
*/
protected function html(?string $value): ?HtmlString
{
return filled($value) ? new HtmlString(RichContentRenderer::make($value)->toHtml()) : null;
}
/**
* @param list<int|string>|null $ids
* @return Collection<int, Photo>
*/
protected function photos(?array $ids): Collection
{
if (blank($ids)) {
return new Collection;
}
$photos = Photo::query()->published()->whereKey($ids)->with('media')->get()->keyBy('id');
return new Collection(array_values(array_filter(array_map(fn ($id) => $photos->get((int) $id), $ids))));
}
/**
* The seasonal photo the active appearance sets for a single slot of this placed section.
*
* @param array<string, mixed> $data
*/
protected function seasonalPhoto(Theme $theme, array $data, string $slot): ?Photo
{
return $this->seasonalPhotos($theme, $data, $slot)?->first();
}
/**
* The seasonal photos the active appearance sets for a slot of this placed section, null if none.
*
* @param array<string, mixed> $data
* @return Collection<int, Photo>|null
*/
protected function seasonalPhotos(Theme $theme, array $data, string $slot): ?Collection
{
$ids = $theme->sectionPhotoIds($data['id'] ?? null, $slot);
if ($ids === []) {
return null;
}
$photos = $this->photos($ids);
return $photos->isEmpty() ? null : $photos;
}
/**
* Photos of a collection slot: the automatic photos, replaced or preceded by the seasonal ones.
*
* @param array<string, mixed> $data
* @param Collection<int, Photo> $automatic
* @return Collection<int, Photo>
*/
protected function withSeasonalPhotos(Theme $theme, array $data, string $slot, Collection $automatic): Collection
{
$seasonal = $this->seasonalPhotos($theme, $data, $slot);
if ($seasonal === null) {
return $automatic;
}
if ($theme->sectionPhotoMode($data['id'] ?? null) === 'prepend') {
return $seasonal->merge($automatic)->unique('id')->values();
}
return $seasonal;
}
protected function pageUrl(?string $slug): ?string
{
if (blank($slug)) {
return null;
}
return $slug === Page::HOME ? route('home') : route('page', ['slug' => $slug]);
}
/**
* Select field listing the website photos with thumbnails.
*/
protected static function photoSelect(string $name, string $label, bool $multiple = false): Select
{
return PhotoSelect::make($name)
->label($label)
->multiple($multiple);
}
/**
* Select field listing the pages of the site, for links.
*/
protected static function pageSelect(string $name, string $label): Select
{
return Select::make($name)
->label($label)
->options(fn (): array => Page::query()->orderBy('sort_order')->pluck('title', 'slug')->all())
->searchable();
}
}