99use App \Models \Setting ;
1010use App \Helpers \Helper ;
1111use Osama \LaravelTeamsNotification \TeamsNotification ;
12+
1213class SlackSettingsForm extends Component
1314{
1415 public $ webhook_endpoint ;
1516 public $ webhook_channel ;
1617 public $ webhook_botname ;
17- public $ isDisabled ='disabled ' ;
18+ public $ isDisabled = 'disabled ' ;
1819 public $ webhook_name ;
1920 public $ webhook_link ;
2021 public $ webhook_placeholder ;
@@ -37,36 +38,37 @@ class SlackSettingsForm extends Component
3738 ];
3839
3940
40- public function mount () {
41- $ this ->webhook_text = [
41+ public function mount ()
42+ {
43+ $ this ->webhook_text = [
4244 "slack " => array (
4345 "name " => trans ('admin/settings/general.slack ' ) ,
4446 "icon " => 'fab fa-slack ' ,
4547 "placeholder " => "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX " ,
4648 "link " => 'https://api.slack.com/messaging/webhooks ' ,
4749 "test " => "testWebhook "
48- ),
49- "general " => array (
50+ ),
51+ "general " => array (
5052 "name " => trans ('admin/settings/general.general_webhook ' ),
5153 "icon " => "fab fa-hashtag " ,
5254 "placeholder " => trans ('general.url ' ),
5355 "link " => "" ,
5456 "test " => "testWebhook "
55- ),
56- "google " => array (
57+ ),
58+ "google " => array (
5759 "name " => trans ('admin/settings/general.google_workspaces ' ),
5860 "icon " => "fa-brands fa-google " ,
5961 "placeholder " => "https://chat.googleapis.com/v1/spaces/xxxxxxxx/messages?key=xxxxxx " ,
6062 "link " => "https://developers.google.com/chat/how-tos/webhooks#register_the_incoming_webhook " ,
6163 "test " => "googleWebhookTest "
62- ),
63- "microsoft " => array (
64+ ),
65+ "microsoft " => array (
6466 "name " => trans ('admin/settings/general.ms_teams ' ),
6567 "icon " => "fa-brands fa-microsoft " ,
6668 "placeholder " => "https://abcd.webhook.office.com/webhookb2/XXXXXXX " ,
6769 "link " => "https://support.microsoft.com/en-us/office/create-incoming-webhooks-with-workflows-for-microsoft-teams-8ae491c7-0394-4861-ba59-055e33f75498 " ,
6870 "test " => "msTeamTestWebhook "
69- ),
71+ ),
7072 ];
7173
7274 $ this ->setting = Setting::getSettings ();
@@ -87,27 +89,29 @@ public function mount() {
8789 }
8890
8991 if ($ this ->setting ->webhook_endpoint != null && $ this ->setting ->webhook_channel != null ) {
90- $ this ->isDisabled = '' ;
92+ $ this ->isDisabled = '' ;
9193 }
9294 if ($ this ->webhook_selected === 'microsoft ' && $ this ->teams_webhook_deprecated ) {
9395 session ()->flash ('warning ' , trans ('admin/settings/message.webhook.ms_teams_deprecation ' ));
9496 }
9597 }
96- public function updated ($ field ) {
98+ public function updated ($ field )
99+ {
97100
98101 $ this ->validateOnly ($ field , $ this ->rules );
99-
100102 }
101103
102- public function updatedWebhookSelected () {
104+ public function updatedWebhookSelected ()
105+ {
103106 $ this ->webhook_name = $ this ->webhook_text [$ this ->webhook_selected ]['name ' ];
104- $ this ->webhook_icon = $ this ->webhook_text [$ this ->webhook_selected ]["icon " ]; ;
107+ $ this ->webhook_icon = $ this ->webhook_text [$ this ->webhook_selected ]["icon " ];
108+ ;
105109 $ this ->webhook_placeholder = $ this ->webhook_text [$ this ->webhook_selected ]["placeholder " ];
106110 $ this ->webhook_endpoint = null ;
107111 $ this ->webhook_link = $ this ->webhook_text [$ this ->webhook_selected ]["link " ];
108112 $ this ->webhook_test = $ this ->webhook_text [$ this ->webhook_selected ]["test " ];
109113 if ($ this ->webhook_selected != 'slack ' ) {
110- $ this ->isDisabled = '' ;
114+ $ this ->isDisabled = '' ;
111115 $ this ->save_button = trans ('general.save ' );
112116 }
113117 if ($ this ->webhook_selected == 'microsoft ' || $ this ->webhook_selected == 'google ' ) {
@@ -120,26 +124,27 @@ public function updatedwebhookEndpoint()
120124 $ this ->teams_webhook_deprecated = !Str::contains ($ this ->webhook_endpoint , 'workflows ' );
121125 }
122126
123- private function isButtonDisabled () {
124- if (empty ($ this ->webhook_endpoint )) {
125- $ this ->isDisabled = 'disabled ' ;
126- $ this ->save_button = trans ('admin/settings/general.webhook_presave ' );
127- }
128- if (empty ($ this ->webhook_channel )) {
129- $ this ->isDisabled = 'disabled ' ;
130- $ this ->save_button = trans ('admin/settings/general.webhook_presave ' );
131- }
127+ private function isButtonDisabled ()
128+ {
129+ if (empty ($ this ->webhook_endpoint )) {
130+ $ this ->isDisabled = 'disabled ' ;
131+ $ this ->save_button = trans ('admin/settings/general.webhook_presave ' );
132+ }
133+ if (empty ($ this ->webhook_channel )) {
134+ $ this ->isDisabled = 'disabled ' ;
135+ $ this ->save_button = trans ('admin/settings/general.webhook_presave ' );
136+ }
132137 }
133138
134139 public function render ()
135140 {
136141 $ this ->isButtonDisabled ();
137142
138143 return view ('livewire.slack-settings-form ' );
139-
140144 }
141145
142- public function testWebhook () {
146+ public function testWebhook ()
147+ {
143148
144149 $ webhook = new Client ([
145150 'base_url ' => e ($ this ->webhook_endpoint ),
@@ -156,34 +161,33 @@ public function testWebhook() {
156161 'username ' => e ($ this ->webhook_botname ),
157162 'icon_emoji ' => ':heart: ' ,
158163
159- ]);
164+ ]
165+ );
160166
161167 try {
162168 $ test = $ webhook ->post ($ this ->webhook_endpoint , ['body ' => $ payload , ['headers ' => ['Content-Type ' => 'application/json ' ]]]);
163169
164- if (($ test ->getStatusCode () == 302 )|| ($ test ->getStatusCode () == 301 )) {
165- return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error_redirect ' , ['endpoint ' => $ this ->webhook_endpoint ]));
170+ if (($ test ->getStatusCode () == 302 ) || ($ test ->getStatusCode () == 301 )) {
171+ return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error_redirect ' , ['endpoint ' => $ this ->webhook_endpoint ]));
166172 }
167- $ this ->isDisabled = '' ;
173+ $ this ->isDisabled = '' ;
168174 $ this ->save_button = trans ('general.save ' );
169- return session ()->flash ('success ' , trans ('admin/settings/message.webhook.success ' , ['webhook_name ' => $ this ->webhook_name ]));
170-
175+ return session ()->flash ('success ' , trans ('admin/settings/message.webhook.success ' , ['webhook_name ' => $ this ->webhook_name ]));
171176 } catch (\Exception $ e ) {
172-
173- $ this ->isDisabled ='disabled ' ;
177+ $ this ->isDisabled = 'disabled ' ;
174178 $ this ->save_button = trans ('admin/settings/general.webhook_presave ' );
175- return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error ' , ['error_message ' => $ e ->getMessage (), 'app ' => $ this ->webhook_name ]));
179+ return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error ' , ['error_message ' => $ e ->getMessage (), 'app ' => $ this ->webhook_name ]));
176180 }
177181
178- return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error_misc ' ));
179-
182+ return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error_misc ' ));
180183 }
181184
182185
183- public function clearSettings () {
186+ public function clearSettings ()
187+ {
184188
185189 if (Helper::isDemoMode ()) {
186- session ()->flash ('error ' ,trans ('general.feature_disabled ' ));
190+ session ()->flash ('error ' , trans ('general.feature_disabled ' ));
187191 } else {
188192 $ this ->webhook_endpoint = '' ;
189193 $ this ->webhook_channel = '' ;
@@ -201,7 +205,7 @@ public function clearSettings() {
201205 public function submit ()
202206 {
203207 if (Helper::isDemoMode ()) {
204- session ()->flash ('error ' ,trans ('general.feature_disabled ' ));
208+ session ()->flash ('error ' , trans ('general.feature_disabled ' ));
205209 } else {
206210 $ this ->validate ($ this ->rules );
207211
@@ -212,11 +216,11 @@ public function submit()
212216
213217 $ this ->setting ->save ();
214218
215- session ()->flash ('success ' ,trans ('admin/settings/message.update.success ' ));
219+ session ()->flash ('success ' , trans ('admin/settings/message.update.success ' ));
216220 }
217-
218221 }
219- public function googleWebhookTest () {
222+ public function googleWebhookTest ()
223+ {
220224
221225 $ payload = [
222226 "text " => trans ('general.webhook_test_msg ' , ['app ' => $ this ->webhook_name ]),
@@ -225,29 +229,29 @@ public function googleWebhookTest() {
225229 try {
226230 $ response = Http::withHeaders ([
227231 'content-type ' => 'application/json ' ,
228- ])->post ($ this ->webhook_endpoint ,
229- $ payload )->throw ();
232+ ])->post (
233+ $ this ->webhook_endpoint ,
234+ $ payload
235+ )->throw ();
230236
231237
232238 if (($ response ->getStatusCode () == 302 ) || ($ response ->getStatusCode () == 301 )) {
233239 return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error_redirect ' , ['endpoint ' => $ this ->webhook_endpoint ]));
234240 }
235241
236- $ this ->isDisabled = '' ;
242+ $ this ->isDisabled = '' ;
237243 $ this ->save_button = trans ('general.save ' );
238- return session ()->flash ('success ' , trans ('admin/settings/message.webhook.success ' , ['webhook_name ' => $ this ->webhook_name ]));
239-
244+ return session ()->flash ('success ' , trans ('admin/settings/message.webhook.success ' , ['webhook_name ' => $ this ->webhook_name ]));
240245 } catch (\Exception $ e ) {
241-
242- $ this ->isDisabled ='disabled ' ;
246+ $ this ->isDisabled = 'disabled ' ;
243247 $ this ->save_button = trans ('admin/settings/general.webhook_presave ' );
244- return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error ' , ['error_message ' => $ e ->getMessage (), 'app ' => $ this ->webhook_name ]));
248+ return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error ' , ['error_message ' => $ e ->getMessage (), 'app ' => $ this ->webhook_name ]));
245249 }
246250 }
247- public function msTeamTestWebhook () {
251+ public function msTeamTestWebhook ()
252+ {
248253
249254 try {
250-
251255 if ($ this ->teams_webhook_deprecated ) {
252256 //will use the deprecated webhook format
253257 $ payload =
@@ -260,33 +264,32 @@ public function msTeamTestWebhook() {
260264 ];
261265 $ response = Http::withHeaders ([
262266 'content-type ' => 'application/json ' ,
263- ])->post ($ this ->webhook_endpoint ,
264- $ payload )->throw ();
267+ ])->post (
268+ $ this ->webhook_endpoint ,
269+ $ payload
270+ )->throw ();
271+ } else {
272+ $ notification = new TeamsNotification ($ this ->webhook_endpoint );
273+ $ message = trans ('general.webhook_test_msg ' , ['app ' => $ this ->webhook_name ]);
274+ $ notification ->success ()->sendMessage ($ message );
275+
276+ $ response = Http::withHeaders ([
277+ 'content-type ' => 'application/json ' ,
278+ ])->post ($ this ->webhook_endpoint );
265279 }
266- else {
267- $ notification = new TeamsNotification ($ this ->webhook_endpoint );
268- $ message = trans ('general.webhook_test_msg ' , ['app ' => $ this ->webhook_name ]);
269- $ notification ->success ()->sendMessage ($ message );
270-
271- $ response = Http::withHeaders ([
272- 'content-type ' => 'application/json ' ,
273- ])->post ($ this ->webhook_endpoint );
274- }
275-
276- if (($ response ->getStatusCode () == 302 )||($ response ->getStatusCode () == 301 )) {
277- return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error_redirect ' , ['endpoint ' => $ this ->webhook_endpoint ]));
278- }
279- $ this ->isDisabled ='' ;
280- $ this ->save_button = trans ('general.save ' );
281- return session ()->flash ('success ' , trans ('admin/settings/message.webhook.success ' , ['webhook_name ' => $ this ->webhook_name ]));
282-
283- } catch (\Exception $ e ) {
284-
285- $ this ->isDisabled ='disabled ' ;
286- $ this ->save_button = trans ('admin/settings/general.webhook_presave ' );
287- return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error ' , ['error_message ' => $ e ->getMessage (), 'app ' => $ this ->webhook_name ]));
288- }
289-
290- return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error_misc ' ));
291- }
280+
281+ if (($ response ->getStatusCode () == 302 ) || ($ response ->getStatusCode () == 301 )) {
282+ return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error_redirect ' , ['endpoint ' => $ this ->webhook_endpoint ]));
283+ }
284+ $ this ->isDisabled = '' ;
285+ $ this ->save_button = trans ('general.save ' );
286+ return session ()->flash ('success ' , trans ('admin/settings/message.webhook.success ' , ['webhook_name ' => $ this ->webhook_name ]));
287+ } catch (\Exception $ e ) {
288+ $ this ->isDisabled = 'disabled ' ;
289+ $ this ->save_button = trans ('admin/settings/general.webhook_presave ' );
290+ return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error ' , ['error_message ' => $ e ->getMessage (), 'app ' => $ this ->webhook_name ]));
291+ }
292+
293+ return session ()->flash ('error ' , trans ('admin/settings/message.webhook.error_misc ' ));
294+ }
292295}
0 commit comments