CI Feature / test (push) Successful in 4m18s
Der Aufbau eines Formulars zeigt Bereiche als Karten, Teilbereiche als eingerahmte Einheit darin und Felder als Zeilen (Frage, Art der Antwort, Pflicht). Hilfetext, Auswahlwerte, Bedingung und weitere Einstellungen öffnen als Pop-up. Bereiche, Teilbereiche und Felder lassen sich ziehen (SortableJS aus Filament) oder per Pfeiltasten und über "Verschieben nach" ordnen; Rückgängig nimmt Änderungen bis zum Speichern zurück. Der Browser führt den Baum und übergibt ihn beim Speichern als Text; FormEditor prüft ihn (validateTree) und überführt ihn über fromTree in dieselbe flache Ansicht wie bisher. Entwurf, Katalog und Engine bleiben unverändert, keine Migration. Tests: Die Livewire-Tests des Editors setzen jetzt den Baumtext (editor_json) statt der Builder-Blöcke; neu sind Baumumwandlung ohne Verlust, Teilbereich mit Feldern, Prüfung und Konfiguration. Das ist bewusst: Der Builder entfällt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
296 lines
12 KiB
PHP
296 lines
12 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Support;
|
||
|
||
use App\Domain\Communication\FieldType;
|
||
use App\Domain\Communication\FormEditor;
|
||
use App\Domain\Communication\FormTexts;
|
||
use App\Domain\Communication\InquiryField;
|
||
use App\Domain\Communication\InquiryIssue;
|
||
use App\Domain\Communication\TemplateCompiler;
|
||
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\Forms\Components\ViewField;
|
||
use Filament\Schemas\Components\Component;
|
||
use Filament\Schemas\Components\Section;
|
||
use Filament\Schemas\Components\Tabs;
|
||
use Filament\Schemas\Components\Tabs\Tab;
|
||
use Filament\Schemas\Components\Text;
|
||
use Filament\Schemas\Components\Utilities\Get;
|
||
|
||
/**
|
||
* Formular-Editor (ADR-61): ein Formular wird an einer Stelle bearbeitet. Es besteht aus Bereichen (im mehrstufigen Dialog
|
||
* je ein Schritt), darin Felder und Teilbereiche, Bedingungen direkt am Element. Felder werden hier angelegt und
|
||
* geändert; sie landen im selben Katalog wie bisher (FormEditor). Die Regelliste bleibt für alles, was sich nicht an ein
|
||
* Element heften lässt. Alles sind Auswahlfelder und Listen; es gibt keine freie Eingabe von Ausdrücken oder JSON.
|
||
*/
|
||
final class TemplateEditor
|
||
{
|
||
/**
|
||
* @return list<Component>
|
||
*/
|
||
public static function schema(?int $templateId = null): array
|
||
{
|
||
return [
|
||
TextInput::make('name')->label('Name des Formulars')->required()->maxLength(120)
|
||
->helperText('So heißt das Formular in der Verwaltung und in der Auswahl des Widgets „Anfrage“.'),
|
||
Textarea::make('description')->label('Beschreibung (nur Verwaltung)')->rows(2)->maxLength(400),
|
||
Tabs::make('vorlage')->persistTabInQueryString('bereich')->columnSpanFull()->tabs([
|
||
Tab::make('Aufbau')->icon('heroicon-o-queue-list')->schema(self::structure($templateId)),
|
||
Tab::make('Texte')->icon('heroicon-o-chat-bubble-bottom-center-text')->schema(self::texts()),
|
||
Tab::make('Weitere Regeln')->icon('heroicon-o-adjustments-horizontal')->schema(self::rules()),
|
||
]),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Der Aufbau ist eine eigene Formularansicht (resources/views/filament/communication/form-editor.blade.php). Sie führt
|
||
* den Baum aus Bereichen, Teilbereichen und Feldern im Browser und übergibt ihn beim Speichern als Text (editor_json);
|
||
* FormEditor prüft und speichert ihn.
|
||
*
|
||
* @return list<Component>
|
||
*/
|
||
private static function structure(?int $templateId): array
|
||
{
|
||
return [
|
||
ViewField::make('editor_json')
|
||
->hiddenLabel()
|
||
->view('filament.communication.form-editor')
|
||
->viewData(['config' => app(FormEditor::class)->clientConfig($templateId)])
|
||
->columnSpanFull(),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return list<Component>
|
||
*/
|
||
private static function rules(): array
|
||
{
|
||
return [
|
||
Section::make()->schema([
|
||
Text::make('Hier stehen Regeln, die sich nicht an ein einzelnes Feld oder einen Bereich heften lassen: weitere Regeln für dasselbe Ziel und Vorbelegungen. Bedingungen für ein Feld oder einen Bereich legst du direkt am Element an. Eine Bedingung darf nur auf Felder verweisen, die im Formular weiter oben stehen.'),
|
||
Repeater::make('editor.rules')
|
||
->label('Weitere Regeln')
|
||
->schema([
|
||
Hidden::make('pos'),
|
||
Select::make('effect')->label('Wirkung')->required()->live()->options(TemplateCompiler::EFFECTS),
|
||
Select::make('target')->label('Ziel')->required()->live()->searchable()
|
||
->options(fn ($livewire, Get $get): array => self::targets(self::flat($livewire), (string) $get('effect'))),
|
||
Select::make('value')->label('Vorbelegen mit')->live()
|
||
->options(fn (Get $get): array => str_starts_with((string) $get('target'), 'field:') ? self::fieldValues(substr((string) $get('target'), 6)) : [])
|
||
->visible(fn (Get $get): bool => $get('effect') === 'preset')->required(fn (Get $get): bool => $get('effect') === 'preset'),
|
||
Select::make('match')->label('Es müssen zutreffen')->options(['all' => 'alle Bedingungen', 'any' => 'mindestens eine Bedingung'])->default('all')->required(),
|
||
Repeater::make('conditions')
|
||
->label('Bedingungen')
|
||
->schema([
|
||
Select::make('source')->label('Wenn …')->required()->live()->searchable()
|
||
->options(fn ($livewire): array => self::sources(self::flat($livewire))),
|
||
Select::make('op')->label('Vergleich')->required()->options(TemplateCompiler::OPERATORS)->default('eq')->live(),
|
||
Select::make('values')->label('Wert')->required()->multiple()
|
||
->options(fn ($livewire, Get $get): array => self::sourceValues(self::flat($livewire), (string) $get('source'))),
|
||
])
|
||
->minItems(1)
|
||
->addActionLabel('Bedingung hinzufügen'),
|
||
])
|
||
->collapsible()
|
||
->addActionLabel('Regel hinzufügen')
|
||
->itemLabel(fn (array $state, $livewire): string => (TemplateCompiler::EFFECTS[$state['effect'] ?? ''] ?? 'Neue Regel').(($state['target'] ?? null) ? ' → '.self::targetName((string) $state['target'], self::flat($livewire)) : '')),
|
||
]),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return list<Component>
|
||
*/
|
||
private static function texts(): array
|
||
{
|
||
$fields = [];
|
||
|
||
foreach (FormTexts::FIELDS as $key => [$label, $default, $help, $multiline]) {
|
||
$field = $multiline
|
||
? Textarea::make("draft.texts.$key")->rows(2)->maxLength(1500)
|
||
: TextInput::make("draft.texts.$key")->maxLength(200);
|
||
|
||
$fields[] = $field->label($label)->placeholder($default !== '' ? $default : 'Kein Text')->helperText($help);
|
||
}
|
||
|
||
return [
|
||
Section::make('Texte des Formulars')
|
||
->description('Alle Texte des Formulars werden hier gepflegt, nie im CMS. Leer lassen: es gilt der grau angezeigte Standardtext.')
|
||
->schema($fields),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Der aktuelle Aufbau in der flachen Ansicht (Bereiche, Felder, Teilbereiche, Ende-Markierungen), aus dem Text der Formularansicht.
|
||
*
|
||
* @return array<string, mixed>
|
||
*/
|
||
private static function flat(mixed $livewire): array
|
||
{
|
||
$tree = json_decode((string) ($livewire->data['editor_json'] ?? ''), true);
|
||
|
||
return FormEditor::fromTree(is_array($tree) ? $tree : []);
|
||
}
|
||
|
||
/**
|
||
* Inhalt des Formulars in Reihenfolge: Bereiche, Teilbereiche, Felder, und ob die Frage nach dem Anliegen vorkommt.
|
||
*
|
||
* @param array<string, mixed> $editor
|
||
* @return array{steps: array<string, string>, groups: array<string, string>, fields: array<string, string>, hasIssue: bool, issues: list<string>}
|
||
*/
|
||
private static function inventory(array $editor): array
|
||
{
|
||
$steps = [];
|
||
$groups = [];
|
||
$fields = [];
|
||
$hasIssue = false;
|
||
$issues = [];
|
||
|
||
foreach (array_values((array) ($editor['areas'] ?? [])) as $area) {
|
||
$key = (string) ($area['key'] ?? '');
|
||
|
||
if ($key !== '') {
|
||
$steps[$key] = (string) (($area['title'] ?? '') ?: 'Bereich');
|
||
}
|
||
|
||
foreach (array_values((array) ($area['items'] ?? [])) as $block) {
|
||
$data = (array) ($block['data'] ?? []);
|
||
$type = $block['type'] ?? null;
|
||
|
||
if ($type === 'issue') {
|
||
$hasIssue = true;
|
||
$issues = array_values(array_filter((array) ($data['issues'] ?? []), 'is_string'));
|
||
} elseif ($type === 'section' && filled($data['key'] ?? null)) {
|
||
$groups[(string) $data['key']] = (string) (($data['label'] ?? '') ?: 'Teilbereich');
|
||
} elseif ($type === 'field' && filled($data['key'] ?? null)) {
|
||
$fields[(string) $data['key']] = (string) (($data['label'] ?? '') ?: 'Feld');
|
||
}
|
||
}
|
||
}
|
||
|
||
return ['steps' => $steps, 'groups' => $groups, 'fields' => $fields, 'hasIssue' => $hasIssue, 'issues' => $issues];
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $editor
|
||
* @return array<string, mixed>
|
||
*/
|
||
private static function targets(array $editor, string $effect): array
|
||
{
|
||
$inventory = self::inventory($editor);
|
||
$steps = [];
|
||
foreach ($inventory['steps'] as $key => $title) {
|
||
$steps["step:$key"] = 'Bereich: '.$title;
|
||
}
|
||
$groups = [];
|
||
foreach ($inventory['groups'] as $key => $name) {
|
||
$groups["group:$key"] = 'Teilbereich: '.$name;
|
||
}
|
||
$fields = [];
|
||
foreach ($inventory['fields'] as $key => $name) {
|
||
$fields["field:$key"] = 'Feld: '.$name;
|
||
}
|
||
|
||
return match ($effect) {
|
||
'skip' => $steps,
|
||
'require', 'preset' => $fields,
|
||
default => ['Bereiche' => $steps, 'Teilbereiche' => $groups, 'Felder' => $fields],
|
||
};
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $editor
|
||
*/
|
||
private static function targetName(string $target, array $editor = []): string
|
||
{
|
||
[$kind, $key] = array_pad(explode(':', $target, 2), 2, '');
|
||
$inventory = self::inventory($editor);
|
||
|
||
return match ($kind) {
|
||
'step' => (string) ($inventory['steps'][$key] ?? $key),
|
||
'field' => (string) ($inventory['fields'][$key] ?? InquiryField::query()->where('key', $key)->value('name') ?? $key),
|
||
'group' => (string) ($inventory['groups'][$key] ?? $key),
|
||
default => $key,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* Felder, auf deren Wert sich eine Bedingung stützen darf: gespeicherte Auswahlfelder und Ja/Nein-Felder. Mit
|
||
* Zustandspfad der Bedingung nur Felder, die im Formular davor stehen (sonst lehnt die Prüfung die Regel ab).
|
||
*
|
||
* @param array<string, mixed> $editor
|
||
* @return array<string, string>
|
||
*/
|
||
private static function sources(array $editor): array
|
||
{
|
||
$catalog = InquiryField::query()->get()->keyBy('key');
|
||
$sources = [];
|
||
|
||
foreach ((array) ($editor['areas'] ?? []) as $area) {
|
||
foreach ((array) ($area['items'] ?? []) as $block) {
|
||
$data = (array) ($block['data'] ?? []);
|
||
$key = (string) ($data['key'] ?? '');
|
||
|
||
if (($block['type'] ?? null) === 'issue') {
|
||
$sources['anliegen'] = 'Anliegen';
|
||
$sources['unteranliegen'] = 'Unteranliegen';
|
||
} elseif (($block['type'] ?? null) === 'field' && isset($catalog[$key]) && $catalog[$key]->type->canControl()) {
|
||
$sources[$key] = (string) (($data['label'] ?? '') ?: $catalog[$key]->label);
|
||
}
|
||
}
|
||
}
|
||
|
||
return $sources;
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $editor
|
||
* @return array<string, string>
|
||
*/
|
||
private static function sourceValues(array $editor, string $source): array
|
||
{
|
||
if ($source === 'anliegen' || $source === 'unteranliegen') {
|
||
$only = self::inventory($editor)['issues'];
|
||
$options = [];
|
||
|
||
foreach (InquiryIssue::tree() as $issue) {
|
||
if ($only !== [] && ! in_array($issue['key'], $only, true)) {
|
||
continue;
|
||
}
|
||
|
||
if ($source === 'anliegen') {
|
||
$options[$issue['key']] = $issue['label'];
|
||
|
||
continue;
|
||
}
|
||
|
||
foreach ($issue['children'] as $child) {
|
||
$options[$child['key']] = $issue['label'].' › '.$child['label'];
|
||
}
|
||
}
|
||
|
||
return $options;
|
||
}
|
||
|
||
return self::fieldValues($source);
|
||
}
|
||
|
||
/**
|
||
* @return array<string, string>
|
||
*/
|
||
private static function fieldValues(string $key): array
|
||
{
|
||
$field = InquiryField::query()->where('key', $key)->first();
|
||
|
||
if ($field === null || ! in_array($field->type, [FieldType::Select, FieldType::Multiselect, FieldType::Boolean], true)) {
|
||
return [];
|
||
}
|
||
|
||
return collect($field->optionList())->pluck('label', 'key')->all();
|
||
}
|
||
}
|