adrema/modules/Mailgateway/Components/Form.php

120 lines
3.6 KiB
PHP
Raw Normal View History

2024-10-20 21:19:07 +02:00
<?php
namespace Modules\Mailgateway\Components;
2024-10-24 23:17:23 +02:00
use Modules\Mailgateway\Models\Mailgateway;
2024-10-24 22:42:30 +02:00
use App\Mailgateway\Types\Type;
2024-10-20 21:19:07 +02:00
use Illuminate\Support\Collection;
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 = '';
2024-10-24 22:42:30 +02:00
public ?Type $type = null;
#[Validate('required|string')]
public ?string $typeClass = null;
2024-10-20 21:19:07 +02:00
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',
2024-10-24 22:42:30 +02:00
...$this->type ? collect($this->type::rules())->mapWithKeys(fn ($rules, $key) => ["type.{$key}" => $rules]) : [],
2024-10-20 21:19:07 +02:00
];
}
public function validationAttributes(): array
{
return [
2024-10-24 22:42:30 +02:00
'type' => 'Typ',
2024-10-20 21:19:07 +02:00
'name' => 'Beschreibung',
'domain' => 'Domain',
2024-10-24 22:42:30 +02:00
...$this->type ? collect($this->type::fieldNames())->mapWithKeys(fn ($attribute, $key) => ["params.{$key}" => $attribute]) : [],
2024-10-20 21:19:07 +02:00
];
}
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;
2024-10-24 22:42:30 +02:00
$this->type = $this->model->type;
$this->typeClass = get_class($this->model->type);
2024-10-20 21:19:07 +02:00
}
}
2024-10-24 22:42:30 +02:00
public function fields(): array
2024-10-20 21:19:07 +02:00
{
2024-10-24 22:42:30 +02:00
return $this->type ? $this->type::fields() : [];
2024-10-20 21:19:07 +02:00
}
2024-10-24 22:42:30 +02:00
public function updatedTypeClass(?string $type): void
2024-10-20 21:19:07 +02:00
{
2024-10-24 22:42:30 +02:00
if (!$type) {
return;
}
$this->type = $type::from([]);
2024-10-20 21:19:07 +02:00
}
#[On('onStoreFromModal')]
public function onSave(): void
{
$this->validate();
2024-10-24 22:42:30 +02:00
if (!$this->type->works()) {
2024-10-20 21:19:07 +02:00
throw ValidationException::withMessages(['connection' => 'Verbindung fehlgeschlagen.']);
}
$payload = [
'name' => $this->name,
'domain' => $this->domain,
2024-10-24 22:42:30 +02:00
'type' => $this->type,
2024-10-20 21:19:07 +02:00
];
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 />
2024-10-24 22:42:30 +02:00
<x-form::select name="typeClass" wire:model.live="typeClass" label="Typ" :options="$types" required />
2024-10-20 21:19:07 +02:00
@foreach($this->fields() as $index => $field)
<x-form::text
wire:key="index"
2024-10-24 22:42:30 +02:00
wire:model="type.{{$field['name']}}"
2024-10-20 21:19:07 +02:00
: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;
}
}