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:
1 parent
f255bf5200
commit
fb479c1b01
18 files changed
+1394
-57
No files matched your search
@@ -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 ?? []);
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,9 @@ public function evaluate(array $form, array $values): array
|
||||
}
|
||||
|
||||
foreach ((array) $form['steps'] as $step) {
|
||||
$stepVisible = ! $this->any($byTarget['step:'.$step['key']] ?? [], 'skip', $context);
|
||||
// Ein Bereich lässt sich wie eine Gruppe anzeigen oder ausblenden; „überspringen“ bleibt für ältere Vorlagen gültig.
|
||||
$stepRules = $byTarget['step:'.$step['key']] ?? [];
|
||||
$stepVisible = ! $this->any($stepRules, 'skip', $context) && $this->visible($stepRules, $context);
|
||||
$state['steps'][$step['key']] = $stepVisible;
|
||||
|
||||
foreach ($step['items'] as $item) {
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Communication;
|
||||
|
||||
/**
|
||||
* Der Anfragegrund, mit dem das Widget „Anfrage“ ein veröffentlichtes Formular lädt (ADR-61): ein vorgewähltes Anliegen,
|
||||
* optional ein Unteranliegen, und die Formularführung. Das ist eine Darstellungs- und Vorauswahlentscheidung, keine
|
||||
* Berechtigung: Alles, was ein Anfragegrund wählen kann, bietet das veröffentlichte Formular ohnehin an. Aus dem Grund
|
||||
* entsteht dieselbe Konfiguration wie bei einem Anfrage-Einstieg, damit Auslieferung, Auswertung und Annahme nur eine
|
||||
* Auslegung haben (FormFactory::effective).
|
||||
*/
|
||||
final class FormReason
|
||||
{
|
||||
/**
|
||||
* Bereinigt die Angaben aus der Adresse oder der Anfrage des Browsers: nur bekannte Schlüssel, nur gültige Kennungen.
|
||||
*
|
||||
* @param array<string, mixed> $input
|
||||
* @return array{anliegen: ?string, unteranliegen: ?string, modus: ?string}
|
||||
*/
|
||||
public static function clean(array $input): array
|
||||
{
|
||||
$key = fn (mixed $value): ?string => Keys::valid($value) ? $value : null;
|
||||
$mode = $input['modus'] ?? null;
|
||||
|
||||
return [
|
||||
'anliegen' => $key($input['anliegen'] ?? null),
|
||||
'unteranliegen' => $key($input['unteranliegen'] ?? null),
|
||||
'modus' => $mode === 'wizard' || $mode === 'compact' ? $mode : null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Konfiguration, die FormFactory::effective erwartet. Ein Anliegen, das die Fassung nicht kennt, schränkt
|
||||
* nichts ein (das Formular fragt dann wie gewohnt).
|
||||
*
|
||||
* @param array<string, mixed> $definition aufgelöste Fassung
|
||||
* @param array<string, mixed> $reason
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function config(array $definition, array $reason): array
|
||||
{
|
||||
$config = ['mode' => ($reason['modus'] ?? null) === 'wizard' ? 'wizard' : 'compact', 'open' => 'inline'];
|
||||
$issue = $reason['anliegen'] ?? null;
|
||||
|
||||
foreach ((array) ($definition['issues'] ?? []) as $top) {
|
||||
if ($top['key'] !== $issue) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$config['allowed_issues'] = [$issue];
|
||||
$config['defaults'] = ['anliegen' => $issue];
|
||||
$sub = $reason['unteranliegen'] ?? null;
|
||||
|
||||
if ($sub !== null && in_array($sub, array_column((array) $top['children'], 'key'), true)) {
|
||||
$config['allowed_subissues'] = [$sub];
|
||||
$config['defaults']['unteranliegen'] = $sub;
|
||||
}
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
}
|
||||
@@ -16,9 +16,14 @@
|
||||
*/
|
||||
final class FormToken
|
||||
{
|
||||
public static function issue(int $entryId, int $versionId): string
|
||||
/**
|
||||
* @param string $kind 'entry' (Anfrage-Einstieg) oder 'form' (veröffentlichte Vorlage, vom Widget „Anfrage“ gewählt);
|
||||
* $entryId ist dann die Kennung des Einstiegs beziehungsweise der Vorlage
|
||||
*/
|
||||
public static function issue(int $entryId, int $versionId, string $kind = 'entry'): string
|
||||
{
|
||||
return Crypt::encryptString(json_encode([
|
||||
'k' => $kind === 'form' ? 'form' : 'entry',
|
||||
'e' => $entryId,
|
||||
'v' => $versionId,
|
||||
't' => now()->getTimestamp(),
|
||||
@@ -27,7 +32,7 @@ public static function issue(int $entryId, int $versionId): string
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{e: int, v: int, t: int, n: string}|null
|
||||
* @return array{k: string, e: int, v: int, t: int, n: string}|null
|
||||
*/
|
||||
public static function read(mixed $token): ?array
|
||||
{
|
||||
@@ -45,6 +50,6 @@ public static function read(mixed $token): ?array
|
||||
return null;
|
||||
}
|
||||
|
||||
return ['e' => (int) $data['e'], 'v' => (int) $data['v'], 't' => (int) $data['t'], 'n' => $data['n']];
|
||||
return ['k' => ($data['k'] ?? 'entry') === 'form' ? 'form' : 'entry', 'e' => (int) $data['e'], 'v' => (int) $data['v'], 't' => (int) $data['t'], 'n' => $data['n']];
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,20 @@ public function issueTitle(): string
|
||||
return trim(implode(' › ', array_filter([$this->issue_label, $this->subissue_label])));
|
||||
}
|
||||
|
||||
/**
|
||||
* Woher die Anfrage kam: der Anfrage-Einstieg (älterer Weg) oder das Formular, das das Widget „Anfrage“ eingebunden hat.
|
||||
*/
|
||||
public function channelTitle(): string
|
||||
{
|
||||
if ($this->entry !== null) {
|
||||
return $this->entry->name;
|
||||
}
|
||||
|
||||
$template = $this->templateVersion?->template;
|
||||
|
||||
return $template !== null ? 'Formular „'.$template->name.'“' : 'Formular gelöscht';
|
||||
}
|
||||
|
||||
public function contactTitle(): string
|
||||
{
|
||||
return $this->contact_name ?: ($this->contact_email ?: 'Unbekannt');
|
||||
|
||||
@@ -50,10 +50,62 @@ public function deliver(InquiryEntry $entry, ?int $versionId = null): ?array
|
||||
* @throws SubmissionException
|
||||
*/
|
||||
public function submit(InquiryEntry $entry, array $payload): array
|
||||
{
|
||||
return $this->accept('entry', $entry->id, $entry->id, fn (?int $version): ?array => $this->deliver($entry, $version), $payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Fassung und das wirksame Formular einer veröffentlichten Vorlage für einen Besucher, wie sie das Widget
|
||||
* „Anfrage“ ausliefert. $reason legt den Anfragegrund fest: ein vorgewähltes Anliegen (und Unteranliegen) und die
|
||||
* Formularführung. Null, wenn die Vorlage nicht veröffentlicht oder archiviert ist.
|
||||
*
|
||||
* @param array{anliegen?: ?string, unteranliegen?: ?string, modus?: ?string} $reason
|
||||
* @return array{form: array<string, mixed>, version: InquiryTemplateVersion}|null
|
||||
*/
|
||||
public function deliverForm(InquiryTemplate $template, array $reason = [], ?int $versionId = null): ?array
|
||||
{
|
||||
$template->loadMissing('publishedVersion');
|
||||
|
||||
if ($template->archived_at !== null || $template->publishedVersion === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$version = $template->publishedVersion;
|
||||
|
||||
if ($versionId !== null && $versionId !== $version->id) {
|
||||
$version = InquiryTemplateVersion::query()->where('template_id', $template->id)->find($versionId) ?? $version;
|
||||
}
|
||||
|
||||
return ['form' => $this->factory->effective($version->definition, FormReason::config($version->definition, $reason)), 'version' => $version];
|
||||
}
|
||||
|
||||
/**
|
||||
* Nimmt eine Anfrage für eine veröffentlichte Vorlage an (Widget „Anfrage“). Die Verarbeitung ist dieselbe wie über
|
||||
* einen Einstieg; die Anfrage trägt dann keine Einstiegskennung, sondern verweist über ihre Fassung auf die Vorlage.
|
||||
*
|
||||
* @param array{anliegen?: ?string, unteranliegen?: ?string, modus?: ?string} $reason
|
||||
* @param array<string, mixed> $payload
|
||||
* @return array{reference: string, created: bool}
|
||||
*
|
||||
* @throws SubmissionException
|
||||
*/
|
||||
public function submitForm(InquiryTemplate $template, array $reason, array $payload): array
|
||||
{
|
||||
return $this->accept('form', $template->id, null, fn (?int $version): ?array => $this->deliverForm($template, $reason, $version), $payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable(?int): (array{form: array<string, mixed>, version: InquiryTemplateVersion}|null) $deliver
|
||||
* @param array<string, mixed> $payload
|
||||
* @return array{reference: string, created: bool}
|
||||
*
|
||||
* @throws SubmissionException
|
||||
*/
|
||||
private function accept(string $kind, int $targetId, ?int $entryId, callable $deliver, array $payload): array
|
||||
{
|
||||
$token = FormToken::read($payload['token'] ?? null);
|
||||
|
||||
if ($token === null || $token['e'] !== $entry->id) {
|
||||
if ($token === null || $token['k'] !== $kind || $token['e'] !== $targetId) {
|
||||
throw new SubmissionException('Das Formular ist abgelaufen. Bitte laden Sie die Seite neu; Ihre Eingaben bleiben dabei nicht erhalten.', [], 419);
|
||||
}
|
||||
|
||||
@@ -69,7 +121,7 @@ public function submit(InquiryEntry $entry, array $payload): array
|
||||
return ['reference' => $existing->reference, 'created' => false];
|
||||
}
|
||||
|
||||
$delivered = $this->deliver($entry, $token['v']);
|
||||
$delivered = $deliver($token['v']);
|
||||
|
||||
if ($delivered === null) {
|
||||
throw new SubmissionException('Dieses Anfrageformular ist zurzeit nicht verfügbar.', [], 410);
|
||||
@@ -101,7 +153,7 @@ public function submit(InquiryEntry $entry, array $payload): array
|
||||
try {
|
||||
$inquiry = DB::transaction(fn (): Inquiry => Inquiry::query()->create([
|
||||
'reference' => $this->reference(),
|
||||
'entry_id' => $entry->id,
|
||||
'entry_id' => $entryId,
|
||||
'template_version_id' => $delivered['version']->id,
|
||||
'submission_token' => hash('sha256', $token['n']),
|
||||
'issue_key' => $result['issue']['key'] ?? null,
|
||||
|
||||
@@ -295,7 +295,7 @@ private function rules(array $rules, array $fields, array $tree, bool $hasIssue,
|
||||
}
|
||||
|
||||
$kindOk = match ($effect) {
|
||||
'show', 'hide' => in_array($targetKind, ['group', 'field'], true),
|
||||
'show', 'hide' => in_array($targetKind, ['step', 'group', 'field'], true),
|
||||
'require', 'preset' => $targetKind === 'field',
|
||||
'skip' => $targetKind === 'step',
|
||||
default => false,
|
||||
|
||||
@@ -98,7 +98,7 @@ public static function infolist(Schema $schema): Schema
|
||||
TextEntry::make('status')->label('Bearbeitungsstatus')->badge()
|
||||
->formatStateUsing(fn ($state): string => $state->label())
|
||||
->color(fn ($state): string => $state->color()),
|
||||
TextEntry::make('entry.name')->label('Anfrage-Einstieg')->placeholder('Gelöscht'),
|
||||
TextEntry::make('channel')->label('Eingegangen über')->state(fn (Inquiry $record): string => $record->channelTitle()),
|
||||
TextEntry::make('source_path')->label('Quellseite')->placeholder('Unbekannt'),
|
||||
TextEntry::make('created_at')->label('Eingegangen')->dateTime('d.m.Y H:i'),
|
||||
TextEntry::make('reference')->label('Kennzeichen')->copyable(),
|
||||
|
||||
@@ -58,7 +58,7 @@ public function bestand(): array
|
||||
|
||||
protected function catalogRows(): array
|
||||
{
|
||||
$query = Inquiry::query()->with('entry');
|
||||
$query = Inquiry::query()->with(['entry', 'templateVersion.template']);
|
||||
|
||||
if ($this->anliegen !== 'alle') {
|
||||
$query->where('issue_key', $this->anliegen);
|
||||
@@ -79,7 +79,7 @@ protected function catalogRows(): array
|
||||
$rows[] = [
|
||||
'schluessel' => 'q'.$inquiry->id,
|
||||
'titel' => $inquiry->contactTitle().($inquiry->organisation ? ' · '.$inquiry->organisation : ''),
|
||||
'meta' => $inquiry->created_at->format('d.m.Y H:i').' · '.($inquiry->issueTitle() ?: 'Ohne Anliegen').' · '.($inquiry->entry->name ?? 'Einstieg gelöscht'),
|
||||
'meta' => $inquiry->created_at->format('d.m.Y H:i').' · '.($inquiry->issueTitle() ?: 'Ohne Anliegen').' · '.$inquiry->channelTitle(),
|
||||
'text' => Str::limit((string) $inquiry->message, 180),
|
||||
'suchtext' => implode(' ', array_filter([$inquiry->contact_email, $inquiry->reference, $inquiry->contact_phone])),
|
||||
'symbol' => 'modul',
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
|
||||
use App\Domain\Communication\FormEvaluator;
|
||||
use App\Domain\Communication\FormPayload;
|
||||
use App\Domain\Communication\FormReason;
|
||||
use App\Domain\Communication\FormToken;
|
||||
use App\Domain\Communication\InquiryEntry;
|
||||
use App\Domain\Communication\InquiryService;
|
||||
use App\Domain\Communication\InquiryTemplate;
|
||||
use App\Domain\Communication\SubmissionException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -68,6 +70,59 @@ public function submit(Request $request, InquiryEntry $entry): JsonResponse
|
||||
return $this->json(['reference' => $result['reference']], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Widget „Anfrage“ (ADR-61): das veröffentlichte Formular einer Vorlage für den gewählten Anfragegrund. Der Grund
|
||||
* (Anliegen, Unteranliegen, Formularführung) steht in der Adresse, damit Zustand und Absendung ohne weitere
|
||||
* Angaben dasselbe Formular meinen.
|
||||
*/
|
||||
public function formDefinition(Request $request, InquiryTemplate $template): JsonResponse
|
||||
{
|
||||
$delivered = $this->service->deliverForm($template, FormReason::clean($request->query()));
|
||||
|
||||
if ($delivered === null) {
|
||||
return $this->json(['message' => 'Dieses Anfrageformular ist zurzeit nicht verfügbar.'], 404);
|
||||
}
|
||||
|
||||
return $this->json([
|
||||
...FormPayload::definition($delivered['form'], $this->evaluator),
|
||||
'token' => FormToken::issue($template->id, $delivered['version']->id, 'form'),
|
||||
'csrf' => csrf_token(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function formState(Request $request, InquiryTemplate $template): JsonResponse
|
||||
{
|
||||
$delivered = $this->service->deliverForm($template, FormReason::clean($request->query()));
|
||||
|
||||
if ($delivered === null) {
|
||||
return $this->json(['message' => 'Dieses Anfrageformular ist zurzeit nicht verfügbar.'], 404);
|
||||
}
|
||||
|
||||
$values = $request->input('values');
|
||||
|
||||
if (! is_array($values) || strlen((string) json_encode($values)) > 40000) {
|
||||
return $this->json(['message' => 'Die Eingaben sind ungültig.'], 422);
|
||||
}
|
||||
|
||||
return $this->json(FormPayload::state($delivered['form'], $values, $this->evaluator));
|
||||
}
|
||||
|
||||
public function formSubmit(Request $request, InquiryTemplate $template): JsonResponse
|
||||
{
|
||||
try {
|
||||
$result = $this->service->submitForm($template, FormReason::clean($request->query()), [
|
||||
'token' => $request->input('token'),
|
||||
'values' => $request->input('values'),
|
||||
'website' => $request->input('website'),
|
||||
'source' => $request->input('source'),
|
||||
]);
|
||||
} catch (SubmissionException $e) {
|
||||
return $this->json(['message' => $e->getMessage(), 'fields' => (object) $e->fields], $e->getCode() ?: 422);
|
||||
}
|
||||
|
||||
return $this->json(['reference' => $result['reference']], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user