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-03-01 12:30:32 -08:00
|
|
|
public $webhook_endpoint;
|
|
|
|
public $webhook_channel;
|
|
|
|
public $webhook_botname;
|
2023-02-15 15:46:05 -08:00
|
|
|
public $isDisabled ='disabled' ;
|
2023-03-07 15:48:21 -08:00
|
|
|
public $webhook_name;
|
2023-02-27 09:19:59 -08:00
|
|
|
public $webhook_link;
|
2023-03-01 17:10:10 -08:00
|
|
|
public $webhook_placeholder;
|
|
|
|
public $webhook_icon;
|
2023-02-27 09:19:59 -08:00
|
|
|
public $webhook_selected;
|
2023-03-01 17:10:10 -08:00
|
|
|
|
|
|
|
public Setting $setting;
|
|
|
|
|
|
|
|
protected $rules = [
|
2023-03-13 10:54:18 -07:00
|
|
|
'webhook_endpoint' => 'url|required_with:webhook_channel|starts_with:https://hooks.slack.com/services|nullable',
|
2023-03-07 15:48:21 -08:00
|
|
|
'webhook_channel' => 'required_with:webhook_endpoint|starts_with:#|nullable',
|
2023-03-01 17:10:10 -08:00
|
|
|
'webhook_botname' => 'string|nullable',
|
|
|
|
];
|
|
|
|
static $webhook_text= [
|
|
|
|
"Slack" => array(
|
2023-03-07 15:48:21 -08:00
|
|
|
"name" => "Slack",
|
2023-03-01 12:30:32 -08:00
|
|
|
"icon" => 'fab fa-slack',
|
|
|
|
"placeholder" => "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX",
|
2023-03-07 15:48:21 -08:00
|
|
|
"link" => 'https://api.slack.com/messaging/webhooks',
|
2023-03-01 12:30:32 -08:00
|
|
|
),
|
2023-03-08 10:35:45 -08:00
|
|
|
// "Discord" => array(
|
|
|
|
// "name" => "Discord",
|
|
|
|
// "icon" => 'fab fa-discord',
|
|
|
|
// "placeholder" => "https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXXXX",
|
|
|
|
// "link" => 'https://support.discord.com/hc/en-us/articles/360045093012-Server-Integrations-Page',
|
|
|
|
// ),
|
2023-03-07 15:48:21 -08:00
|
|
|
"General"=> array(
|
|
|
|
"name" => "General",
|
|
|
|
"icon" => "fab fa-hashtag",
|
|
|
|
"placeholder" => "",
|
|
|
|
"link" => "",
|
2023-03-01 12:30:32 -08:00
|
|
|
),
|
2023-02-06 09:40:57 -08:00
|
|
|
];
|
|
|
|
|
2023-01-25 16:22:34 -08:00
|
|
|
public function mount(){
|
|
|
|
|
2023-01-25 16:38:08 -08:00
|
|
|
$this->setting = Setting::getSettings();
|
2023-03-07 15:48:21 -08:00
|
|
|
$this->webhook_selected = $this->setting->webhook_selected;
|
|
|
|
$this->webhook_placeholder = self::$webhook_text[$this->setting->webhook_selected]["placeholder"];
|
|
|
|
$this->webhook_name = self::$webhook_text[$this->setting->webhook_selected]["name"];
|
|
|
|
$this->webhook_icon = self::$webhook_text[$this->setting->webhook_selected]["icon"];
|
2023-03-01 12:30:32 -08:00
|
|
|
$this->webhook_endpoint = $this->setting->webhook_endpoint;
|
|
|
|
$this->webhook_channel = $this->setting->webhook_channel;
|
|
|
|
$this->webhook_botname = $this->setting->webhook_botname;
|
2023-03-01 17:10:10 -08:00
|
|
|
$this->webhook_options = $this->setting->webhook_selected;
|
2023-03-01 12:30:32 -08:00
|
|
|
|
2023-01-25 16:22:34 -08:00
|
|
|
|
|
|
|
}
|
2023-02-06 09:40:57 -08:00
|
|
|
public function updated($field){
|
2023-03-07 15:48:21 -08:00
|
|
|
if($this->webhook_selected != 'General') {
|
|
|
|
$this->validateOnly($field, $this->rules);
|
|
|
|
}
|
2023-02-06 09:40:57 -08:00
|
|
|
}
|
2023-03-01 17:10:10 -08:00
|
|
|
public function updatedWebhookSelected(){
|
2023-03-07 15:48:21 -08:00
|
|
|
$this->webhook_name = self::$webhook_text[$this->webhook_selected]['name'];
|
2023-03-01 17:10:10 -08:00
|
|
|
$this->webhook_icon = self::$webhook_text[$this->webhook_selected]["icon"]; ;
|
|
|
|
$this->webhook_placeholder = self::$webhook_text[$this->webhook_selected]["placeholder"];
|
|
|
|
$this->webhook_link = self::$webhook_text[$this->webhook_selected]["link"];
|
|
|
|
|
|
|
|
}
|
2023-01-24 09:25:05 -08:00
|
|
|
|
|
|
|
public function render()
|
|
|
|
{
|
2023-03-01 12:30:32 -08:00
|
|
|
if(empty($this->webhook_channel || $this->webhook_endpoint)){
|
2023-02-15 15:46:05 -08:00
|
|
|
$this->isDisabled= 'disabled';
|
|
|
|
}
|
2023-03-01 12:30:32 -08:00
|
|
|
if(empty($this->webhook_endpoint && $this->webhook_channel)){
|
2023-02-16 08:43:12 -08:00
|
|
|
$this->isDisabled= '';
|
|
|
|
}
|
2023-01-24 09:25:05 -08:00
|
|
|
return view('livewire.slack-settings-form');
|
|
|
|
}
|
|
|
|
|
2023-03-01 12:30:32 -08:00
|
|
|
public function testWebhook(){
|
2023-02-01 16:51:05 -08:00
|
|
|
|
2023-03-07 15:48:21 -08:00
|
|
|
$webhook = new Client([
|
2023-03-01 12:30:32 -08:00
|
|
|
'base_url' => e($this->webhook_endpoint),
|
2023-02-01 16:51:05 -08:00
|
|
|
'defaults' => [
|
|
|
|
'exceptions' => false,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$payload = json_encode(
|
|
|
|
[
|
2023-03-01 12:30:32 -08:00
|
|
|
'channel' => e($this->webhook_channel),
|
2023-03-07 15:48:21 -08:00
|
|
|
'text' => trans('general.webhook_test_msg'),
|
2023-03-01 12:30:32 -08:00
|
|
|
'username' => e($this->webhook_botname),
|
2023-02-01 16:51:05 -08:00
|
|
|
'icon_emoji' => ':heart:',
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
2023-03-07 15:48:21 -08:00
|
|
|
$webhook->post($this->webhook_endpoint, ['body' => $payload]);
|
2023-02-15 15:46:05 -08:00
|
|
|
$this->isDisabled='';
|
2023-03-07 15:57:58 -08:00
|
|
|
return session()->flash('success' , 'Your '.$this->webhook_name.' Integration works!');
|
2023-02-01 16:51:05 -08:00
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
2023-02-15 15:46:05 -08:00
|
|
|
$this->isDisabled= 'disabled';
|
2023-03-08 10:35:45 -08:00
|
|
|
return session()->flash('error' , trans('admin/settings/message.webhook.error', ['error_message' => $e->getMessage(), 'app' => $this->webhook_name]));
|
2023-02-01 16:51:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//}
|
2023-03-07 15:48:21 -08:00
|
|
|
return session()->flash('message' , trans('admin/settings/message.webhook.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-03-07 15:48:21 -08:00
|
|
|
if($this->webhook_selected != 'General') {
|
|
|
|
$this->validate($this->rules);
|
|
|
|
}
|
2023-01-25 16:22:34 -08:00
|
|
|
|
2023-03-07 15:48:21 -08:00
|
|
|
$this->setting->webhook_selected = $this->webhook_selected;
|
2023-03-01 12:30:32 -08:00
|
|
|
$this->setting->webhook_endpoint = $this->webhook_endpoint;
|
|
|
|
$this->setting->webhook_channel = $this->webhook_channel;
|
|
|
|
$this->setting->webhook_botname = $this->webhook_botname;
|
2023-01-25 16:22:34 -08:00
|
|
|
|
2023-03-07 15:48:21 -08:00
|
|
|
|
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
|
|
|
}
|