113 lines
3.1 KiB
PHP
113 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Fileshare\Components;
|
|
|
|
use App\Fileshare\ConnectionTypes\ConnectionType;
|
|
use App\Fileshare\Models\Fileshare;
|
|
use Modules\Mailgateway\Models\Mailgateway;
|
|
use Modules\Mailgateway\Types\Type;
|
|
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 array $type = [];
|
|
#[Validate('required|string|max:255|exclude')]
|
|
public ?string $typeClass = null;
|
|
public array $types;
|
|
public ?Fileshare $model = null;
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
'name' => 'required|string|max:255',
|
|
'type' => 'array|exclude',
|
|
];
|
|
}
|
|
|
|
public function validationAttributes(): array
|
|
{
|
|
return [
|
|
'typeClass' => 'Typ',
|
|
'name' => 'Bezeichnung',
|
|
];
|
|
}
|
|
|
|
public function mount(?string $id = null): void
|
|
{
|
|
$this->types = ConnectionType::forSelect();
|
|
|
|
if ($id) {
|
|
$this->model = Fileshare::findOrFail($id);
|
|
$this->name = $this->model->name;
|
|
$this->typeClass = get_class($this->model->type);
|
|
$this->type = $this->model->type->toArray();
|
|
}
|
|
}
|
|
|
|
public function fields(): array
|
|
{
|
|
return $this->typeClass ? $this->typeClass::fields() : [];
|
|
}
|
|
|
|
public function updatedTypeClass(?string $type): void
|
|
{
|
|
if (!$type) {
|
|
return;
|
|
}
|
|
|
|
$this->type = $type::defaults();
|
|
}
|
|
|
|
#[On('onStoreFromModal')]
|
|
public function onSave(): void
|
|
{
|
|
$payload = $this->validate();
|
|
$type = $this->typeClass::from($this->type);
|
|
|
|
if (!$type->check()) {
|
|
throw ValidationException::withMessages(['typeClass' => 'Verbindung fehlgeschlagen']);
|
|
}
|
|
|
|
if ($this->model) {
|
|
$this->model->update([...$payload, 'type' => $type]);
|
|
} else {
|
|
Fileshare::create([
|
|
...$payload,
|
|
'type' => $type,
|
|
]);
|
|
}
|
|
|
|
$this->dispatch('closeModal');
|
|
$this->dispatch('refresh-page');
|
|
$this->dispatch('success', $this->model ? 'Verbindung aktualisiert.' : 'Verbindung erstellt.');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return <<<'HTML'
|
|
<div>
|
|
<form class="grid grid-cols-2 gap-3">
|
|
<x-form::text name="name" wire:model="name" label="Bezeichnung" required />
|
|
<x-form::select name="typeClass" wire:model.live="typeClass" label="Typ" :options="$types" required />
|
|
@foreach($this->fields() as $index => $field)
|
|
<x-form::text
|
|
wire:key="$index"
|
|
wire:model="type.{{$field['key']}}"
|
|
:label="$field['label']"
|
|
:type="$field['type']"
|
|
:name="'type.'.$field['key']"
|
|
required
|
|
></x-form::text>
|
|
@endforeach
|
|
</form>
|
|
</div>
|
|
HTML;
|
|
}
|
|
}
|