*/ 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 */ 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 */ 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 */ 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 */ 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 $editor * @return array{steps: array, groups: array, fields: array, hasIssue: bool, issues: list} */ 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 $editor * @return array */ 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 $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 $editor * @return array */ 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 $editor * @return array */ 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 */ 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(); } }