lasthourcloud/app/Http/Livewire/Notifications/EmailSettings.php

88 lines
4.0 KiB
PHP
Raw Normal View History

2023-05-25 16:27:52 +00:00
<?php
namespace App\Http\Livewire\Notifications;
2023-06-07 15:24:37 +00:00
use App\Models\InstanceSettings;
2023-05-25 16:27:52 +00:00
use App\Models\Team;
2023-06-14 09:54:00 +00:00
use App\Notifications\Notifications\TestNotification;
2023-06-07 15:24:37 +00:00
use Illuminate\Support\Facades\Notification;
2023-05-25 16:27:52 +00:00
use Livewire\Component;
class EmailSettings extends Component
{
2023-06-02 10:34:45 +00:00
public Team $model;
2023-05-25 16:27:52 +00:00
protected $rules = [
2023-06-01 11:24:20 +00:00
'model.extra_attributes.smtp_active' => 'nullable|boolean',
2023-06-06 15:50:13 +00:00
'model.extra_attributes.smtp_from_address' => 'required|email',
'model.extra_attributes.smtp_from_name' => 'required',
2023-06-12 10:52:58 +00:00
'model.extra_attributes.smtp_recipients' => 'nullable',
2023-06-01 11:24:20 +00:00
'model.extra_attributes.smtp_host' => 'required',
'model.extra_attributes.smtp_port' => 'required',
'model.extra_attributes.smtp_encryption' => 'nullable',
'model.extra_attributes.smtp_username' => 'nullable',
'model.extra_attributes.smtp_password' => 'nullable',
'model.extra_attributes.smtp_timeout' => 'nullable',
2023-06-06 15:50:13 +00:00
'model.extra_attributes.smtp_test_recipients' => 'nullable',
2023-06-19 13:24:04 +00:00
'model.extra_attributes.notifications_email_test' => 'nullable|boolean',
'model.extra_attributes.notifications_email_deployments' => 'nullable|boolean',
2023-05-25 16:27:52 +00:00
];
protected $validationAttributes = [
2023-06-09 13:55:21 +00:00
'model.extra_attributes.smtp_from_address' => 'From Address',
'model.extra_attributes.smtp_from_name' => 'From Name',
'model.extra_attributes.smtp_recipients' => 'Recipients',
'model.extra_attributes.smtp_host' => 'Host',
'model.extra_attributes.smtp_port' => 'Port',
'model.extra_attributes.smtp_encryption' => 'Encryption',
'model.extra_attributes.smtp_username' => 'Username',
'model.extra_attributes.smtp_password' => 'Password',
'model.extra_attributes.smtp_test_recipients' => 'Test Recipients',
2023-05-25 16:27:52 +00:00
];
2023-06-07 15:24:37 +00:00
public function copySMTP()
{
$settings = InstanceSettings::get();
$this->model->extra_attributes->smtp_active = true;
$this->model->extra_attributes->smtp_from_address = $settings->extra_attributes->smtp_from_address;
$this->model->extra_attributes->smtp_from_name = $settings->extra_attributes->smtp_from_name;
$this->model->extra_attributes->smtp_recipients = $settings->extra_attributes->smtp_recipients;
$this->model->extra_attributes->smtp_host = $settings->extra_attributes->smtp_host;
$this->model->extra_attributes->smtp_port = $settings->extra_attributes->smtp_port;
$this->model->extra_attributes->smtp_encryption = $settings->extra_attributes->smtp_encryption;
$this->model->extra_attributes->smtp_username = $settings->extra_attributes->smtp_username;
$this->model->extra_attributes->smtp_password = $settings->extra_attributes->smtp_password;
$this->model->extra_attributes->smtp_timeout = $settings->extra_attributes->smtp_timeout;
$this->model->extra_attributes->smtp_test_recipients = $settings->extra_attributes->smtp_test_recipients;
$this->saveModel();
}
2023-05-25 16:27:52 +00:00
public function submit()
{
$this->resetErrorBag();
$this->validate();
2023-06-06 15:50:13 +00:00
$this->model->extra_attributes->smtp_recipients = str_replace(' ', '', $this->model->extra_attributes->smtp_recipients);
$this->model->extra_attributes->smtp_test_recipients = str_replace(' ', '', $this->model->extra_attributes->smtp_test_recipients);
2023-06-01 10:15:33 +00:00
$this->saveModel();
}
2023-06-19 13:24:04 +00:00
public function saveModel()
2023-06-01 10:15:33 +00:00
{
2023-05-25 16:27:52 +00:00
$this->model->save();
2023-06-01 10:15:33 +00:00
if (is_a($this->model, Team::class)) {
2023-05-25 16:27:52 +00:00
session(['currentTeam' => $this->model]);
}
2023-06-19 12:31:42 +00:00
$this->emit('success', 'Settings saved.');
2023-05-25 16:27:52 +00:00
}
2023-06-07 15:24:37 +00:00
public function sendTestNotification()
{
Notification::send($this->model, new TestNotification);
2023-06-19 13:24:04 +00:00
$this->emit('success', 'Test notification sent.');
2023-06-19 12:31:42 +00:00
}
2023-06-01 10:15:33 +00:00
public function instantSave()
2023-05-25 16:27:52 +00:00
{
2023-06-01 11:24:20 +00:00
try {
$this->submit();
} catch (\Exception $e) {
$this->model->extra_attributes->smtp_active = false;
$this->validate();
}
2023-05-25 16:27:52 +00:00
}
}