131 lines
4.9 KiB
PHP
131 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Ui\Mail;
|
|
|
|
use App\Models\Domain;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Component;
|
|
|
|
class AliasList extends Component
|
|
{
|
|
public string $search = '';
|
|
public bool $showSystemCard = false;
|
|
|
|
#[On('alias:created')]
|
|
#[On('alias:updated')]
|
|
#[On('alias:deleted')]
|
|
public function refreshAliases(): void
|
|
{
|
|
$this->dispatch('$refresh');
|
|
}
|
|
|
|
public function openAliasCreate(int $domainId): void
|
|
{
|
|
$this->dispatch('openModal', component: 'ui.mail.modal.alias-form-modal', arguments: [
|
|
'domainId' => $domainId,
|
|
]);
|
|
}
|
|
|
|
public function openAliasEdit(int $aliasId): void
|
|
{
|
|
// nur Wert übergeben (LivewireUI Modal nimmt Positionsargumente)
|
|
$this->dispatch('openModal', component: 'ui.mail.modal.alias-form-modal', arguments: [
|
|
$aliasId,
|
|
]);
|
|
}
|
|
|
|
public function openAliasDelete(int $aliasId): void
|
|
{
|
|
$this->dispatch('openModal', component: 'ui.mail.modal.alias-delete-modal', arguments: [
|
|
$aliasId,
|
|
]);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$system = Domain::query()->where('is_system', true)->first();
|
|
$term = trim($this->search);
|
|
|
|
$domains = Domain::query()
|
|
->where('is_system', false)
|
|
->withCount(['mailAliases'])
|
|
->with([
|
|
'mailAliases' => fn ($q) => $q
|
|
->withCount('recipients')
|
|
->with(['recipients' => fn($r) => $r->with('mailUser')])
|
|
->orderBy('local'),
|
|
])
|
|
->when($term !== '', function ($q) use ($term) {
|
|
$q->where(function ($w) use ($term) {
|
|
$w->where('domain', 'like', "%{$term}%")
|
|
->orWhereHas('mailAliases', function ($a) use ($term) {
|
|
$a->where('local', 'like', "%{$term}%")
|
|
->orWhereHas('recipients', function ($r) use ($term) {
|
|
$r->where('email', 'like', "%{$term}%")
|
|
->orWhereHas('mailUser', fn($mu) =>
|
|
$mu->where('localpart', 'like', "%{$term}%"));
|
|
});
|
|
});
|
|
});
|
|
})
|
|
->orderBy('domain')
|
|
->get();
|
|
|
|
// Vorbereiten der Alias-Daten (Logik aus dem Blade hierher)
|
|
foreach ($domains as $domain) {
|
|
$domainActive = (bool) ($domain->is_active ?? true);
|
|
|
|
foreach ($domain->mailAliases as $alias) {
|
|
|
|
$alias->effective_active = $domainActive && (bool) ($alias->is_active ?? true);
|
|
|
|
$alias->inactive_reason = null;
|
|
if (!$domainActive) {
|
|
$alias->inactive_reason = 'Domain inaktiv';
|
|
} elseif (!($alias->is_active ?? true)) {
|
|
$alias->inactive_reason = 'Alias inaktiv';
|
|
}
|
|
|
|
$alias->isGroup = $alias->type === 'group';
|
|
$alias->isSingle = !$alias->isGroup;
|
|
$alias->recipientCount = $alias->recipients_count ?? $alias->recipients->count();
|
|
$alias->maxChips = $alias->isGroup ? 2 : 1;
|
|
$alias->extraRecipients= max(0, $alias->recipientCount - $alias->maxChips);
|
|
$alias->shownRecipients= $alias->relationLoaded('recipients')
|
|
? $alias->recipients->take($alias->maxChips)
|
|
: collect();
|
|
|
|
// Quelle (immer)
|
|
$alias->sourceEmail = "{$alias->local}@{$domain->domain}";
|
|
|
|
// Ziele (Text für Pfeil-Zeile)
|
|
if ($alias->isSingle) {
|
|
// 1. Ziel auflösen
|
|
$first = $alias->relationLoaded('recipients') ? $alias->recipients->first() : null;
|
|
if ($first) {
|
|
if ($first->mail_user_id && $first->relationLoaded('mailUser') && $first->mailUser) {
|
|
$alias->arrowTarget = "{$first->mailUser->localpart}@{$domain->domain}";
|
|
} else {
|
|
$alias->arrowTarget = $first->email ?: '—';
|
|
}
|
|
} else {
|
|
$alias->arrowTarget = '—';
|
|
}
|
|
} else {
|
|
// Gruppenlabel
|
|
$label = trim((string)$alias->group_name) !== '' ? $alias->group_name : 'Gruppe';
|
|
$alias->arrowTarget = "{$label} ({$alias->recipientCount})";
|
|
}
|
|
|
|
// Kompletter Pfeil-Text
|
|
$alias->arrowLine = "{$alias->sourceEmail} ⇒ {$alias->arrowTarget}";
|
|
}
|
|
}
|
|
|
|
return view('livewire.ui.mail.alias-list', [
|
|
'domains' => $domains,
|
|
'system' => $this->showSystemCard ? $system : null,
|
|
]);
|
|
}
|
|
}
|