Baue die Verwaltung Kommunikation, die öffentliche Anfrageschnittstelle und das CMS-Widget Anfrage

Anliegen, Feldkatalog, Feldgruppen, Anfragevorlagen mit Fassungen, Anfrage-Einstiege, Posteingang mit
Status und Notizen, Einstellungen mit Empfängern. Das Widget speichert nur die Kennung eines veröffentlichten
Einstiegs; Formular, Bedingungen und Annahme kommen zur Laufzeit vom Server (Kompaktformular und
mehrstufiger Dialog aus derselben Beschreibung).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-27 17:41:55 +01:00
1 parent 2ee1fc9866
commit c6a8167601
45 files changed
+5415

No files matched your search

@@ -0,0 +1,57 @@
<?php
namespace App\Filament\Resources\Communication\Templates;
use App\Domain\Communication\InquiryTemplate;
use App\Filament\Resources\Communication\CommunicationResource;
use App\Filament\Resources\Communication\Templates\Pages\EditInquiryTemplate;
use App\Filament\Resources\Communication\Templates\Pages\ListInquiryTemplates;
use App\Filament\Resources\Communication\Templates\RelationManagers\VersionsRelationManager;
use App\Filament\Support\TemplateEditor;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
/**
* Anfragevorlagen: ein vollständiger fachlicher Anfrageprozess. Der Entwurf ist frei bearbeitbar; wirksam wird nur eine
* bewusst veröffentlichte, unveränderliche Fassung.
*/
class InquiryTemplateResource extends CommunicationResource
{
protected static ?string $model = InquiryTemplate::class;
protected static ?string $navigationLabel = 'Anfragevorlagen';
protected static ?string $modelLabel = 'Anfragevorlage';
protected static ?string $pluralModelLabel = 'Anfragevorlagen';
protected static ?string $recordTitleAttribute = 'name';
protected static ?string $slug = 'kommunikation/vorlagen';
protected static ?int $navigationSort = 5;
public static function form(Schema $schema): Schema
{
return $schema->components(TemplateEditor::schema());
}
public static function table(Table $table): Table
{
return $table->columns([TextColumn::make('name')->label('Bezeichnung')]);
}
public static function getRelations(): array
{
return [VersionsRelationManager::class];
}
public static function getPages(): array
{
return [
'index' => ListInquiryTemplates::route('/'),
'edit' => EditInquiryTemplate::route('/{record}'),
];
}
}
@@ -0,0 +1,151 @@
<?php
namespace App\Filament\Resources\Communication\Templates\Pages;
use App\Domain\Communication\InquiryTemplate;
use App\Domain\Communication\Publisher;
use App\Domain\Communication\TemplateCompiler;
use App\Domain\Communication\TemplateDraft;
use App\Filament\Resources\Communication\Templates\InquiryTemplateResource;
use App\Filament\Support\ArchivesCommunicationRecords;
use App\Models\User;
use Filament\Actions\Action;
use Filament\Forms\Components\Textarea;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Filament\Schemas\Components\Text;
use Filament\Support\Enums\Width;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Validation\ValidationException;
class EditInquiryTemplate extends EditRecord
{
use ArchivesCommunicationRecords;
protected static string $resource = InquiryTemplateResource::class;
protected Width|string|null $maxContentWidth = Width::Full;
protected ?bool $hasUnsavedDataChangesAlert = true;
public function getSubheading(): string|Htmlable|null
{
/** @var InquiryTemplate $template */
$template = $this->getRecord();
if ($template->archived_at !== null) {
return 'Archiviert.';
}
if (! $template->isPublished()) {
return 'Noch nicht veröffentlicht. Besucher sehen diese Vorlage erst nach „Veröffentlichen“.';
}
$version = $template->publishedVersion;
return 'Gültig ist Fassung '.$version->version.' (veröffentlicht am '.$version->published_at->format('d.m.Y').').'
.($template->hasUnpublishedChanges() ? ' Der Entwurf enthält Änderungen, die noch nicht veröffentlicht sind.' : ' Der Entwurf entspricht der gültigen Fassung.');
}
protected function getHeaderActions(): array
{
return [
Action::make('preview')
->label('Vorschau')
->icon('heroicon-o-eye')
->color('gray')
->tooltip('Zeigt den zuletzt gespeicherten Entwurf als Besucher mit allen Bedingungen. Es wird nichts gesendet.')
->url(fn (): string => route('communication.preview.template', $this->getRecord()))
->openUrlInNewTab(),
Action::make('check')
->label('Konfiguration prüfen')
->color('gray')
->action(function (): void {
$this->save(shouldRedirect: false, shouldSendSavedNotification: false);
$result = app(TemplateCompiler::class)->compile(TemplateDraft::clean((array) $this->getRecord()->draft));
if ($result['errors'] === []) {
Notification::make()->title('Die Konfiguration ist in Ordnung.')->body('Der Entwurf wurde gespeichert und lässt sich veröffentlichen.')->success()->send();
return;
}
Notification::make()->title('Die Konfiguration hat Probleme')->body(implode("\n", array_map(fn (string $line): string => '• '.$line, $result['errors'])))->danger()->persistent()->send();
}),
Action::make('publish')
->label('Veröffentlichen')
->color('primary')
->visible(fn (): bool => $this->getRecord()->archived_at === null)
->modalHeading('Neue Fassung veröffentlichen')
->modalSubmitActionLabel('Jetzt veröffentlichen')
->modalWidth('2xl')
->schema(fn (): array => [
Text::make($this->affectedText()),
Textarea::make('note')->label('Bemerkung zur Fassung (optional)')->rows(2)->maxLength(300),
])
->action(function (array $data): void {
$this->save(shouldRedirect: false, shouldSendSavedNotification: false);
try {
$version = app(Publisher::class)->publishTemplate($this->getRecord(), auth('staff')->user() instanceof User ? auth('staff')->user() : null, $data['note'] ?? null);
} catch (ValidationException $e) {
Notification::make()->title('Die Vorlage lässt sich nicht veröffentlichen')->body(implode("\n", array_map(fn (string $line): string => '• '.$line, (array) ($e->errors()['draft'] ?? []))))->danger()->persistent()->send();
return;
}
Notification::make()->title('Fassung '.$version->version.' ist veröffentlicht.')->success()->send();
$this->redirect(InquiryTemplateResource::getUrl('edit', ['record' => $this->getRecord()]));
}),
...$this->archiveActions(),
];
}
/** Einstiege, die diese Vorlage verwenden und von einer neuen Fassung betroffen sind. */
private function affectedText(): string
{
$rows = app(Publisher::class)->affected($this->getRecord());
if ($rows === []) {
return 'Speichert den Entwurf und veröffentlicht ihn als neue Fassung. Noch kein Anfrage-Einstieg verwendet diese Vorlage.';
}
$names = array_map(fn (array $row): string => $row['entry']->name.($row['entry']->published_config !== null ? ' (veröffentlicht)' : ' (Entwurf)'), $rows);
return 'Speichert den Entwurf und veröffentlicht ihn als neue Fassung. Sofort betroffen sind diese Anfrage-Einstiege und damit alle Seiten, die sie einbinden: '.implode(', ', $names).'. Die bisherige Fassung bleibt als Verlauf erhalten und lässt sich wieder in Kraft setzen.';
}
protected function archiveNote(): string
{
return 'Die Vorlage wird archiviert. Anfrage-Einstiege, die sie verwenden, liefern dann kein Formular mehr aus. Eingegangene Anfragen bleiben unverändert.';
}
protected function archiveAttributes(): array
{
return ['archived_at' => now()];
}
protected function indexUrl(): string
{
return InquiryTemplateResource::getUrl('index');
}
protected function archiveBlocked(): ?string
{
$live = $this->getRecord()->entries()->live()->count();
return $live > 0 ? "Es gibt noch $live veröffentlichte Anfrage-Einstiege mit dieser Vorlage. Bitte zuerst zurückziehen oder auf eine andere Vorlage umstellen." : null;
}
protected function mutateFormDataBeforeSave(array $data): array
{
$data['draft'] = TemplateDraft::clean((array) ($data['draft'] ?? []));
return $data;
}
protected function getRedirectUrl(): ?string
{
return null;
}
}
@@ -0,0 +1,114 @@
<?php
namespace App\Filament\Resources\Communication\Templates\Pages;
use App\Domain\Communication\InquiryTemplate;
use App\Domain\Communication\Keys;
use App\Filament\Resources\Communication\Templates\InquiryTemplateResource;
use App\Filament\Support\ListsCatalog;
use Filament\Actions\Action;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Str;
class ListInquiryTemplates extends ListRecords
{
use ListsCatalog;
protected static string $resource = InquiryTemplateResource::class;
public function getSubheading(): string|Htmlable|null
{
return 'Eine Anfragevorlage beschreibt einen vollständigen Anfrageprozess: Schritte, Feldgruppen, Bedingungen und Texte. Änderungen entstehen zunächst als Entwurf und gelten erst nach „Veröffentlichen“. Die Anfrage-Einstiege, die eine Vorlage verwenden, übernehmen jede neu veröffentlichte Fassung.';
}
protected function getHeaderActions(): array
{
return [
Action::make('create')
->label('Vorlage anlegen')
->modalHeading('Neue Anfragevorlage')
->modalDescription('Der Aufbau entsteht danach auf der Seite der Vorlage.')
->modalSubmitActionLabel('Vorlage anlegen')
->schema([
TextInput::make('name')->label('Bezeichnung')->required()->maxLength(120),
Textarea::make('description')->label('Beschreibung (nur Verwaltung, optional)')->rows(2)->maxLength(400),
])
->action(function (array $data): void {
$draft = InquiryTemplate::emptyDraft();
$draft['steps'] = [['key' => 's'.Str::lower(Str::random(6)), 'title' => 'Ihre Angaben', 'intro' => null, 'items' => []]];
$template = InquiryTemplate::query()->create([
'key' => Keys::from($data['name'], InquiryTemplate::query()->pluck('key')->all()),
'name' => $data['name'],
'description' => $data['description'] ?? null,
'draft' => $draft,
]);
Notification::make()->title('Die Vorlage ist angelegt.')->success()->send();
$this->redirect(InquiryTemplateResource::getUrl('edit', ['record' => $template]));
}),
];
}
protected function catalogRows(): array
{
$rows = [];
foreach (InquiryTemplate::query()->with('publishedVersion')->withCount('entries')->orderBy('name')->get() as $template) {
$url = InquiryTemplateResource::getUrl('edit', ['record' => $template]);
$archived = $template->archived_at !== null;
$published = $template->isPublished();
$changes = $published && $template->hasUnpublishedChanges();
$steps = count((array) ($template->draft['steps'] ?? []));
$rows[] = [
'schluessel' => 't'.$template->id,
'titel' => $template->name,
'meta' => 'Anfragevorlage · '.$steps.' '.($steps === 1 ? 'Schritt' : 'Schritte').' · '.$template->entries_count.' '.($template->entries_count === 1 ? 'Einstieg' : 'Einstiege'),
'text' => (string) ($template->description ?? ''),
'symbol' => 'modul',
'url' => $url,
'chips' => array_values(array_filter([
$archived ? ['label' => 'Archiviert', 'ton' => 'leise'] : ($published
? ['label' => 'Fassung '.$template->publishedVersion->version.' gültig', 'ton' => 'gut']
: ['label' => 'Noch nicht veröffentlicht', 'ton' => 'offen']),
$changes ? ['label' => 'Änderungen noch nicht veröffentlicht', 'ton' => 'achtung'] : null,
])),
'zustand' => [$archived ? 'archiviert' : ($published ? 'veroeffentlicht' : 'entwurf'), ...($changes ? ['aenderungen'] : [])],
'aktionen' => [['label' => 'Bearbeiten', 'href' => $url, 'icon' => 'stift']],
];
}
return $rows;
}
protected function zustandOptionen(): array
{
return [
'alle' => 'Jeder Zustand',
'veroeffentlicht' => 'Veröffentlicht',
'entwurf' => 'Noch nicht veröffentlicht',
'aenderungen' => 'Mit unveröffentlichten Änderungen',
'archiviert' => 'Archiviert',
];
}
protected function suchPlatzhalter(): string
{
return 'Bezeichnung oder Beschreibung';
}
protected function einheit(): array
{
return ['Anfragevorlage', 'Anfragevorlagen'];
}
protected function leer(): array
{
return ['titel' => 'Noch keine Anfragevorlagen', 'text' => 'Mit „Vorlage anlegen“ oben entsteht die erste Vorlage. Auf der Seite „Einstellungen“ lässt sich eine Beispielkonfiguration anlegen.'];
}
}
@@ -0,0 +1,78 @@
<?php
namespace App\Filament\Resources\Communication\Templates\RelationManagers;
use App\Domain\Communication\Inquiry;
use App\Domain\Communication\InquiryTemplate;
use App\Domain\Communication\InquiryTemplateVersion;
use App\Domain\Communication\Publisher;
use App\Filament\Resources\Communication\Templates\InquiryTemplateResource;
use App\Models\User;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Validation\ValidationException;
/**
* Fassungen einer Anfragevorlage: unveränderlich, jederzeit nachvollziehbar. Eine frühere Fassung lässt sich wieder in
* Kraft setzen oder als Entwurf laden.
*/
class VersionsRelationManager extends RelationManager
{
protected static string $relationship = 'versions';
protected static ?string $title = 'Fassungen';
protected static ?bool $isLazyLoaded = false;
public function table(Table $table): Table
{
/** @var InquiryTemplate $template */
$template = $this->getOwnerRecord();
return $table
->columns([
TextColumn::make('version')->label('Fassung')->prefix('Nr. '),
TextColumn::make('published_at')->label('Veröffentlicht')->dateTime('d.m.Y H:i'),
TextColumn::make('published_by')->label('Von')->state(fn (InquiryTemplateVersion $record): string => (string) User::query()->whereKey($record->published_by)->value('name')),
TextColumn::make('note')->label('Bemerkung')->placeholder('–'),
TextColumn::make('state')->label('Zustand')->badge()
->color(fn (string $state): string => $state === 'Gültig' ? 'success' : 'gray')
->state(fn (InquiryTemplateVersion $record): string => $record->id === $this->getOwnerRecord()->published_version_id ? 'Gültig' : 'Früher'),
TextColumn::make('inquiries')->label('Anfragen')->state(fn (InquiryTemplateVersion $record): int => Inquiry::query()->where('template_version_id', $record->id)->count()),
])
->recordActions([
Action::make('restore')
->label('Wieder in Kraft setzen')
->visible(fn (InquiryTemplateVersion $record): bool => $record->id !== $template->fresh()->published_version_id)
->requiresConfirmation()
->modalDescription('Besucher sehen danach wieder diese Fassung. Der Entwurf bleibt unverändert. Eingegangene Anfragen bleiben ihrer damaligen Fassung zugeordnet.')
->action(function (InquiryTemplateVersion $record): void {
try {
app(Publisher::class)->restoreVersion($this->getOwnerRecord(), $record);
} catch (ValidationException $e) {
Notification::make()->title('Nicht möglich')->body(implode(' ', $e->errors()['draft'] ?? []))->danger()->send();
return;
}
Notification::make()->title('Fassung '.$record->version.' ist wieder gültig.')->success()->send();
$this->redirect(InquiryTemplateResource::getUrl('edit', ['record' => $this->getOwnerRecord()]));
}),
Action::make('load')
->label('Als Entwurf laden')
->color('gray')
->requiresConfirmation()
->modalDescription('Der aktuelle Entwurf wird durch den Stand dieser Fassung ersetzt. Veröffentlicht wird dadurch nichts.')
->action(function (InquiryTemplateVersion $record): void {
$this->getOwnerRecord()->forceFill(['draft' => $record->source])->save();
Notification::make()->title('Der Entwurf entspricht jetzt Fassung '.$record->version.'.')->success()->send();
$this->redirect(InquiryTemplateResource::getUrl('edit', ['record' => $this->getOwnerRecord()]));
}),
])
->defaultSort('version', 'desc')
->paginated(false);
}
}