Files
laraundfelix/app/Filament/Resources/Communication/Templates/RelationManagers/VersionsRelationManager.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

79 lines
4.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Filament\Resources\Communication\Templates\RelationManagers;
use App\Domain\Communication\Inquiry;
use App\Domain\Communication\InquiryTemplate;
use App\Domain\Communication\InquiryTemplateVersion;
use App\Domain\Communication\Publisher;
use App\Filament\Resources\Communication\Templates\InquiryTemplateResource;
use App\Models\User;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Validation\ValidationException;
/**
* Fassungen einer Anfragevorlage: unveränderlich, jederzeit nachvollziehbar. Eine frühere Fassung lässt sich wieder in
* Kraft setzen oder als Entwurf laden.
*/
class VersionsRelationManager extends RelationManager
{
protected static string $relationship = 'versions';
protected static ?string $title = 'Fassungen';
protected static ?bool $isLazyLoaded = false;
public function table(Table $table): Table
{
/** @var InquiryTemplate $template */
$template = $this->getOwnerRecord();
return $table
->columns([
TextColumn::make('version')->label('Fassung')->prefix('Nr. '),
TextColumn::make('published_at')->label('Veröffentlicht')->dateTime('d.m.Y H:i'),
TextColumn::make('published_by')->label('Von')->state(fn (InquiryTemplateVersion $record): string => (string) User::query()->whereKey($record->published_by)->value('name')),
TextColumn::make('note')->label('Bemerkung')->placeholder('–'),
TextColumn::make('state')->label('Zustand')->badge()
->color(fn (string $state): string => $state === 'Gültig' ? 'success' : 'gray')
->state(fn (InquiryTemplateVersion $record): string => $record->id === $this->getOwnerRecord()->published_version_id ? 'Gültig' : 'Früher'),
TextColumn::make('inquiries')->label('Anfragen')->state(fn (InquiryTemplateVersion $record): int => Inquiry::query()->where('template_version_id', $record->id)->count()),
])
->recordActions([
Action::make('restore')
->label('Wieder in Kraft setzen')
->visible(fn (InquiryTemplateVersion $record): bool => $record->id !== $template->fresh()->published_version_id)
->requiresConfirmation()
->modalDescription('Besucher sehen danach wieder diese Fassung. Der Entwurf bleibt unverändert. Eingegangene Anfragen bleiben ihrer damaligen Fassung zugeordnet.')
->action(function (InquiryTemplateVersion $record): void {
try {
app(Publisher::class)->restoreVersion($this->getOwnerRecord(), $record);
} catch (ValidationException $e) {
Notification::make()->title('Nicht möglich')->body(implode(' ', $e->errors()['draft'] ?? []))->danger()->send();
return;
}
Notification::make()->title('Fassung '.$record->version.' ist wieder gültig.')->success()->send();
$this->redirect(InquiryTemplateResource::getUrl('edit', ['record' => $this->getOwnerRecord()]));
}),
Action::make('load')
->label('Als Entwurf laden')
->color('gray')
->requiresConfirmation()
->modalDescription('Der aktuelle Entwurf wird durch den Stand dieser Fassung ersetzt. Veröffentlicht wird dadurch nichts.')
->action(function (InquiryTemplateVersion $record): void {
$this->getOwnerRecord()->forceFill(['draft' => $record->source])->save();
Notification::make()->title('Der Entwurf entspricht jetzt Fassung '.$record->version.'.')->success()->send();
$this->redirect(InquiryTemplateResource::getUrl('edit', ['record' => $this->getOwnerRecord()]));
}),
])
->defaultSort('version', 'desc')
->paginated(false);
}
}