label('Anliegen anlegen') ->modalHeading('Neues Anliegen') ->modalDescription('Bezeichnung und Einordnung genügen. Die interne Kennung wird einmalig vergeben.') ->modalSubmitActionLabel('Anliegen anlegen') ->schema([ Select::make('parent_id')->label('Einordnung') ->options(fn (): array => InquiryIssue::query()->whereNull('parent_id')->orderBy('sort_order')->pluck('label', 'id')->all()) ->placeholder('Anliegen auf oberster Ebene') ->helperText('Alternativ als Unteranliegen eines bestehenden Anliegens.'), TextInput::make('label')->label('Öffentliche Bezeichnung')->required()->maxLength(120), Textarea::make('description')->label('Beschreibung (optional)')->rows(2)->maxLength(300), ]) ->action(function (array $data): void { $issue = InquiryIssue::query()->create([ 'parent_id' => $data['parent_id'] ?? null, 'key' => Keys::from($data['label'], InquiryIssue::query()->pluck('key')->all()), 'label' => $data['label'], 'description' => $data['description'] ?? null, 'sort_order' => ((int) InquiryIssue::query()->where('parent_id', $data['parent_id'] ?? null)->max('sort_order')) + 10, ]); Notification::make()->title('Das Anliegen ist angelegt.')->success()->send(); $this->redirect(InquiryIssueResource::getUrl('edit', ['record' => $issue])); }), ]; } /** Ein Anliegen vor oder hinter seinem Nachbarn derselben Ebene einordnen. */ public function moveIssue(int $id, string $direction): void { $issue = InquiryIssue::query()->findOrFail($id); $siblings = InquiryIssue::query()->where('parent_id', $issue->parent_id)->orderBy('sort_order')->orderBy('id')->get()->values(); $index = $siblings->search(fn (InquiryIssue $item): bool => $item->id === $issue->id); $target = $direction === 'up' ? $index - 1 : $index + 1; if ($index === false || $target < 0 || $target >= $siblings->count()) { return; } $order = $siblings->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 { $all = InquiryIssue::query()->orderBy('sort_order')->orderBy('id')->get(); $children = $all->whereNotNull('parent_id')->groupBy('parent_id'); $rows = []; $tops = $all->whereNull('parent_id')->values(); $row = function (InquiryIssue $issue, ?InquiryIssue $parent, int $position, int $count) use (&$rows): void { $url = InquiryIssueResource::getUrl('edit', ['record' => $issue]); $state = $issue->archived_at !== null ? 'archiviert' : ($issue->active ? 'aktiv' : 'inaktiv'); $rows[] = [ 'schluessel' => 'i'.$issue->id, 'titel' => ($parent !== null ? '↳ ' : '').$issue->label, 'meta' => $parent !== null ? 'Unteranliegen von „'.$parent->label.'“' : 'Anliegen', 'text' => (string) ($issue->description ?? ''), 'symbol' => 'modul', 'url' => $url, 'chips' => [ match ($state) { 'aktiv' => ['label' => 'Wird angeboten', 'ton' => 'gut'], 'inaktiv' => ['label' => 'Deaktiviert', 'ton' => 'leise'], default => ['label' => 'Archiviert', 'ton' => 'leise'], }, ], 'zustand' => [$state], 'aktionen' => [ ['label' => 'Bearbeiten', 'href' => $url, 'icon' => 'stift'], ...($position > 0 ? [['label' => 'Nach oben', 'wire' => "moveIssue({$issue->id}, 'up')"]] : []), ...($position < $count - 1 ? [['label' => 'Nach unten', 'wire' => "moveIssue({$issue->id}, 'down')"]] : []), ], ]; }; foreach ($tops as $i => $top) { $row($top, null, $i, $tops->count()); $kids = ($children[$top->id] ?? collect())->values(); foreach ($kids as $j => $child) { $row($child, $top, $j, $kids->count()); } } return $rows; } protected function zustandOptionen(): array { return ['alle' => 'Jeder Zustand', 'aktiv' => 'Wird angeboten', 'inaktiv' => 'Deaktiviert', 'archiviert' => 'Archiviert']; } protected function suchPlatzhalter(): string { return 'Bezeichnung oder Beschreibung'; } protected function einheit(): array { return ['Anliegen', 'Anliegen']; } protected function leer(): array { return ['titel' => 'Noch keine Anliegen', 'text' => 'Mit „Anliegen anlegen“ oben entsteht das erste Anliegen, zum Beispiel „Kurse & Workshops“. Auf der Seite „Einstellungen“ lässt sich eine Beispielkonfiguration anlegen.']; } }