CI Feature / test (push) Successful in 5m32s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
82 lines
4.4 KiB
PHP
82 lines
4.4 KiB
PHP
<?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
|
||
->heading('Fassungen')
|
||
->emptyStateHeading('Noch keine Fassung veröffentlicht')
|
||
->emptyStateDescription('Mit „Veröffentlichen“ entsteht die erste Fassung.')
|
||
->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);
|
||
}
|
||
}
|