Ersetze die Builder-Liste des Formular-Editors durch eine Formularansicht mit Ziehen und Ablegen
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>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-28 08:08:39 +01:00
1 parent 79c4e877ab
commit 380a3013c7
10 files changed
+1860 -225

No files matched your search

+240
View File
@@ -2,6 +2,7 @@
namespace App\Domain\Communication;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
@@ -271,6 +272,245 @@ public function persist(array $editor): array
});
}
/** Beschriftungen der Wirkungen am Element, je Art des Ziels. */
public const EFFECT_LABELS = [
'step' => ['show' => 'Bereich nur anzeigen, wenn …', 'hide' => 'Bereich ausblenden, wenn …', 'skip' => 'Bereich überspringen, wenn …'],
'group' => ['show' => 'Teilbereich nur anzeigen, wenn …', 'hide' => 'Teilbereich ausblenden, wenn …'],
'field' => ['show' => 'Feld nur anzeigen, wenn …', 'hide' => 'Feld ausblenden, wenn …', 'require' => 'Pflichtfeld, wenn …'],
];
/**
* Baumansicht für die Formularoberfläche (ADR-62): Ein Teilbereich trägt seine Felder selbst (items), die
* Ende-Markierung der flachen Ansicht entfällt. Die Umwandlung ist verlustfrei: fromTree(toTree(x)) liefert dieselbe
* Struktur, die persist() in denselben Entwurf überführt.
*
* @param array<string, mixed> $view Ergebnis von expand()
* @return array{areas: list<array<string, mixed>>, rules: list<array<string, mixed>>}
*/
public static function toTree(array $view): array
{
$areas = [];
foreach (array_values((array) ($view['areas'] ?? [])) as $area) {
$area = is_array($area) ? $area : [];
$items = [];
$section = null;
foreach (array_values((array) ($area['items'] ?? [])) as $block) {
$block = is_array($block) ? $block : [];
$data = is_array($block['data'] ?? null) ? $block['data'] : [];
switch ((string) ($block['type'] ?? '')) {
case 'issue':
$section = null;
$items[] = ['kind' => 'issue', 'issues' => array_values(array_filter((array) ($data['issues'] ?? []), 'is_string'))];
break;
case 'end':
$section = null;
break;
case 'section':
$items[] = ['kind' => 'section', ...$data, 'items' => []];
$section = array_key_last($items);
break;
case 'field':
if ($section !== null) {
$items[$section]['items'][] = ['kind' => 'field', ...$data];
} else {
$items[] = ['kind' => 'field', ...$data];
}
break;
}
}
$areas[] = [...Arr::except($area, ['items']), 'items' => $items];
}
return ['areas' => $areas, 'rules' => array_values((array) ($view['rules'] ?? []))];
}
/**
* Umkehrung von toTree(): die flache Ansicht, die persist() erwartet. Nach jedem Teilbereich steht eine
* Ende-Markierung, damit folgende Einzelfelder nicht in ihn fallen.
*
* @param array<string, mixed> $tree
* @return array{areas: list<array<string, mixed>>, rules: list<array<string, mixed>>}
*/
public static function fromTree(array $tree): array
{
$areas = [];
$field = fn (array $item): array => ['type' => 'field', 'data' => Arr::except($item, ['kind', '_id'])];
foreach (array_values((array) ($tree['areas'] ?? [])) as $area) {
$area = is_array($area) ? $area : [];
$blocks = [];
foreach (array_values((array) ($area['items'] ?? [])) as $item) {
$item = is_array($item) ? $item : [];
switch ((string) ($item['kind'] ?? '')) {
case 'issue':
$blocks[] = ['type' => 'issue', 'data' => ['issues' => array_values(array_filter((array) ($item['issues'] ?? []), 'is_string'))]];
break;
case 'section':
$blocks[] = ['type' => 'section', 'data' => Arr::except($item, ['kind', '_id', 'items'])];
foreach (array_values((array) ($item['items'] ?? [])) as $inner) {
$blocks[] = $field(is_array($inner) ? $inner : []);
}
$blocks[] = ['type' => 'end', 'data' => []];
break;
case 'field':
$blocks[] = $field($item);
break;
}
}
$areas[] = [...Arr::except($area, ['items', '_id']), 'items' => $blocks];
}
return ['areas' => $areas, 'rules' => array_values((array) ($tree['rules'] ?? []))];
}
/**
* Prüft die Baumansicht vor dem Speichern. Was nur gekürzt oder ignoriert würde, wird hier benannt, damit die
* Verwaltung nichts stillschweigend verliert.
*
* @param array<string, mixed> $tree
* @return list<string>
*/
public function validateTree(array $tree): array
{
$errors = [];
$seen = [];
$issueQuestions = 0;
$limit = function (string $where, string $what, mixed $value, int $max) use (&$errors): void {
if (mb_strlen(trim((string) $value)) > $max) {
$errors[] = "$where: $what darf höchstens $max Zeichen lang sein.";
}
};
foreach (array_values((array) ($tree['areas'] ?? [])) as $index => $area) {
$area = is_array($area) ? $area : [];
$title = trim((string) ($area['title'] ?? ''));
$where = 'Bereich '.($index + 1).($title !== '' ? ' „'.$title.'“' : '');
if ($title === '') {
$errors[] = "$where: Der Titel fehlt.";
}
$limit($where, 'Der Titel', $title, 120);
$limit($where, 'Die Einleitung', $area['intro'] ?? '', 600);
$checkField = function (array $field) use (&$errors, &$seen, $where, $limit): void {
$label = trim((string) ($field['label'] ?? ''));
$name = $label !== '' ? '„'.$label.'“' : 'Ein Feld';
if ($label === '') {
$errors[] = "$where: Ein Feld hat keine Frage oder Beschriftung.";
}
$limit($where, "Die Frage $name", $label, 200);
$limit($where, "Der Hilfetext von $name", $field['help'] ?? '', 600);
$type = FieldType::tryFrom((string) ($field['type'] ?? ''));
if ($type === null) {
$errors[] = "$where: Bei $name fehlt die Art der Antwort.";
} elseif ($type->hasOptions()) {
$options = array_filter((array) ($field['options'] ?? []), fn (mixed $option): bool => is_array($option) && trim((string) ($option['label'] ?? '')) !== '');
if ($options === []) {
$errors[] = "$where: Bei $name fehlen die Auswahlwerte.";
}
foreach ($options as $option) {
$limit($where, "Ein Auswahlwert von $name", $option['label'], 120);
}
}
$key = (string) ($field['key'] ?? '');
if ($key !== '' && ! str_starts_with($key, 'tmp_')) {
if (isset($seen[$key])) {
$errors[] = "$where: $name steht mehrfach im Formular.";
}
$seen[$key] = true;
}
};
foreach (array_values((array) ($area['items'] ?? [])) as $item) {
$item = is_array($item) ? $item : [];
if (($item['kind'] ?? null) === 'issue') {
$issueQuestions++;
} elseif (($item['kind'] ?? null) === 'section') {
$label = trim((string) ($item['label'] ?? ''));
if ($label === '') {
$errors[] = "$where: Ein Teilbereich hat keine Überschrift.";
}
$limit($where, 'Die Überschrift eines Teilbereichs', $label, 200);
$limit($where, 'Die Erläuterung eines Teilbereichs', $item['description'] ?? '', 600);
foreach (array_values((array) ($item['items'] ?? [])) as $inner) {
$checkField(is_array($inner) ? $inner : []);
}
} elseif (($item['kind'] ?? null) === 'field') {
$checkField($item);
}
}
}
if ($issueQuestions > 1) {
$errors[] = 'Die Frage nach dem Anliegen darf nur einmal im Formular vorkommen.';
}
return array_values(array_unique($errors));
}
/**
* Auswahllisten für die Formularoberfläche: Feldarten, Verwendungen, Wirkungen, Vergleiche, Anliegen und die Felder
* des Katalogs mit Verwendung in anderen Formularen.
*
* @return array<string, mixed>
*/
public function clientConfig(?int $exceptTemplate = null): array
{
$usage = $this->usage($exceptTemplate);
return [
'types' => array_map(fn (FieldType $type): array => ['value' => $type->value, 'label' => $type->label(), 'options' => $type->hasOptions(), 'control' => $type->canControl()], FieldType::cases()),
'purposes' => array_map(fn (FieldPurpose $purpose): array => ['value' => $purpose->value, 'label' => $purpose->label(), 'types' => array_map(fn (FieldType $type): string => $type->value, $purpose->types())], FieldPurpose::cases()),
'effects' => self::EFFECT_LABELS,
'operators' => TemplateCompiler::OPERATORS,
'issues' => InquiryIssue::tree(),
'catalog' => InquiryField::query()->offered()->orderBy('name')->get()->map(fn (InquiryField $field): array => [
'key' => $field->key,
'name' => $field->name,
'label' => $field->label,
'help' => $field->help,
'type' => $field->type->value,
'options' => $field->optionList(),
'purpose' => $field->purpose?->value,
'used_in' => implode(', ', $usage['fields'][$field->key] ?? []),
])->values()->all(),
];
}
/**
* Eine neue Zeile in der Karteikarte der Bedingungen (leer: Standardwerte).
*