Files
fewo-stommer/app/Filament/Pages/EditContent.php
T
Lars StommerandClaude Opus 5.5 bb7eb62782 Inhaltsverwaltung: Bilder, Webseiten-Texte, Ausstattung, FAQ, Hinweis-Banner
- Bilder mit automatischen WebP-Webgrößen (640/1280/2048 px), Sortierung per Drag & Drop, Mehrfach-Upload, Import-Befehl photos:import
- Webseiten-Texte als feste, bearbeitbare Bausteine (config/content.php), Rich-Text bereinigt ausgegeben
- Ausstattung und FAQ aus der bisherigen Webseite übernommen
- Rolle Inhalte hat Zugriff auf alle Webseiten-Bereiche

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:19:51 +02:00

141 lines
4.5 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\Models\Photo;
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 = 2;
/** @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' => Select::make($name)
->label($field['label'])
->options(fn (): array => Photo::query()->orderBy('category')->orderBy('sort_order')->pluck('alt_text', 'id')->all())
->searchable()
->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);
}
}