2024-10-20 21:19:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Mailgateway\Components;
|
|
|
|
|
|
|
|
use App\Mailgateway\Models\Mailgateway;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
use Livewire\Attributes\On;
|
|
|
|
use Livewire\Attributes\Validate;
|
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class Form extends Component
|
|
|
|
{
|
|
|
|
|
|
|
|
public string $name = '';
|
|
|
|
public string $domain = '';
|
|
|
|
public array $params = [];
|
|
|
|
#[Validate('required')]
|
|
|
|
public ?string $cls = null;
|
|
|
|
public Collection $types;
|
2024-10-20 22:02:43 +02:00
|
|
|
public ?Mailgateway $model = null;
|
2024-10-20 21:19:07 +02:00
|
|
|
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
'domain' => 'required|string|max:255',
|
|
|
|
'cls' => ['required', 'string', 'max:255', Rule::in(app('mail-gateways'))],
|
|
|
|
'params' => 'present|array',
|
2024-10-20 22:02:43 +02:00
|
|
|
...$this->cls ? collect($this->cls::rules())->mapWithKeys(fn ($rules, $key) => ["params.{$key}" => $rules]) : [],
|
2024-10-20 21:19:07 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validationAttributes(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'cls' => 'Typ',
|
|
|
|
'name' => 'Beschreibung',
|
|
|
|
'domain' => 'Domain',
|
|
|
|
...$this->cls ? collect($this->cls::fieldNames())->mapWithKeys(fn ($attribute, $key) => ["params.{$key}" => $attribute]) : [],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-10-20 22:02:43 +02:00
|
|
|
public function mount(?string $id = null): void
|
2024-10-20 21:19:07 +02:00
|
|
|
{
|
|
|
|
$this->types = app('mail-gateways')->map(fn ($gateway) => [
|
|
|
|
'name' => $gateway::name(),
|
|
|
|
'id' => $gateway,
|
|
|
|
]);
|
|
|
|
|
2024-10-20 22:02:43 +02:00
|
|
|
if ($id) {
|
|
|
|
$this->model = Mailgateway::find($id);
|
|
|
|
$this->name = $this->model->name;
|
|
|
|
$this->domain = $this->model->domain;
|
|
|
|
$this->cls = get_class($this->model->type);
|
|
|
|
$this->params = (array) $this->model->type;
|
2024-10-20 21:19:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updatedType(string $type): void
|
|
|
|
{
|
|
|
|
$this->params = $type::defaults();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fields(): array
|
|
|
|
{
|
|
|
|
return $this->cls ? $this->cls::fields() : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[On('onStoreFromModal')]
|
|
|
|
public function onSave(): void
|
|
|
|
{
|
|
|
|
$this->validate();
|
|
|
|
|
|
|
|
if (!app($this->cls)->setParams($this->params)->works()) {
|
|
|
|
throw ValidationException::withMessages(['connection' => 'Verbindung fehlgeschlagen.']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$payload = [
|
|
|
|
'name' => $this->name,
|
|
|
|
'domain' => $this->domain,
|
|
|
|
'type' => ['cls' => $this->cls, 'params' => $this->params],
|
|
|
|
];
|
2024-10-20 22:02:43 +02:00
|
|
|
if ($this->model) {
|
|
|
|
$this->model->update($payload);
|
2024-10-20 21:19:07 +02:00
|
|
|
} else {
|
|
|
|
Mailgateway::create($payload);
|
|
|
|
}
|
|
|
|
$this->dispatch('closeModal');
|
2024-10-20 21:23:47 +02:00
|
|
|
$this->dispatch('refresh-page');
|
2024-10-20 21:19:07 +02:00
|
|
|
$this->dispatch('success', 'Erfolgreich gespeichert.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return <<<'HTML'
|
|
|
|
<div>
|
|
|
|
<form class="grid grid-cols-2 gap-3">
|
|
|
|
<x-form::text name="name" wire:model="name" label="Beschreibung" required />
|
|
|
|
<x-form::text name="domain" wire:model="domain" label="Domain" required />
|
|
|
|
<x-form::select name="cls" wire:model.live="cls" label="Typ" :options="$types" required />
|
|
|
|
@foreach($this->fields() as $index => $field)
|
|
|
|
<x-form::text
|
|
|
|
wire:key="index"
|
|
|
|
wire:model="params.{{$field['name']}}"
|
|
|
|
:label="$field['label']"
|
|
|
|
:type="$field['type']"
|
|
|
|
:name="$field['name']"
|
2024-10-20 22:02:43 +02:00
|
|
|
:required="str_contains('required', $field['validator'])"
|
2024-10-20 21:19:07 +02:00
|
|
|
></x-form::text>
|
|
|
|
@endforeach
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
}
|