homeos/app/Livewire/Modals/SmtpSetup.php

133 lines
3.8 KiB
PHP

<?php
namespace App\Livewire\Modals;
use App\Models\Addon;
use App\Services\AddonService;
use App\Services\NotificationService;
use LivewireUI\Modal\ModalComponent;
class SmtpSetup extends ModalComponent
{
public string $host = '';
public string $port = '587';
public string $encryption = 'tls';
public string $username = '';
public string $password = '';
public string $fromEmail = '';
public string $fromName = 'HomeOS';
public string $toEmail = '';
public bool $evDeviceOffline = true;
public bool $evLowBattery = true;
public bool $evAutomation = true;
public bool $hasStoredPassword = false;
public ?string $flash = null;
public bool $flashOk = true;
public static function modalMaxWidth(): string
{
return '3xl';
}
public function mount(): void
{
$cfg = Addon::where('key', 'smtp')->first()?->config ?? [];
$this->host = $cfg['host'] ?? '';
$this->port = (string) ($cfg['port'] ?? '587');
$this->encryption = $cfg['encryption'] ?? 'tls';
$this->username = $cfg['username'] ?? '';
$this->fromEmail = $cfg['from_email'] ?? '';
$this->fromName = $cfg['from_name'] ?? 'HomeOS';
$this->toEmail = $cfg['to_email'] ?? '';
$this->hasStoredPassword = filled($cfg['password'] ?? null);
$this->evDeviceOffline = (bool) data_get($cfg, 'events.device_offline', true);
$this->evLowBattery = (bool) data_get($cfg, 'events.low_battery', true);
$this->evAutomation = (bool) data_get($cfg, 'events.automation', true);
}
protected function rules(): array
{
return [
'host' => 'required|string|max:190',
'port' => 'required|integer|min:1|max:65535',
// '' (None) is a valid choice — 'required' would reject it, so validate membership only.
'encryption' => 'in:,tls,ssl',
'username' => 'nullable|string|max:190',
'password' => $this->hasStoredPassword ? 'nullable|string|max:190' : 'nullable|string|max:190',
'fromEmail' => 'required|email|max:190',
'fromName' => 'nullable|string|max:100',
'toEmail' => 'required|email|max:190',
];
}
private function persist(): void
{
$service = app(AddonService::class);
$addon = $service->install('smtp');
$existing = $addon->config ?? [];
$config = [
'host' => $this->host,
'port' => (int) $this->port,
'encryption' => $this->encryption,
'username' => $this->username,
'password' => $this->password !== '' ? $this->password : ($existing['password'] ?? ''),
'from_email' => $this->fromEmail,
'from_name' => $this->fromName,
'to_email' => $this->toEmail,
'events' => [
'device_offline' => $this->evDeviceOffline,
'low_battery' => $this->evLowBattery,
'automation' => $this->evAutomation,
],
];
$addon->forceFill(['config' => $config, 'status' => 'connected', 'connected_at' => now()])->save();
}
public function sendTest(): void
{
$this->validate();
$this->persist();
try {
$ok = app(NotificationService::class)->send(__('addons.smtp.test_subject'), __('addons.smtp.test_body'));
} catch (\Throwable $e) {
report($e);
$ok = false;
}
$this->flash = $ok ? __('addons.smtp.test_ok') : __('addons.smtp.test_fail');
$this->flashOk = $ok;
}
public function save()
{
$this->validate();
$this->persist();
$this->closeModal();
return redirect()->route('addons.index');
}
public function render()
{
return view('livewire.modals.smtp-setup');
}
}