coolify/app/Livewire/SettingsEmail.php

118 lines
3.7 KiB
PHP
Raw Normal View History

2023-06-06 13:30:33 +00:00
<?php
namespace App\Livewire;
2023-06-06 13:30:33 +00:00
use App\Models\InstanceSettings;
use Livewire\Attributes\Validate;
2023-06-06 13:30:33 +00:00
use Livewire\Component;
class SettingsEmail extends Component
2023-06-06 13:30:33 +00:00
{
public InstanceSettings $settings;
2024-06-10 20:43:34 +00:00
#[Validate(['boolean'])]
2024-11-03 20:45:17 +00:00
public bool $smtpEnabled = false;
#[Validate(['nullable', 'string'])]
2024-11-03 20:45:17 +00:00
public ?string $smtpHost = null;
#[Validate(['nullable', 'numeric', 'min:1', 'max:65535'])]
2024-11-03 20:45:17 +00:00
public ?int $smtpPort = null;
#[Validate(['nullable', 'string'])]
2024-11-03 20:45:17 +00:00
public ?string $smtpEncryption = null;
#[Validate(['nullable', 'string'])]
2024-11-03 20:45:17 +00:00
public ?string $smtpUsername = null;
#[Validate(['nullable'])]
2024-11-03 20:45:17 +00:00
public ?string $smtpPassword = null;
#[Validate(['nullable', 'numeric'])]
2024-11-03 20:45:17 +00:00
public ?int $smtpTimeout = null;
#[Validate(['nullable', 'email'])]
2024-11-03 20:45:17 +00:00
public ?string $smtpFromAddress = null;
#[Validate(['nullable', 'string'])]
2024-11-03 20:45:17 +00:00
public ?string $smtpFromName = null;
#[Validate(['boolean'])]
2024-11-03 20:45:17 +00:00
public bool $resendEnabled = false;
#[Validate(['nullable', 'string'])]
2024-11-03 20:45:17 +00:00
public ?string $resendApiKey = null;
2024-06-10 20:43:34 +00:00
2023-06-20 19:18:14 +00:00
public function mount()
{
2024-11-03 20:45:17 +00:00
if (isInstanceAdmin() === false) {
return redirect()->route('dashboard');
}
2024-11-03 20:45:17 +00:00
$this->settings = instanceSettings();
$this->syncData();
2023-06-20 19:18:14 +00:00
}
2024-11-03 20:45:17 +00:00
public function syncData(bool $toModel = false)
2024-06-10 20:43:34 +00:00
{
2024-11-03 20:45:17 +00:00
if ($toModel) {
$this->validate();
$this->settings->smtp_enabled = $this->smtpEnabled;
$this->settings->smtp_host = $this->smtpHost;
$this->settings->smtp_port = $this->smtpPort;
$this->settings->smtp_encryption = $this->smtpEncryption;
$this->settings->smtp_username = $this->smtpUsername;
$this->settings->smtp_password = $this->smtpPassword;
$this->settings->smtp_timeout = $this->smtpTimeout;
$this->settings->smtp_from_address = $this->smtpFromAddress;
$this->settings->smtp_from_name = $this->smtpFromName;
2024-06-10 20:43:34 +00:00
2024-11-03 20:45:17 +00:00
$this->settings->resend_enabled = $this->resendEnabled;
$this->settings->resend_api_key = $this->resendApiKey;
$this->settings->save();
2024-11-03 20:45:17 +00:00
} else {
$this->smtpEnabled = $this->settings->smtp_enabled;
$this->smtpHost = $this->settings->smtp_host;
$this->smtpPort = $this->settings->smtp_port;
$this->smtpEncryption = $this->settings->smtp_encryption;
$this->smtpUsername = $this->settings->smtp_username;
$this->smtpPassword = $this->settings->smtp_password;
$this->smtpTimeout = $this->settings->smtp_timeout;
$this->smtpFromAddress = $this->settings->smtp_from_address;
$this->smtpFromName = $this->settings->smtp_from_name;
2024-06-10 20:43:34 +00:00
2024-11-03 20:45:17 +00:00
$this->resendEnabled = $this->settings->resend_enabled;
$this->resendApiKey = $this->settings->resend_api_key;
2023-09-08 14:56:14 +00:00
}
}
2024-06-10 20:43:34 +00:00
2024-11-03 20:45:17 +00:00
public function submit()
2023-06-13 08:51:58 +00:00
{
try {
2024-11-03 20:45:17 +00:00
$this->resetErrorBag();
$this->syncData(true);
$this->dispatch('success', 'Settings saved.');
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-06-13 08:51:58 +00:00
}
}
2024-11-03 20:45:17 +00:00
public function instantSave(string $type)
2023-06-06 13:30:33 +00:00
{
try {
2024-11-03 20:45:17 +00:00
if ($type === 'SMTP') {
$this->resendEnabled = false;
} else {
$this->smtpEnabled = false;
}
$this->syncData(true);
if ($this->smtpEnabled || $this->resendEnabled) {
$this->dispatch('success', "{$type} enabled.");
} else {
$this->dispatch('success', "{$type} disabled.");
}
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-06-20 19:18:14 +00:00
}
2023-06-06 13:30:33 +00:00
}
}