diff --git a/app/Http/Livewire/SlackSettingsForm.php b/app/Http/Livewire/SlackSettingsForm.php index cd34b450a..dfbc3b17c 100644 --- a/app/Http/Livewire/SlackSettingsForm.php +++ b/app/Http/Livewire/SlackSettingsForm.php @@ -8,47 +8,86 @@ use App\Models\Setting; class SlackSettingsForm extends Component { - public $slack_endpoint; - public $slack_channel; - public $slack_botname; + public $webhook_endpoint; + public $webhook_channel; + public $webhook_botname; public $isDisabled ='disabled' ; + public $webhook_name; + public $webhook_link; + public $webhook_placeholder; + public $webhook_icon; + public $webhook_selected; public Setting $setting; 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', + 'webhook_endpoint' => 'url|required_with:webhook_channel|starts_with:https://hooks.slack.com/services|nullable', + 'webhook_channel' => 'required_with:webhook_endpoint|starts_with:#|nullable', + 'webhook_botname' => 'string|nullable', + ]; + static $webhook_text= [ + "Slack" => array( + "name" => "Slack", + "icon" => 'fab fa-slack', + "placeholder" => "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX", + "link" => 'https://api.slack.com/messaging/webhooks', + ), +// "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', +// ), + "General"=> array( + "name" => "General", + "icon" => "fab fa-hashtag", + "placeholder" => "", + "link" => "", + ), ]; public function mount(){ $this->setting = Setting::getSettings(); - $this->slack_endpoint = $this->setting->slack_endpoint; - $this->slack_channel = $this->setting->slack_channel; - $this->slack_botname = $this->setting->slack_botname; + $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"]; + $this->webhook_endpoint = $this->setting->webhook_endpoint; + $this->webhook_channel = $this->setting->webhook_channel; + $this->webhook_botname = $this->setting->webhook_botname; + $this->webhook_options = $this->setting->webhook_selected; + } public function updated($field){ + if($this->webhook_selected != 'General') { + $this->validateOnly($field, $this->rules); + } + } + public function updatedWebhookSelected(){ + $this->webhook_name = self::$webhook_text[$this->webhook_selected]['name']; + $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"]; - $this->validateOnly($field ,$this->rules); } public function render() { - if(empty($this->slack_channel || $this->slack_endpoint)){ + if(empty($this->webhook_channel || $this->webhook_endpoint)){ $this->isDisabled= 'disabled'; } - if(empty($this->slack_endpoint && $this->slack_channel)){ + if(empty($this->webhook_endpoint && $this->webhook_channel)){ $this->isDisabled= ''; } return view('livewire.slack-settings-form'); } - public function testSlack(){ + public function testWebhook(){ - $slack = new Client([ - 'base_url' => e($this->slack_endpoint), + $webhook = new Client([ + 'base_url' => e($this->webhook_endpoint), 'defaults' => [ 'exceptions' => false, ], @@ -56,35 +95,39 @@ class SlackSettingsForm extends Component $payload = json_encode( [ - 'channel' => e($this->slack_channel), - 'text' => trans('general.slack_test_msg'), - 'username' => e($this->slack_botname), + 'channel' => e($this->webhook_channel), + 'text' => trans('general.webhook_test_msg'), + 'username' => e($this->webhook_botname), 'icon_emoji' => ':heart:', ]); try { - $slack->post($this->slack_endpoint, ['body' => $payload]); + $webhook->post($this->webhook_endpoint, ['body' => $payload]); $this->isDisabled=''; - return session()->flash('success' , 'Your Slack Integration works!'); + return session()->flash('success' , 'Your '.$this->webhook_name.' Integration works!'); } catch (\Exception $e) { $this->isDisabled= 'disabled'; - return session()->flash('error' , trans('admin/settings/message.slack.error', ['error_message' => $e->getMessage()])); + return session()->flash('error' , trans('admin/settings/message.webhook.error', ['error_message' => $e->getMessage(), 'app' => $this->webhook_name])); } //} - return session()->flash('message' , trans('admin/settings/message.slack.error_misc')); + return session()->flash('message' , trans('admin/settings/message.webhook.error_misc')); } public function submit() { - $this->validate($this->rules); + if($this->webhook_selected != 'General') { + $this->validate($this->rules); + } + + $this->setting->webhook_selected = $this->webhook_selected; + $this->setting->webhook_endpoint = $this->webhook_endpoint; + $this->setting->webhook_channel = $this->webhook_channel; + $this->setting->webhook_botname = $this->webhook_botname; - $this->setting->slack_endpoint = $this->slack_endpoint; - $this->setting->slack_channel = $this->slack_channel; - $this->setting->slack_botname = $this->slack_botname; $this->setting->save(); diff --git a/composer.json b/composer.json index abcf675b1..2f1007fa7 100644 --- a/composer.json +++ b/composer.json @@ -127,6 +127,9 @@ "sort-packages": true, "optimize-autoloader": true, "discard-changes": true, - "process-timeout": 3000 + "process-timeout": 3000, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/database/migrations/2023_02_28_173527_adds_webhook_option_to_settings_table.php b/database/migrations/2023_02_28_173527_adds_webhook_option_to_settings_table.php new file mode 100644 index 000000000..9e7a27872 --- /dev/null +++ b/database/migrations/2023_02_28_173527_adds_webhook_option_to_settings_table.php @@ -0,0 +1,39 @@ +string('webhook_selected')->after('slack_botname')->default('Slack')->nullable(); + $table->renameColumn('slack_botname', 'webhook_botname'); + $table->renameColumn('slack_endpoint', 'webhook_endpoint'); + $table->renameColumn('slack_channel', 'webhook_channel'); + + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('settings', function (Blueprint $table) { + $table->dropColumn('webhook_selected'); + $table->renameColumn('webhook_botname', 'slack_botname'); + $table->renameColumn('webhook_endpoint', 'slack_endpoint'); + $table->renameColumn('webhook_channel', 'slack_channel'); + }); + } +} diff --git a/resources/lang/en/admin/settings/general.php b/resources/lang/en/admin/settings/general.php index 6416b4ed4..4ccb7419b 100644 --- a/resources/lang/en/admin/settings/general.php +++ b/resources/lang/en/admin/settings/general.php @@ -198,16 +198,18 @@ return [ 'show_images_in_email' => 'Show images in emails', 'show_images_in_email_help' => 'Uncheck this box if your Snipe-IT installation is behind a VPN or closed network and users outside the network will not be able to load images served from this installation in their emails.', 'site_name' => 'Site Name', - 'slack' => 'Slack', - 'slack_title' => 'Update Slack Settings', - 'slack_help' => 'Slack settings', - 'slack_botname' => 'Slack Botname', - 'slack_channel' => 'Slack Channel', - 'slack_endpoint' => 'Slack Endpoint', - 'slack_integration' => 'Slack Settings', - 'slack_integration_help' => 'Slack integration is optional, however the endpoint and channel are required if you wish to use it. To configure Slack integration, you must first create an incoming webhook on your Slack account. Click on the Test Slack Integration button to confirm your settings are correct before saving. ', - 'slack_integration_help_button' => 'Once you have saved your Slack information, a test button will appear.', - 'slack_test_help' => 'Test whether your Slack integration is configured correctly. YOU MUST SAVE YOUR UPDATED SLACK SETTINGS FIRST.', + 'integrations' => 'Integrations', + 'webhook' => ':app', + 'webhook_title' => 'Update Webhook Settings', + 'webhook_help' => 'Integration settings', + 'webhook_botname' => ':app Botname', + 'webhook_channel' => ':app Channel', + 'webhook_endpoint' => ':app Endpoint', + 'webhook_integration' => ':app Settings', + 'webhook_test' =>'Test :app integration', + 'webhook_integration_help' => ':app integration is optional, however the endpoint and channel are required if you wish to use it. To configure :app integration, you must first create an incoming webhook on your :app account. Click on the Test :app Integration button to confirm your settings are correct before saving. ', + 'webhook_integration_help_button' => 'Once you have saved your :app information, a test button will appear.', + 'webhook_test_help' => 'Test whether your :app integration is configured correctly. YOU MUST SAVE YOUR UPDATED :app SETTINGS FIRST.', 'snipe_version' => 'Snipe-IT version', 'support_footer' => 'Support Footer Links ', 'support_footer_help' => 'Specify who sees the links to the Snipe-IT Support info and Users Manual', diff --git a/resources/lang/en/admin/settings/message.php b/resources/lang/en/admin/settings/message.php index b0648d1c1..48b42d120 100644 --- a/resources/lang/en/admin/settings/message.php +++ b/resources/lang/en/admin/settings/message.php @@ -33,12 +33,12 @@ return [ 'testing_authentication' => 'Testing LDAP Authentication...', 'authentication_success' => 'User authenticated against LDAP successfully!' ], - 'slack' => [ - 'sending' => 'Sending Slack test message...', + 'webhook' => [ + 'sending' => 'Sending :app test message...', 'success_pt1' => 'Success! Check the ', 'success_pt2' => ' channel for your test message, and be sure to click SAVE below to store your settings.', '500' => '500 Server Error.', - 'error' => 'Something went wrong. Slack responded with: :error_message', + 'error' => 'Something went wrong. :app responded with: :error_message', 'error_misc' => 'Something went wrong. :( ', ] ]; diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index 97b82d95a..d13317c3e 100644 --- a/resources/lang/en/general.php +++ b/resources/lang/en/general.php @@ -250,8 +250,8 @@ return [ 'signature' => 'Signature', 'signed_off_by' => 'Signed Off By', 'skin' => 'Skin', - 'slack_msg_note' => 'A slack message will be sent', - 'slack_test_msg' => 'Oh hai! Looks like your Slack integration with Snipe-IT is working!', + 'webhook_msg_note' => 'A :app message will be sent', + 'webhook_test_msg' => 'Oh hai! Looks like your :app integration with Snipe-IT is working!', 'some_features_disabled' => 'DEMO MODE: Some features are disabled for this installation.', 'site_name' => 'Site Name', 'state' => 'State', diff --git a/resources/views/livewire/slack-settings-form.blade.php b/resources/views/livewire/slack-settings-form.blade.php index 6d48220ca..cdfd03c8c 100644 --- a/resources/views/livewire/slack-settings-form.blade.php +++ b/resources/views/livewire/slack-settings-form.blade.php @@ -1,99 +1,168 @@ + {{-- Page title --}} + @section('title') + {{ trans('admin/settings/general.webhook_title') }} + @parent + @stop + + @section('header_right') + {{ trans('general.back') }} + @stop + -
- {!! trans('admin/settings/general.slack_integration_help',array('slack_link' => 'https://my.slack.com/services/new/incoming-webhook')) !!} -
-