- Webseite > Videos (Datei, Standbild, Kapitel mit Raum im Grundriss); Upload bis 100 MB - Titelbereich: Video statt Foto (stumme Schleife, respektiert Bewegung reduzieren/Save-Data) und naechste freie Termine mit Gesamtpreis (NextFreeStays) - Neuer Baustein Rundgang (Video) mit waehlbarer Anordnung; Grundriss springt ins Video - Buchungsseite: Preis unter dem Kalender, Formular rechts, Ablauf als Grafik - Seebad: Fahrkarten, Fahrplan-Kapitel, Sonnenaufgang als einmaliges Ereignis Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
209 lines
6.5 KiB
PHP
209 lines
6.5 KiB
PHP
<?php
|
||
|
||
namespace App\Themes\Sections;
|
||
|
||
use App\Enums\PhotoCategory;
|
||
use App\Filament\Forms\Components\PhotoSelect;
|
||
use App\Models\Page;
|
||
use App\Models\Photo;
|
||
use App\Models\Video;
|
||
use App\Themes\Theme;
|
||
use Filament\Forms\Components\Radio;
|
||
use Filament\Forms\Components\RichEditor\RichContentRenderer;
|
||
use Filament\Forms\Components\Select;
|
||
use Filament\Schemas\Components\Component;
|
||
use Filament\Schemas\Components\Utilities\Get;
|
||
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
|
||
{
|
||
/** How photos follow the seasons – shown below every photo choice. */
|
||
public const SEASON_HINT = 'Fotos mit Saison (Webseite › Fotos › „Zeigen in“) erscheinen nur in ihrem Erscheinungsbild, alle anderen ganzjährig.';
|
||
|
||
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 [];
|
||
}
|
||
|
||
/**
|
||
* 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))));
|
||
}
|
||
|
||
/**
|
||
* Chosen photos in the editor's order: published and fitting the season in effect.
|
||
*
|
||
* @param list<int|string>|null $ids
|
||
* @return Collection<int, Photo>
|
||
*/
|
||
protected function chosenPhotos(Theme $theme, ?array $ids): Collection
|
||
{
|
||
return $this->photos($ids)->filter(fn (Photo $photo): bool => $photo->fitsSeason($theme->appearance))->values();
|
||
}
|
||
|
||
/**
|
||
* Photos picked automatically for the section's apartment and the season (Photo::automatic).
|
||
*
|
||
* @return Collection<int, Photo>
|
||
*/
|
||
protected function automaticPhotos(Theme $theme, ?PhotoCategory $category = null, ?int $limit = null): Collection
|
||
{
|
||
$photos = Photo::automatic($theme->property(), $theme->appearance, $category);
|
||
|
||
return $limit !== null ? $photos->take($limit)->values() : $photos;
|
||
}
|
||
|
||
/**
|
||
* Photos of a section that offers "automatisch" or "eigene Auswahl" (fields from photoSourceFields()).
|
||
*
|
||
* @param array<string, mixed> $data
|
||
* @return Collection<int, Photo>
|
||
*/
|
||
protected function sourcedPhotos(Theme $theme, array $data, ?PhotoCategory $category = null, ?int $limit = null): Collection
|
||
{
|
||
if (($data['photo_source'] ?? 'auto') === 'selection') {
|
||
$photos = $this->chosenPhotos($theme, $data['photo_ids'] ?? []);
|
||
|
||
return $limit !== null ? $photos->take($limit)->values() : $photos;
|
||
}
|
||
|
||
return $this->automaticPhotos($theme, $category, $limit);
|
||
}
|
||
|
||
/**
|
||
* "Automatisch" or "Eigene Auswahl" plus the selection field, shown only for a selection.
|
||
*
|
||
* @return list<Component>
|
||
*/
|
||
protected static function photoSourceFields(string $automaticLabel): array
|
||
{
|
||
return [
|
||
Radio::make('photo_source')
|
||
->label('Bilder')
|
||
->options(['auto' => $automaticLabel, 'selection' => 'Eigene Auswahl'])
|
||
->default('auto')
|
||
->inline()
|
||
->live(),
|
||
static::photoSelect('photo_ids', 'Ausgewählte Bilder', multiple: true)
|
||
->helperText(self::SEASON_HINT)
|
||
->visible(fn (Get $get): bool => $get('photo_source') === 'selection'),
|
||
];
|
||
}
|
||
|
||
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 website videos (Admin › Webseite › Videos).
|
||
*/
|
||
protected static function videoSelect(string $name, string $label): Select
|
||
{
|
||
return Select::make($name)
|
||
->label($label)
|
||
->options(fn (): array => Video::query()->orderBy('title')->pluck('title', 'id')->all())
|
||
->placeholder('– kein Video –');
|
||
}
|
||
|
||
/**
|
||
* The chosen video, if it still exists and has a file.
|
||
*/
|
||
protected function video(mixed $id): ?Video
|
||
{
|
||
$video = filled($id) ? Video::query()->with('media')->find($id) : null;
|
||
|
||
return $video?->url() ? $video : null;
|
||
}
|
||
|
||
/**
|
||
* 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();
|
||
}
|
||
}
|