Liefere das Anfrageformular je Anfragegrund aus und gib dem Widget Gründe, Formularführung und Präsentation

Das Widget Anfrage wählt statt eines Anfrage-Einstiegs mehrere Anfragegründe, jeder zeigt auf ein veröffentlichtes Formular
(optional mit vorgewähltem Anliegen). Der Besucher erhält genau das Formular seines Grundes: /anfrage/formular/{vorlage}
mit Grund in der Adresse, dasselbe Token, dieselbe Auswertung und Annahme. Ein Dialog (dialog-Element) und die Grundauswahl
als Karten oder Auswahlfeld kommen aus resources/js/inquiry-widget.js. Seiten mit einem Einstieg bleiben darstellbar.
Bereiche lassen sich wie Gruppen anzeigen oder ausblenden.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-27 22:29:31 +01:00
1 parent f255bf5200
commit fb479c1b01
18 files changed
+1394 -57

No files matched your search

+261 -25
View File
@@ -3,19 +3,34 @@
namespace App\Domain\Cms\Widgets\Types;
use App\Domain\Cms\Widgets\RenderContext;
use App\Domain\Cms\Widgets\WidgetErrors;
use App\Domain\Cms\Widgets\WidgetType;
use App\Domain\Communication\InquiryEntry;
use App\Domain\Communication\InquiryTemplate;
use App\Domain\Communication\Keys;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Text;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
use Illuminate\Validation\Rule;
/**
* Kontakt-Widget: bindet einen veröffentlichten Anfrage-Einstieg der Kommunikationsverwaltung ein (ADR-60). Das Widget
* speichert ausschließlich die Kennung des Einstiegs. Felder, Auswahlwerte, Bedingungen, Schritte, Texte und
* Empfänger gibt es im CMS nicht; sie werden in der Kommunikationsverwaltung gepflegt.
* Kontakt-Widget (ADR-60, ADR-61). Der Redakteur legt fest, welche Anfragegründe auf der Seite angeboten werden und
* welches veröffentlichte Formular jeder Grund öffnet; Formularführung (Langformular oder mehrstufig) und Darstellung
* (im Abschnitt oder als Dialog) sind zwei getrennte Einstellungen. Der Besucher wählt einen Grund und erhält genau das
* zugehörige Formular. Felder, Bedingungen, Texte und Empfänger gibt es im CMS nicht: Sie werden im Formular-Editor der
* Kommunikationsverwaltung gepflegt und gelten sofort auf allen Seiten.
*
* Seiten aus der ersten Fassung (ein Anfrage-Einstieg unter refs.entry) werden weiter dargestellt.
*/
class InquiryWidget extends WidgetType
{
public const MAX_REASONS = 8;
public function key(): string
{
return 'inquiry';
@@ -28,7 +43,7 @@ public function label(): string
public function description(): string
{
return 'Bindet einen Anfrage-Einstieg aus der Kommunikationsverwaltung ein: Kontaktformular oder mehrstufiger Anfragedialog.';
return 'Kontaktformular mit Auswahl des Anfragegrundes: Der Besucher wählt, worum es geht, und erhält genau das passende Formular.';
}
public function icon(): string
@@ -48,35 +63,122 @@ public function preferredUnits(): int
public function narrowBehavior(): string
{
return 'Das Formular nutzt die Breite des Platzes; Felder stehen untereinander, der mehrstufige Dialog bleibt einspaltig.';
return 'Das Formular nutzt die Breite des Platzes; Felder stehen untereinander, Karten der Grundauswahl stapeln, der Dialog füllt auf dem Smartphone den Bildschirm.';
}
public function stages(): array
{
return ['fluid' => 'einspaltig, in Lesebreite begrenzt; Felder gleicher Art stehen auf breiten Plätzen nebeneinander'];
return ['fluid' => 'einspaltig, in Lesebreite begrenzt; Felder gleicher Art und Karten stehen bei viel Platz nebeneinander'];
}
public function schematic(): array
{
return [[6, 4, 24, 3, 'ink'], [6, 10, 48, 5, 'line'], [6, 18, 48, 5, 'line'], [6, 26, 18, 5, 'accent']];
return [[6, 4, 24, 3, 'ink'], [6, 10, 22, 6, 'line'], [32, 10, 22, 6, 'line'], [6, 20, 48, 5, 'line'], [6, 28, 18, 5, 'accent']];
}
/**
* Formularführung und Präsentation sind unabhängig: Jede Kombination ist zulässig.
*
* @return array<string, array<string, string>>
*/
public function settings(): array
{
return [
'mode' => ['compact' => 'Langes Formular', 'wizard' => 'Mehrstufiger Dialog'],
'open' => ['inline' => 'Im Abschnitt', 'modal' => 'Als Dialog über der Seite'],
'picker' => ['cards' => 'Karten', 'select' => 'Auswahlfeld'],
'width' => ['normal' => 'Standard', 'narrow' => 'Schmal', 'wide' => 'Breit'],
'spacing' => ['normal' => 'Standard', 'compact' => 'Kompakt', 'airy' => 'Großzügig'],
'columns' => ['auto' => 'Automatisch', 'one' => 'Immer eine Spalte'],
'button' => ['solid' => 'Gefüllt', 'outline' => 'Umrandet'],
];
}
public function settingLabels(): array
{
return [
'mode' => ['Formularführung', 'Langes Formular: alle Bereiche stehen auf einer Seite, bedingte Felder erscheinen je nach Eingabe. Mehrstufiger Dialog: die Bereiche werden Schritt für Schritt mit Fortschrittsanzeige durchlaufen. Vorgabe: Langes Formular.'],
'open' => ['Präsentation', 'Im Abschnitt: das Formular steht direkt auf der Seite. Als Dialog: ein Button öffnet das Formular über der Seite; auf dem Smartphone füllt er den Bildschirm. Vorgabe: Im Abschnitt.'],
'picker' => ['Auswahl des Anfragegrundes', 'Karten zeigen die Gründe mit Beschreibung nebeneinander, das Auswahlfeld braucht wenig Platz. Bei nur einem Grund entfällt die Auswahl. Vorgabe: Karten.'],
'width' => ['Breite des Formulars', 'Vorgabe: Standard. Das Formular nutzt höchstens die Breite des Platzes.'],
'spacing' => ['Abstände', 'Abstand zwischen den Feldern. Vorgabe: Standard.'],
'columns' => ['Anordnung der Felder', 'Automatisch: Felder gleicher Art (zum Beispiel Vor- und Nachname) stehen bei viel Platz nebeneinander. Vorgabe: Automatisch.'],
'button' => ['Buttondarstellung', 'Gilt für den Button, der den Dialog öffnet, und für „Weiter“ und „Absenden“. Vorgabe: Gefüllt.'],
];
}
public function settingGroups(): array
{
return [
'Formularführung und Präsentation' => ['mode', 'open'],
'Gestaltung' => ['picker', 'width', 'spacing', 'columns', 'button'],
];
}
public function dataRules(): array
{
return [];
return [
// Nur die Existenz: Eine später zurückgezogene Vorlage darf eine Seite nicht unspeicherbar machen. Öffentlich
// erscheint ein Grund nur, solange sein Formular veröffentlicht ist (resolve()).
'reasons' => ['nullable', 'array', 'max:'.self::MAX_REASONS],
'reasons.*.key' => ['nullable', 'string', 'max:60'],
'reasons.*.label' => ['required', 'string', 'max:80'],
'reasons.*.description' => ['nullable', 'string', 'max:200'],
'reasons.*.template' => ['required', 'integer', Rule::exists('inquiry_templates', 'id')],
'reasons.*.issue' => ['nullable', 'string', 'max:60'],
'reasons.*.subissue' => ['nullable', 'string', 'max:60'],
'question' => ['nullable', 'string', 'max:160'],
'open_label' => ['nullable', 'string', 'max:60'],
];
}
public function dataFields(): array
{
return [];
return [
Repeater::make('reasons')
->label('Anfragegründe')
->helperText('Diese Gründe bietet die Seite an. Wählt der Besucher einen Grund, erscheint genau das zugeordnete Formular. Bei nur einem Grund entfällt die Auswahl. Ein Button kann einen Grund vorwählen: Adresse der Seite mit „?grund=Kennung“ (die Kennung steht unter dem Grund).')
->schema([
Hidden::make('key'),
TextInput::make('label')->label('Bezeichnung für Besucher')->required()->maxLength(80)->placeholder('Zum Beispiel: Kurse & Workshops'),
Textarea::make('description')->label('Kurzbeschreibung (optional)')->rows(2)->maxLength(200)->helperText('Erscheint auf der Karte unter der Bezeichnung.'),
Select::make('template')->label('Formular')->required()->searchable()->live()
->options(fn (): array => self::templateOptions())
->afterStateUpdated(function (Set $set): void {
$set('issue', null);
$set('subissue', null);
})
->helperText('Nur veröffentlichte Formulare stehen zur Auswahl. Inhalt, Felder und Bedingungen pflegst du unter „Kommunikation → Formulare & Befragungen“.'),
Select::make('issue')->label('Anliegen im Formular vorwählen (optional)')->live()
->options(fn (Get $get): array => self::issueOptions((int) $get('template')))
->placeholder('Keines: das Formular fragt selbst')
->visible(fn (Get $get): bool => self::issueOptions((int) $get('template')) !== [])
->afterStateUpdated(fn (Set $set) => $set('subissue', null))
->helperText('Mehrere Gründe können dasselbe Formular nutzen und sich im Anliegen unterscheiden. Die Frage nach dem Anliegen entfällt dann.'),
Select::make('subissue')->label('Unteranliegen vorwählen (optional)')
->options(fn (Get $get): array => self::subissueOptions((int) $get('template'), (string) $get('issue')))
->placeholder('Keines')
->visible(fn (Get $get): bool => filled($get('issue')) && self::subissueOptions((int) $get('template'), (string) $get('issue')) !== []),
Text::make(fn (Get $get): string => filled($get('key')) ? 'Kennung für Links: grund='.$get('key') : 'Die Kennung für Links entsteht beim Speichern.'),
])
->minItems(1)
->maxItems(self::MAX_REASONS)
->reorderableWithButtons()
->collapsible()
->addActionLabel('Anfragegrund hinzufügen')
->itemLabel(fn (array $state): ?string => $state['label'] ?? null),
TextInput::make('question')->label('Frage zur Auswahl des Grundes')->maxLength(160)->placeholder('Worum geht es?')
->helperText('Steht über der Auswahl, wenn mehr als ein Grund angeboten wird. Leer: „Worum geht es?“.'),
TextInput::make('open_label')->label('Beschriftung des Buttons (nur Dialog)')->maxLength(60)->placeholder('Anfrage stellen')
->helperText('Erscheint nur bei der Präsentation „Als Dialog über der Seite“.'),
];
}
public function refRules(): array
{
return [
// Nur die Existenz: Ein später zurückgezogener Einstieg darf eine Seite nicht unspeicherbar machen (Seiten bleiben
// konsistent). Auswahl im Editor sind nur veröffentlichte Einstiege; öffentlich erscheint nichts, solange er es nicht ist.
'entry' => ['required', 'integer', Rule::exists('inquiry_entries', 'id')],
// Nur die Existenz (Seiten der ersten Fassung): Ein später zurückgezogener Einstieg darf eine Seite nicht unspeicherbar machen.
'entry' => ['nullable', 'integer', Rule::exists('inquiry_entries', 'id')],
];
}
@@ -84,33 +186,167 @@ public function refFields(): array
{
return [
Select::make('entry')
->label('Anfrage-Einstieg')
->required()
->label('Bisheriger Anfrage-Einstieg')
->searchable()
->options(fn (): array => InquiryEntry::query()->live()->orderBy('name')->pluck('name', 'id')->all())
->helperText('Nur veröffentlichte Einstiege stehen zur Auswahl. Anliegen, Felder, Bedingungen, Texte und Empfänger pflegst du in der Kommunikationsverwaltung („Kommunikation → Anfrage-Einstiege“), nicht hier. Eine Änderung dort gilt sofort auf allen Seiten, die diesen Einstieg verwenden.'),
Text::make('Ein Einstieg ist die konkrete Nutzungsvariante einer Anfragevorlage. Mehrere Seiten können denselben Einstieg einbinden.'),
->options(fn (): array => InquiryEntry::query()->orderBy('name')->pluck('name', 'id')->all())
->visible(fn (Get $get): bool => filled($get('entry')))
->helperText('Diese Seite nutzt noch einen Anfrage-Einstieg der ersten Fassung. Er wird nur angezeigt, solange oben kein Anfragegrund eingetragen ist. Neue Seiten brauchen keinen Einstieg.'),
];
}
public function editorSchema(): array
public function clean(array $data, array $refs, WidgetErrors $errors): array
{
return parent::editorSchema();
$reasons = [];
$taken = [];
foreach (array_values((array) ($data['reasons'] ?? [])) as $reason) {
$reason = is_array($reason) ? $reason : [];
$key = Keys::valid($reason['key'] ?? null) && ! in_array($reason['key'], $taken, true) ? $reason['key'] : Keys::from((string) ($reason['label'] ?? ''), $taken);
$taken[] = $key;
$reasons[] = [
'key' => $key,
'label' => trim((string) ($reason['label'] ?? '')),
'description' => filled($reason['description'] ?? null) ? trim((string) $reason['description']) : null,
'template' => (int) ($reason['template'] ?? 0),
'issue' => filled($reason['issue'] ?? null) ? (string) $reason['issue'] : null,
'subissue' => filled($reason['issue'] ?? null) && filled($reason['subissue'] ?? null) ? (string) $reason['subissue'] : null,
];
}
$data['reasons'] = $reasons;
if ($reasons === [] && ! filled($refs['entry'] ?? null)) {
$errors->add('data.reasons', 'Mindestens ein Anfragegrund ist nötig.');
}
return [$data, $refs];
}
public function resolve(array $widget, RenderContext $context): array
{
$id = $widget['refs']['entry'] ?? null;
$entry = is_numeric($id) ? InquiryEntry::query()->with('template')->find((int) $id) : null;
$reasons = array_values(array_filter((array) ($widget['data']['reasons'] ?? []), 'is_array'));
return ['entry' => $entry, 'entryLive' => $entry?->isLive() ?? false];
if ($reasons === []) {
$id = $widget['refs']['entry'] ?? null;
$entry = is_numeric($id) ? InquiryEntry::query()->with('template')->find((int) $id) : null;
return ['legacy' => true, 'entry' => $entry, 'entryLive' => $entry?->isLive() ?? false, 'config' => null, 'unavailable' => []];
}
$templates = InquiryTemplate::query()->with('publishedVersion')->whereIn('id', array_unique(array_map(fn (array $r): int => (int) ($r['template'] ?? 0), $reasons)))->get()->keyBy('id');
$settings = (array) ($widget['settings'] ?? []);
$mode = ($settings['mode'] ?? 'compact') === 'wizard' ? 'wizard' : 'compact';
$live = [];
$unavailable = [];
foreach ($reasons as $reason) {
$template = $templates[(int) ($reason['template'] ?? 0)] ?? null;
$label = (string) ($reason['label'] ?? '');
if ($template === null || $template->archived_at !== null || $template->publishedVersion === null) {
$unavailable[] = $label;
continue;
}
$query = array_filter([
'anliegen' => $reason['issue'] ?? null,
'unteranliegen' => $reason['subissue'] ?? null,
'modus' => $mode,
], fn (mixed $value): bool => filled($value));
$suffix = '?'.http_build_query($query);
$live[] = [
'key' => (string) ($reason['key'] ?? ''),
'label' => $label,
'description' => $reason['description'] ?? null,
'definitionUrl' => route('inquiry.form.definition', $template).$suffix,
'stateUrl' => route('inquiry.form.state', $template).$suffix,
'submitUrl' => route('inquiry.form.submit', $template).$suffix,
];
}
$config = $live === [] ? null : [
'open' => ($settings['open'] ?? 'inline') === 'modal' ? 'modal' : 'inline',
'picker' => ($settings['picker'] ?? 'cards') === 'select' ? 'select' : 'cards',
'question' => filled($widget['data']['question'] ?? null) ? (string) $widget['data']['question'] : 'Worum geht es?',
'openLabel' => filled($widget['data']['open_label'] ?? null) ? (string) $widget['data']['open_label'] : 'Anfrage stellen',
'preview' => $context->preview,
'reasons' => $live,
];
return ['legacy' => false, 'entry' => null, 'entryLive' => false, 'config' => $config, 'unavailable' => $unavailable];
}
public function summary(array $widget): string
{
$id = $widget['refs']['entry'] ?? null;
$name = is_numeric($id) ? InquiryEntry::query()->whereKey((int) $id)->value('name') : null;
$reasons = array_values(array_filter((array) ($widget['data']['reasons'] ?? []), 'is_array'));
return $name !== null ? 'Anfrage: '.$name : 'Kein Anfrage-Einstieg gewählt';
if ($reasons === []) {
$id = $widget['refs']['entry'] ?? null;
$name = is_numeric($id) ? InquiryEntry::query()->whereKey((int) $id)->value('name') : null;
return $name !== null ? 'Anfrage: '.$name : 'Kein Anfragegrund gewählt';
}
$labels = array_map(fn (array $reason): string => (string) ($reason['label'] ?? ''), $reasons);
$mode = ($widget['settings']['mode'] ?? 'compact') === 'wizard' ? 'mehrstufig' : 'langes Formular';
$open = ($widget['settings']['open'] ?? 'inline') === 'modal' ? 'Dialog' : 'im Abschnitt';
return 'Anfrage: '.implode(', ', $labels).' ('.$mode.', '.$open.')';
}
/**
* @return array<int, string>
*/
private static function templateOptions(): array
{
return InquiryTemplate::query()->whereNotNull('published_version_id')->whereNull('archived_at')->orderBy('name')->pluck('name', 'id')->all();
}
/**
* @return array<string, string>
*/
private static function issueOptions(int $templateId): array
{
$options = [];
foreach ((array) (self::definition($templateId)['issues'] ?? []) as $issue) {
$options[(string) $issue['key']] = (string) $issue['label'];
}
return $options;
}
/**
* @return array<string, string>
*/
private static function subissueOptions(int $templateId, string $issue): array
{
$options = [];
foreach ((array) (self::definition($templateId)['issues'] ?? []) as $top) {
if ($top['key'] !== $issue) {
continue;
}
foreach ((array) $top['children'] as $child) {
$options[(string) $child['key']] = (string) $child['label'];
}
}
return $options;
}
/**
* @return array<string, mixed>
*/
private static function definition(int $templateId): array
{
if ($templateId <= 0) {
return [];
}
return (array) (InquiryTemplate::query()->with('publishedVersion')->find($templateId)?->publishedVersion?->definition ?? []);
}
}