Files
fewo-stommer/app/Filament/Pages/EditContent.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

139 lines
4.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Filament\Pages;
use App\Enums\UserRole;
use App\Filament\Forms\Components\PhotoSelect;
use App\Support\Content;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Actions;
use Filament\Schemas\Components\EmbeddedSchema;
use Filament\Schemas\Components\Form;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Tabs\Tab;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use UnitEnum;
/**
* One form for all editable website texts from config/content.php, one tab per section.
*
* @property-read Schema $form
*/
class EditContent extends Page
{
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedPencilSquare;
protected static string|UnitEnum|null $navigationGroup = 'Webseite';
protected static ?string $navigationLabel = 'Texte';
protected static ?string $title = 'Webseiten-Texte';
protected static ?string $slug = 'texte';
protected static ?int $navigationSort = 3;
/** @var array<string, ?string> */
public array $data = [];
public static function canAccess(): bool
{
return (bool) auth()->user()?->hasRole(UserRole::Admin, UserRole::Content);
}
public function mount(): void
{
$values = [];
foreach (Content::sections() as $section) {
foreach (array_keys($section['fields']) as $key) {
$values[self::fieldName($key)] = Content::get($key);
}
}
$this->form->fill($values);
}
public function form(Schema $schema): Schema
{
return $schema
->statePath('data')
->components([
Tabs::make('sections')
->persistTabInQueryString()
->tabs(collect(Content::sections())
->map(fn (array $section): Tab => Tab::make($section['label'])->schema(
collect($section['fields'])->map(fn (array $field, string $key) => $this->field($key, $field))->values()->all(),
))
->values()
->all()),
]);
}
public function content(Schema $schema): Schema
{
return $schema->components([
Form::make([EmbeddedSchema::make('form')])
->id('form')
->livewireSubmitHandler('save')
->footer([
Actions::make([
Action::make('save')->label('Speichern')->submit('save')->keyBindings(['mod+s']),
])->sticky(),
]),
]);
}
public function save(): void
{
$state = $this->form->getState();
$values = [];
foreach (Content::sections() as $section) {
foreach (array_keys($section['fields']) as $key) {
$value = $state[self::fieldName($key)] ?? null;
$values[$key] = $value === null ? null : (string) $value;
}
}
Content::save($values);
Notification::make()->title('Texte gespeichert')->success()->send();
}
/**
* @param array{label: string, type: string, default?: ?string} $field
*/
private function field(string $key, array $field): TextInput|Textarea|RichEditor|Select
{
$name = self::fieldName($key);
return match ($field['type']) {
'textarea' => Textarea::make($name)->label($field['label'])->rows(3),
'richtext' => RichEditor::make($name)
->label($field['label'])
->toolbarButtons([['bold', 'italic', 'link'], ['h2', 'h3'], ['bulletList', 'orderedList'], ['undo', 'redo']]),
'photo' => PhotoSelect::make($name)
->label($field['label'])
->placeholder('Kein Bild gewählt')
->helperText('Bilder verwalten Sie unter „Webseite › Bilder“.'),
default => TextInput::make($name)->label($field['label'])->maxLength(255),
};
}
/**
* Dots would be read as nested state, so content keys are flattened for the form.
*/
private static function fieldName(string $key): string
{
return str_replace('.', '__', $key);
}
}