2023-01-24 09:25:05 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
|
2023-02-01 16:51:05 -08:00
|
|
|
use GuzzleHttp\Client;
|
2023-01-24 09:25:05 -08:00
|
|
|
use Livewire\Component;
|
|
|
|
use App\Models\Setting;
|
|
|
|
|
|
|
|
class SlackSettingsForm extends Component
|
|
|
|
{
|
2023-01-25 16:22:34 -08:00
|
|
|
public $slack_endpoint;
|
|
|
|
public $slack_channel;
|
|
|
|
public $slack_botname;
|
2023-02-15 15:46:05 -08:00
|
|
|
public $isDisabled ='disabled' ;
|
2023-01-25 16:22:34 -08:00
|
|
|
|
2023-01-24 09:25:05 -08:00
|
|
|
public Setting $setting;
|
|
|
|
|
2023-02-06 09:40:57 -08:00
|
|
|
protected $rules = [
|
|
|
|
'slack_endpoint' => 'url|required_with:slack_channel|starts_with:https://hooks.slack.com/|nullable',
|
|
|
|
'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable',
|
|
|
|
'slack_botname' => 'string|nullable',
|
|
|
|
];
|
|
|
|
|
2023-01-25 16:22:34 -08:00
|
|
|
public function mount(){
|
|
|
|
|
2023-01-25 16:38:08 -08:00
|
|
|
$this->setting = Setting::getSettings();
|
2023-01-25 16:22:34 -08:00
|
|
|
$this->slack_endpoint = $this->setting->slack_endpoint;
|
|
|
|
$this->slack_channel = $this->setting->slack_channel;
|
|
|
|
$this->slack_botname = $this->setting->slack_botname;
|
|
|
|
|
|
|
|
}
|
2023-02-06 09:40:57 -08:00
|
|
|
public function updated($field){
|
|
|
|
|
|
|
|
$this->validateOnly($field ,$this->rules);
|
|
|
|
}
|
2023-01-24 09:25:05 -08:00
|
|
|
|
|
|
|
public function render()
|
|
|
|
{
|
2023-02-15 15:46:05 -08:00
|
|
|
if(empty($this->slack_channel || $this->slack_endpoint)){
|
|
|
|
$this->isDisabled= 'disabled';
|
|
|
|
}
|
2023-02-16 08:43:12 -08:00
|
|
|
if(empty($this->slack_endpoint && $this->slack_channel)){
|
|
|
|
$this->isDisabled= '';
|
|
|
|
}
|
2023-01-24 09:25:05 -08:00
|
|
|
return view('livewire.slack-settings-form');
|
|
|
|
}
|
|
|
|
|
2023-02-01 16:51:05 -08:00
|
|
|
public function testSlack(){
|
|
|
|
|
|
|
|
$slack = new Client([
|
|
|
|
'base_url' => e($this->slack_endpoint),
|
|
|
|
'defaults' => [
|
|
|
|
'exceptions' => false,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$payload = json_encode(
|
|
|
|
[
|
|
|
|
'channel' => e($this->slack_channel),
|
|
|
|
'text' => trans('general.slack_test_msg'),
|
|
|
|
'username' => e($this->slack_botname),
|
|
|
|
'icon_emoji' => ':heart:',
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$slack->post($this->slack_endpoint, ['body' => $payload]);
|
2023-02-15 15:46:05 -08:00
|
|
|
$this->isDisabled='';
|
2023-02-01 16:51:05 -08:00
|
|
|
return session()->flash('success' , 'Your Slack Integration works!');
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
2023-02-15 15:46:05 -08:00
|
|
|
$this->isDisabled= 'disabled';
|
2023-02-15 14:06:52 -08:00
|
|
|
return session()->flash('error' , trans('admin/settings/message.slack.error', ['error_message' => $e->getMessage()]));
|
2023-02-01 16:51:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//}
|
2023-02-15 14:06:52 -08:00
|
|
|
return session()->flash('message' , trans('admin/settings/message.slack.error_misc'));
|
2023-02-01 16:51:05 -08:00
|
|
|
|
2023-02-01 12:08:13 -08:00
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-01-25 16:22:34 -08:00
|
|
|
public function submit()
|
|
|
|
{
|
2023-02-06 09:40:57 -08:00
|
|
|
$this->validate($this->rules);
|
2023-01-25 16:22:34 -08:00
|
|
|
|
|
|
|
$this->setting->slack_endpoint = $this->slack_endpoint;
|
|
|
|
$this->setting->slack_channel = $this->slack_channel;
|
|
|
|
$this->setting->slack_botname = $this->slack_botname;
|
|
|
|
|
2023-01-31 11:32:05 -08:00
|
|
|
$this->setting->save();
|
|
|
|
|
2023-02-01 16:51:05 -08:00
|
|
|
session()->flash('save',trans('admin/settings/message.update.success'));
|
2023-01-31 11:32:05 -08:00
|
|
|
|
2023-01-30 16:10:02 -08:00
|
|
|
|
2023-01-25 16:22:34 -08:00
|
|
|
}
|
2023-01-24 09:25:05 -08:00
|
|
|
}
|