mailwolt/app/Livewire/Ui/Domain/Modal/DomainEditModal.php

111 lines
3.8 KiB
PHP

<?php
namespace App\Livewire\Ui\Domain\Modal;
use App\Models\Domain;
use LivewireUI\Modal\ModalComponent;
class DomainEditModal extends ModalComponent
{
public $domain;
public $description;
public $is_active;
public array $tags = [];
public array $tagPalette = ['#22c55e','#06b6d4','#a855f7','#f59e0b','#ef4444','#3b82f6'];
public function mount(int $domainId)
{
$d = Domain::findOrFail($domainId);
if ($d->is_system) { $this->dispatch('toast', state:'error', text:'System-Domain kann nicht bearbeitet werden.'); $this->dispatch('closeModal'); return; }
$this->domain = $d->domain;
$this->description = $d->description;
$this->is_active = $d->is_active;
$this->tags = [];
$raw = $d->tags ?? [];
if (is_string($raw)) {
$raw = array_values(array_filter(array_map('trim', explode(',', $raw))));
foreach ($raw as $lbl) $this->tags[] = ['label'=>$lbl, 'color'=>$this->tagPalette[0]];
} elseif (is_array($raw)) {
foreach ($raw as $t) {
$label = is_array($t) ? trim((string)($t['label'] ?? '')) : trim((string)$t);
if ($label === '') continue;
$color = $this->normalizeHex(is_array($t) ? ($t['color'] ?? '') : '') ?? $this->tagPalette[0];
$this->tags[] = ['label'=>$label, 'color'=>$color];
}
}
if (empty($this->tags)) $this->tags = [['label'=>'', 'color'=>$this->tagPalette[0]]];
}
public function addTag(): void
{
$this->tags[] = ['label'=>'', 'color'=>$this->tagPalette[0]];
}
public function removeTag(int $i): void
{
unset($this->tags[$i]);
$this->tags = array_values($this->tags);
}
public function pickTagColor(int $i, string $hex): void
{
if (!isset($this->tags[$i])) return;
$hex = $this->normalizeHex($hex);
if ($hex) $this->tags[$i]['color'] = $hex;
}
private function normalizeHex(?string $hex): ?string
{
$hex = trim((string)$hex);
if ($hex === '') return null;
if ($hex[0] !== '#') $hex = "#{$hex}";
return preg_match('/^#[0-9a-fA-F]{6}$/', $hex) ? strtolower($hex) : null;
}
public function save()
{
$this->validate([
'description' => 'nullable|string|max:500',
'is_active' => 'boolean',
'tags' => 'array|max:50',
'tags.*.label' => 'nullable|string|max:40',
'tags.*.color' => ['nullable','regex:/^#[0-9a-fA-F]{6}$/'],
]);
$d = Domain::where('domain', $this->domain)->firstOrFail();
if ($d->is_system) { $this->dispatch('toast', state:'error', text:'System-Domain kann nicht bearbeitet werden.'); return; }
$out = [];
foreach ($this->tags as $t) {
$label = trim((string)($t['label'] ?? ''));
if ($label === '') continue;
$color = $this->normalizeHex($t['color'] ?? '') ?? $this->tagPalette[0];
$out[] = ['label'=>$label, 'color'=>$color];
}
$d->update([
'description' => $this->description,
'is_active' => $this->is_active,
'tags' => $out,
]);
$this->dispatch('domain-updated');
$this->dispatch('closeModal');
$this->dispatch('toast',
type: 'done',
badge: 'Domain',
title: 'Domain aktualisiert',
text: 'Die Domain <b>' . e($d->domain) . '</b> wurde erfolgreich aktualisiert. Alle Änderungen sind sofort aktiv.',
duration: 6000,
);
}
public function render()
{
return view('livewire.ui.domain.modal.domain-edit-modal', [
'tagPalette' => $this->tagPalette,
]);
}
}