clusev/app/Livewire/Modals/ConfirmAction.php

76 lines
1.9 KiB
PHP

<?php
namespace App\Livewire\Modals;
use App\Models\AuditEvent;
use Illuminate\Support\Facades\Auth;
use LivewireUI\Modal\ModalComponent;
/**
* Generic R5 confirmation dialog for destructive / stateful actions.
*
* The opener passes copy + an audit descriptor + a follow-up event. On confirm
* we persist exactly one AuditEvent and re-dispatch the opener's event so the
* originating page can apply its own state change. The modal stays
* domain-agnostic and is reused for every confirm in the app (R5).
*/
class ConfirmAction extends ModalComponent
{
public string $heading = 'Aktion bestätigen';
public string $body = '';
public string $confirmLabel = 'Bestätigen';
public bool $danger = false;
public string $icon = '';
/** Audit descriptor — one row written on confirm. */
public string $auditAction = '';
public ?string $auditTarget = null;
public ?int $serverId = null;
/** Follow-up event re-dispatched to the page after a successful confirm. */
public string $event = '';
/** @var array<string, mixed> */
public array $params = [];
public string $notify = 'Aktion ausgeführt.';
public static function modalMaxWidth(): string
{
return 'md';
}
public function confirm(): void
{
if ($this->auditAction !== '') {
AuditEvent::create([
'user_id' => Auth::id(),
'server_id' => $this->serverId,
'actor' => Auth::user()?->name ?? 'system',
'action' => $this->auditAction,
'target' => $this->auditTarget,
'ip' => request()->ip(),
]);
}
if ($this->event !== '') {
$this->dispatch($this->event, ...$this->params);
}
$this->dispatch('notify', message: $this->notify);
$this->closeModal();
}
public function render()
{
return view('livewire.modals.confirm-action');
}
}