Files
laraundfelix/app/Filament/Pages/KommunikationEinstellungen.php
T
LarsandClaude Sonnet 5.5 c6a8167601 Baue die Verwaltung Kommunikation, die öffentliche Anfrageschnittstelle und das CMS-Widget Anfrage
Anliegen, Feldkatalog, Feldgruppen, Anfragevorlagen mit Fassungen, Anfrage-Einstiege, Posteingang mit
Status und Notizen, Einstellungen mit Empfängern. Das Widget speichert nur die Kennung eines veröffentlichten
Einstiegs; Formular, Bedingungen und Annahme kommen zur Laufzeit vom Server (Kompaktformular und
mehrstufiger Dialog aus derselben Beschreibung).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 17:41:55 +01:00

177 lines
7.4 KiB
PHP

<?php
namespace App\Filament\Pages;
use App\Domain\Communication\CommunicationAccess;
use App\Domain\Communication\CommunicationSettings;
use App\Domain\Communication\ExampleConfiguration;
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 = 7;
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('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 !== '')));
}
}