Anliegen, Feldkatalog, Feldgruppen, Anfragevorlagen mit unveränderlichen Fassungen, Anfrage-Einstiege und eingegangene Anfragen. Eine gemeinsame Regelauswertung bedient Besucher, Vorschau und Anfrageservice; Werte nicht mehr relevanter Felder werden verworfen, Anliegen und Auswahlwerte serverseitig geprüft, doppeltes Absenden bleibt eine Anfrage. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
180 lines
6.8 KiB
PHP
180 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Communication;
|
|
|
|
/**
|
|
* Baut aus einer veröffentlichten Fassung und der Konfiguration eines Anfrage-Einstiegs das wirksame Formular:
|
|
* zugelassene Anliegen, Texte, Darstellung und Vorbelegung. Dieselbe Funktion bedient Besucher, Vorschau und
|
|
* Anfrageservice, damit nirgends eine zweite Auslegung entsteht.
|
|
*/
|
|
final class FormFactory
|
|
{
|
|
/**
|
|
* @param array<string, mixed> $definition aufgelöste Fassung (TemplateCompiler)
|
|
* @param array<string, mixed> $config Konfiguration des Einstiegs (veröffentlichter Stand oder Entwurf)
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function effective(array $definition, array $config): array
|
|
{
|
|
$allowed = array_values(array_filter((array) ($config['allowed_issues'] ?? []), 'is_string'));
|
|
$allowedSub = array_values(array_filter((array) ($config['allowed_subissues'] ?? []), 'is_string'));
|
|
$tree = [];
|
|
|
|
foreach ((array) ($definition['issues'] ?? []) as $issue) {
|
|
if ($allowed !== [] && ! in_array($issue['key'], $allowed, true)) {
|
|
continue;
|
|
}
|
|
|
|
$children = (array) $issue['children'];
|
|
$chosen = array_values(array_filter($children, fn (array $child): bool => in_array($child['key'], $allowedSub, true)));
|
|
$issue['children'] = $chosen !== [] ? $chosen : $children;
|
|
$tree[] = $issue;
|
|
}
|
|
|
|
$form = $definition;
|
|
$form['issues'] = $tree;
|
|
$form['texts'] = FormTexts::merge((array) ($definition['texts'] ?? []), (array) ($config['texts'] ?? []));
|
|
$form['mode'] = ($config['mode'] ?? 'compact') === 'wizard' ? 'wizard' : 'compact';
|
|
$form['open'] = ($config['open'] ?? 'inline') === 'button' ? 'button' : 'inline';
|
|
$form['defaults'] = $this->defaults($form, (array) ($config['defaults'] ?? []));
|
|
$form['controls'] = $this->controls($form);
|
|
|
|
return $form;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $form
|
|
* @param array<string, mixed> $wanted
|
|
* @return array<string, string>
|
|
*/
|
|
private function defaults(array $form, array $wanted): array
|
|
{
|
|
$defaults = [];
|
|
$tops = array_column((array) $form['issues'], 'key');
|
|
|
|
if (isset($wanted['anliegen']) && in_array($wanted['anliegen'], $tops, true)) {
|
|
$defaults['anliegen'] = $wanted['anliegen'];
|
|
|
|
foreach ($form['issues'] as $issue) {
|
|
if ($issue['key'] === $wanted['anliegen'] && isset($wanted['unteranliegen']) && in_array($wanted['unteranliegen'], array_column($issue['children'], 'key'), true)) {
|
|
$defaults['unteranliegen'] = $wanted['unteranliegen'];
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ((array) $form['fields'] as $key => $field) {
|
|
$value = $wanted[$key] ?? null;
|
|
|
|
if (! is_string($value) || ! in_array($field['type'], ['select', 'boolean'], true)) {
|
|
continue;
|
|
}
|
|
|
|
$valid = $field['type'] === 'boolean' ? ['ja', 'nein'] : array_column($field['options'], 'key');
|
|
|
|
if (in_array($value, $valid, true)) {
|
|
$defaults[$key] = $value;
|
|
}
|
|
}
|
|
|
|
return $defaults;
|
|
}
|
|
|
|
/**
|
|
* Felder, auf deren Änderung das Formular die Regeln neu auswerten lassen muss.
|
|
*
|
|
* @param array<string, mixed> $form
|
|
* @return list<string>
|
|
*/
|
|
private function controls(array $form): array
|
|
{
|
|
$controls = [];
|
|
|
|
foreach ((array) $form['rules'] as $rule) {
|
|
foreach ($rule['conditions'] as $condition) {
|
|
$controls[$condition['source']] = true;
|
|
}
|
|
}
|
|
|
|
return array_keys($controls);
|
|
}
|
|
|
|
/**
|
|
* Prüft die Konfiguration eines Einstiegs gegen eine Fassung. Leere Liste: gültig.
|
|
*
|
|
* @param array<string, mixed> $definition
|
|
* @param array<string, mixed> $config
|
|
* @return list<string>
|
|
*/
|
|
public function problems(array $definition, array $config): array
|
|
{
|
|
$problems = [];
|
|
$tops = array_column((array) ($definition['issues'] ?? []), 'key');
|
|
$subs = [];
|
|
|
|
foreach ((array) ($definition['issues'] ?? []) as $issue) {
|
|
foreach ($issue['children'] as $child) {
|
|
$subs[$child['key']] = $issue['key'];
|
|
}
|
|
}
|
|
|
|
$allowed = array_values(array_filter((array) ($config['allowed_issues'] ?? []), 'is_string'));
|
|
$allowedSub = array_values(array_filter((array) ($config['allowed_subissues'] ?? []), 'is_string'));
|
|
|
|
foreach ($allowed as $key) {
|
|
if (! in_array($key, $tops, true)) {
|
|
$problems[] = "Das Anliegen „{$key}“ ist in der veröffentlichten Vorlage nicht enthalten.";
|
|
}
|
|
}
|
|
|
|
foreach ($allowedSub as $key) {
|
|
if (! isset($subs[$key])) {
|
|
$problems[] = "Das Unteranliegen „{$key}“ ist in der veröffentlichten Vorlage nicht enthalten.";
|
|
} elseif ($allowed !== [] && ! in_array($subs[$key], $allowed, true)) {
|
|
$problems[] = "Das Unteranliegen „{$key}“ gehört zu einem Anliegen, das dieser Einstieg nicht zulässt.";
|
|
}
|
|
}
|
|
|
|
if (($definition['has_issue'] ?? false) && $allowed !== [] && array_diff($allowed, $tops) === $allowed) {
|
|
$problems[] = 'Keines der zugelassenen Anliegen ist in der Vorlage vorhanden.';
|
|
}
|
|
|
|
foreach ((array) ($config['defaults'] ?? []) as $key => $value) {
|
|
if ($key === 'anliegen') {
|
|
if (! in_array($value, $allowed !== [] ? $allowed : $tops, true)) {
|
|
$problems[] = 'Das vorausgewählte Anliegen ist nicht zugelassen oder nicht in der Vorlage.';
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
if ($key === 'unteranliegen') {
|
|
if (! isset($subs[$value])) {
|
|
$problems[] = 'Das vorausgewählte Unteranliegen ist nicht in der Vorlage.';
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
$field = $definition['fields'][$key] ?? null;
|
|
|
|
if ($field === null || ! in_array($field['type'], ['select', 'boolean'], true)) {
|
|
$problems[] = "Die Vorbelegung für „{$key}“ ist in der veröffentlichten Vorlage nicht möglich.";
|
|
|
|
continue;
|
|
}
|
|
|
|
$valid = $field['type'] === 'boolean' ? ['ja', 'nein'] : array_column($field['options'], 'key');
|
|
|
|
if (! in_array($value, $valid, true)) {
|
|
$problems[] = "Der vorausgewählte Wert für „{$field['label']}“ gibt es nicht mehr.";
|
|
}
|
|
}
|
|
|
|
if (isset($config['mode']) && ! in_array($config['mode'], ['compact', 'wizard'], true)) {
|
|
$problems[] = 'Die Darstellung muss „kompakt“ oder „mehrstufig“ sein.';
|
|
}
|
|
|
|
return $problems;
|
|
}
|
|
}
|