label('Feldgruppe anlegen') ->modalHeading('Neue Feldgruppe') ->modalDescription('Die Felder legst du danach auf der Seite der Gruppe fest.') ->modalSubmitActionLabel('Feldgruppe anlegen') ->schema([TextInput::make('name')->label('Fachliche Bezeichnung')->required()->maxLength(120)]) ->action(function (array $data): void { $group = InquiryFieldGroup::query()->create([ 'key' => Keys::from($data['name'], InquiryFieldGroup::query()->pluck('key')->all()), 'name' => $data['name'], 'items' => [], 'sort_order' => ((int) InquiryFieldGroup::query()->max('sort_order')) + 10, ]); Notification::make()->title('Die Feldgruppe ist angelegt.')->success()->send(); $this->redirect(InquiryFieldGroupResource::getUrl('edit', ['record' => $group])); }), ]; } public function moveGroup(int $id, string $direction): void { $group = InquiryFieldGroup::query()->findOrFail($id); $all = InquiryFieldGroup::query()->orderBy('sort_order')->orderBy('id')->get()->values(); $index = $all->search(fn (InquiryFieldGroup $item): bool => $item->id === $group->id); $target = $direction === 'up' ? $index - 1 : $index + 1; if ($index === false || $target < 0 || $target >= $all->count()) { return; } $order = $all->all(); [$order[$index], $order[$target]] = [$order[$target], $order[$index]]; foreach ($order as $position => $item) { $item->forceFill(['sort_order' => ($position + 1) * 10])->save(); } } protected function catalogRows(): array { $templates = InquiryTemplate::query()->get(['id', 'draft']); $names = InquiryField::query()->pluck('name', 'key'); $groups = InquiryFieldGroup::query()->orderBy('sort_order')->orderBy('id')->get(); $rows = []; foreach ($groups as $i => $group) { $url = InquiryFieldGroupResource::getUrl('edit', ['record' => $group]); $state = $group->archived_at !== null ? 'archiviert' : ($group->active ? 'aktiv' : 'inaktiv'); $used = $templates->filter(function (InquiryTemplate $template) use ($group): bool { foreach ((array) ($template->draft['steps'] ?? []) as $step) { foreach ((array) ($step['items'] ?? []) as $item) { if (($item['group'] ?? null) === $group->key) { return true; } } } return false; })->count(); $fieldNames = array_map(fn (array $item): string => (string) ($names[$item['field']] ?? $item['field']), $group->itemList()); $rows[] = [ 'schluessel' => 'g'.$group->id, 'titel' => $group->name, 'meta' => 'Feldgruppe · '.count($fieldNames).' '.(count($fieldNames) === 1 ? 'Feld' : 'Felder'), 'text' => implode(', ', $fieldNames), 'symbol' => 'modul', 'url' => $url, 'chips' => [ match ($state) { 'aktiv' => ['label' => 'Wird angeboten', 'ton' => 'gut'], 'inaktiv' => ['label' => 'Deaktiviert', 'ton' => 'leise'], default => ['label' => 'Archiviert', 'ton' => 'leise'], }, ['label' => $used === 0 ? 'In keiner Vorlage' : ($used === 1 ? 'In 1 Vorlage' : "In $used Vorlagen"), 'ton' => 'neutral'], ], 'zustand' => [$state], 'aktionen' => [ ['label' => 'Bearbeiten', 'href' => $url, 'icon' => 'stift'], ...($i > 0 ? [['label' => 'Nach oben', 'wire' => "moveGroup({$group->id}, 'up')"]] : []), ...($i < $groups->count() - 1 ? [['label' => 'Nach unten', 'wire' => "moveGroup({$group->id}, 'down')"]] : []), ], ]; } return $rows; } protected function zustandOptionen(): array { return ['alle' => 'Jeder Zustand', 'aktiv' => 'Wird angeboten', 'inaktiv' => 'Deaktiviert', 'archiviert' => 'Archiviert']; } protected function suchPlatzhalter(): string { return 'Bezeichnung oder Feldname'; } protected function einheit(): array { return ['Feldgruppe', 'Feldgruppen']; } protected function leer(): array { return ['titel' => 'Noch keine Feldgruppen', 'text' => 'Mit „Feldgruppe anlegen“ oben entsteht die erste Gruppe, zum Beispiel „Kontaktinformationen“.']; } }