Files
laraundfelix/app/Filament/Pages/KommunikationEinstellungen.php
T
LarsandClaude Sonnet 5.5 7302ac78e9
CI Feature / test (push) Successful in 4m17s
Bearbeite ein Formular vollständig in einem Editor und reduziere die Navigation der Kommunikation
Der Editor zeigt Bereiche mit Feldern und Teilbereichen; Felder werden direkt im Bereich angelegt oder aus dem Katalog
übernommen und landen im selben Katalog wie bisher. Bedingungen stehen am Feld, Teilbereich oder Bereich und werden zu
denselben Regeln wie die Regelliste; alles andere bleibt unter „Weitere Regeln“. Navigation: Anfragen, Formulare &
Befragungen, Einstellungen. Anliegen, Feldkatalog, Feldgruppen und Einstiege bleiben erreichbar (Einstellungen).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 22:37:32 +01:00

192 lines
8.6 KiB
PHP

<?php
namespace App\Filament\Pages;
use App\Domain\Communication\CommunicationAccess;
use App\Domain\Communication\CommunicationSettings;
use App\Domain\Communication\ExampleConfiguration;
use App\Filament\Resources\Communication\Entries\InquiryEntryResource;
use App\Filament\Resources\Communication\Fields\InquiryFieldResource;
use App\Filament\Resources\Communication\Groups\InquiryFieldGroupResource;
use App\Filament\Resources\Communication\Issues\InquiryIssueResource;
use App\Mail\CommunicationTestMail;
use Filament\Actions\Action;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Actions;
use Filament\Schemas\Components\EmbeddedSchema;
use Filament\Schemas\Components\Form;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Facades\Mail;
use RuntimeException;
use Throwable;
/**
* Allgemeine Kommunikationsvorgaben: Empfänger der internen Benachrichtigung und Aufbewahrungsdauer. Die Empfänger
* stehen nur hier; Besucher und das CMS können keine Empfänger festlegen.
*/
class KommunikationEinstellungen extends Page
{
protected static string|\UnitEnum|null $navigationGroup = 'Kommunikation';
protected static ?string $navigationLabel = 'Einstellungen';
protected static ?int $navigationSort = 3;
protected static ?string $slug = 'kommunikation/einstellungen';
/** @var array<string, mixed>|null */
public ?array $data = [];
public static function canAccess(): bool
{
return CommunicationAccess::inbox();
}
public function getTitle(): string|Htmlable
{
return 'Kommunikation: Einstellungen';
}
public function getSubheading(): string|Htmlable|null
{
return 'Wer bei einer neuen Anfrage benachrichtigt wird und wie lange Anfragen aufbewahrt werden.';
}
public function mount(): void
{
$settings = CommunicationSettings::current();
$this->form->fill([
'recipients' => implode("\n", (array) $settings->recipients),
'retention_months' => $settings->retention_months,
]);
}
public function form(Schema $schema): Schema
{
return $schema
->components([
Section::make('Benachrichtigung')->schema([
Textarea::make('recipients')->label('Empfänger der internen Benachrichtigung')->rows(4)
->helperText('Eine E-Mail-Adresse je Zeile. Die Benachrichtigung nennt nur Kennzeichen und Anliegen, nie Kontaktdaten oder Antworten. Ohne Empfänger werden Anfragen trotzdem gespeichert; der Posteingang weist dann auf die fehlende Benachrichtigung hin.')
->rule(fn (): \Closure => function (string $attribute, mixed $value, \Closure $fail): void {
foreach (self::lines((string) $value) as $address) {
if (filter_var($address, FILTER_VALIDATE_EMAIL) === false) {
$fail('„'.$address.'“ ist keine gültige E-Mail-Adresse.');
return;
}
}
if (count(self::lines((string) $value)) > 10) {
$fail('Bitte höchstens zehn Empfänger.');
}
}),
]),
Section::make('Weitere Bausteine')->description('Anliegen und der gemeinsame Feldkatalog gehören zu den Formularen. Im Formular-Editor müssen sie nicht geöffnet werden; hier lassen sie sich einsehen und ordnen.')
->visible(fn (): bool => CommunicationAccess::configure())
->collapsed()
->schema([
Actions::make([
Action::make('issues')->label('Anliegen')->url(InquiryIssueResource::getUrl('index'))->color('gray'),
Action::make('fields')->label('Feldkatalog')->url(InquiryFieldResource::getUrl('index'))->color('gray'),
Action::make('groups')->label('Feldgruppen')->url(InquiryFieldGroupResource::getUrl('index'))->color('gray'),
Action::make('entries')->label('Anfrage-Einstiege (ältere Seiten)')->url(InquiryEntryResource::getUrl('index'))->color('gray'),
]),
]),
Section::make('Aufbewahrung')->schema([
TextInput::make('retention_months')->label('Anfragen löschen nach … Monaten')->numeric()->minValue(1)->maxValue(120)
->helperText('Leer: Anfragen werden nicht automatisch gelöscht. Der Befehl „php artisan communication:purge“ löscht Anfragen, die älter sind. Einzelne Anfragen lassen sich jederzeit im Posteingang löschen. Die Dauer ist mit dem Datenschutzkonzept abzustimmen.'),
]),
])
->statePath('data');
}
public function content(Schema $schema): Schema
{
return $schema->components([
Form::make([EmbeddedSchema::make('form')])
->id('form')
->livewireSubmitHandler('save')
->footer([Actions::make([Action::make('save')->label('Einstellungen speichern')->submit('save')->keyBindings(['mod+s'])])]),
]);
}
public function save(): void
{
$data = $this->form->getState();
CommunicationSettings::current()->update([
'recipients' => self::lines((string) ($data['recipients'] ?? '')),
'retention_months' => filled($data['retention_months'] ?? null) ? (int) $data['retention_months'] : null,
]);
Notification::make()->title('Die Einstellungen sind gespeichert.')->success()->send();
}
protected function getHeaderActions(): array
{
return [
Action::make('test')
->label('Testnachricht senden')
->icon('heroicon-o-envelope')
->color('gray')
->requiresConfirmation()
->modalDescription('Sendet eine Testnachricht an die gespeicherten Empfänger. Sie enthält keine Anfragedaten.')
->action(function (): void {
$recipients = CommunicationSettings::current()->recipientList();
if ($recipients === []) {
Notification::make()->title('Es sind noch keine Empfänger gespeichert.')->warning()->send();
return;
}
try {
Mail::to($recipients)->send(new CommunicationTestMail);
} catch (Throwable $e) {
report($e);
Notification::make()->title('Der Versand ist fehlgeschlagen.')->body($e->getMessage())->danger()->send();
return;
}
Notification::make()->title('Die Testnachricht ist abgesendet.')->body('Der Mailserver hat sie angenommen. Ob sie ankommt, zeigt dein Postfach.')->success()->send();
}),
Action::make('example')
->label('Beispielkonfiguration anlegen')
->icon('heroicon-o-sparkles')
->color('gray')
->visible(fn (): bool => ExampleConfiguration::isEmpty())
->requiresConfirmation()
->modalHeading('Beispielkonfiguration anlegen?')
->modalDescription('Legt Anliegen, Felder, Feldgruppen, vier Anfragevorlagen und fünf Anfrage-Einstiege (Allgemeiner Kontakt, Kurse & Workshops, Schul-AG, Programme & Materialien, Kooperation) an und veröffentlicht sie. Alles lässt sich danach ändern. Das geht nur, solange die Kommunikationsverwaltung noch leer ist.')
->action(function (): void {
try {
app(ExampleConfiguration::class)->install(CommunicationAccess::user());
} catch (RuntimeException $e) {
Notification::make()->title($e->getMessage())->danger()->send();
return;
}
Notification::make()->title('Die Beispielkonfiguration ist angelegt und veröffentlicht.')->success()->send();
$this->redirect(static::getUrl());
}),
];
}
/**
* @return list<string>
*/
private static function lines(string $text): array
{
return array_values(array_unique(array_filter(array_map('trim', preg_split('/[\r\n,;]+/', $text) ?: []), fn (string $line): bool => $line !== '')));
}
}