add multi array, migration, still having issues

This commit is contained in:
Godfrey M 2023-03-01 12:30:32 -08:00
parent e8fc895e51
commit 354f04534e
4 changed files with 256 additions and 180 deletions

View file

@ -8,56 +8,74 @@ use App\Models\Setting;
class SlackSettingsForm extends Component class SlackSettingsForm extends Component
{ {
public $slack_endpoint; public $webhook_endpoint;
public $slack_channel; public $webhook_channel;
public $slack_botname; public $webhook_botname;
public $isDisabled ='disabled' ; public $isDisabled ='disabled' ;
public $integration_app= 'WORKING';
public $webhook_link; public $webhook_link;
public $webhook_selected; public $webhook_selected;
public $webhook_options= ['Slack'=>'fab fa-slack', 'Discord'=>'fab fa-discord', 'Rocket.Chat'=>'fab fa-rocketchat']; public $webhook_options = array(
public $webhook; array(
"name" => 'Slack',
"icon" => 'fab fa-slack',
"placeholder" => "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX",
"link" => 'https://api.slack.com/messaging/webhooks'
),
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'
),
array(
"name" => 'Rocket Chat',
"icon" => 'fab fa-rocketchat',
"placeholder" => "https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXXXX",
"link" => '',
),
);
public $keys;
public $icon; public $icon;
public Setting $setting; public Setting $setting;
protected $rules = [ protected $rules = [
'slack_endpoint' => 'url|required_with:slack_channel|starts_with:https://hooks.slack.com/|nullable', 'webhook_endpoint' => 'url|required_with:slack_channel|starts_with:https://hooks.slack.com/|nullable',
'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable', 'webhook_channel' => 'required_with:slack_endpoint|starts_with:#|nullable',
'slack_botname' => 'string|nullable', 'webhook_botname' => 'string|nullable',
]; ];
public function mount(){ public function mount(){
$this->setting = Setting::getSettings(); $this->setting = Setting::getSettings();
$this->icon =''; $this->webhook_endpoint = $this->setting->webhook_endpoint;
$this->webhook = $this->webhook_selected; $this->webhook_channel = $this->setting->webhook_channel;
$this->slack_endpoint = $this->setting->slack_endpoint; $this->webhook_botname = $this->setting->webhook_botname;
$this->slack_channel = $this->setting->slack_channel; // $this->webhook_options = $this->setting->webhook_selected;lma
$this->slack_botname = $this->setting->slack_botname; $this->keys = array_column($this->webhook_options, 'name');
} }
public function updated($field){ public function updated($field){
$this->webhook_selected = $this->webhook_options;
$this->webhook = $this->webhook_selected;
$this->validateOnly($field ,$this->rules); $this->validateOnly($field ,$this->rules);
} }
public function render() public function render()
{ {
if(empty($this->slack_channel || $this->slack_endpoint)){ if(empty($this->webhook_channel || $this->webhook_endpoint)){
$this->isDisabled= 'disabled'; $this->isDisabled= 'disabled';
} }
if(empty($this->slack_endpoint && $this->slack_channel)){ if(empty($this->webhook_endpoint && $this->webhook_channel)){
$this->isDisabled= ''; $this->isDisabled= '';
} }
return view('livewire.slack-settings-form'); return view('livewire.slack-settings-form');
} }
public function testSlack(){ public function testWebhook(){
$slack = new Client([ $slack = new Client([
'base_url' => e($this->slack_endpoint), 'base_url' => e($this->webhook_endpoint),
'defaults' => [ 'defaults' => [
'exceptions' => false, 'exceptions' => false,
], ],
@ -65,14 +83,14 @@ class SlackSettingsForm extends Component
$payload = json_encode( $payload = json_encode(
[ [
'channel' => e($this->slack_channel), 'channel' => e($this->webhook_channel),
'text' => trans('general.slack_test_msg'), 'text' => trans('general.slack_test_msg'),
'username' => e($this->slack_botname), 'username' => e($this->webhook_botname),
'icon_emoji' => ':heart:', 'icon_emoji' => ':heart:',
]); ]);
try { try {
$slack->post($this->slack_endpoint, ['body' => $payload]); $slack->post($this->webhook_endpoint, ['body' => $payload]);
$this->isDisabled=''; $this->isDisabled='';
return session()->flash('success' , 'Your Slack Integration works!'); return session()->flash('success' , 'Your Slack Integration works!');
@ -91,9 +109,10 @@ class SlackSettingsForm extends Component
{ {
$this->validate($this->rules); $this->validate($this->rules);
$this->setting->slack_endpoint = $this->slack_endpoint; $this->setting->webhook_options = $this->webhook_options;
$this->setting->slack_channel = $this->slack_channel; $this->setting->webhook_endpoint = $this->webhook_endpoint;
$this->setting->slack_botname = $this->slack_botname; $this->setting->webhook_channel = $this->webhook_channel;
$this->setting->webhook_botname = $this->webhook_botname;
$this->setting->save(); $this->setting->save();

View file

@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddsWebhookOptionToSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('settings', function (Blueprint $table) {
$table->string('webhook_selected')->after('slack_botname')->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');
});
}
}

View file

@ -198,16 +198,16 @@ return [
'show_images_in_email' => 'Show images in emails', '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.', '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', 'site_name' => 'Site Name',
'slack' => ':app', 'webhook' => ':app',
'slack_title' => 'Update :app Settings', 'webhook_title' => 'Update :app Settings',
'slack_help' => ':app settings', 'webhook_help' => ':app settings',
'slack_botname' => ':app Botname', 'webhook_botname' => ':app Botname',
'slack_channel' => ':app Channel', 'webhook_channel' => ':app Channel',
'slack_endpoint' => ':app Endpoint', 'webhook_endpoint' => ':app Endpoint',
'slack_integration' => ':app Settings', 'webhook_integration' => ':app Settings',
'slack_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 <a href=":slack_link" target="_new" rel="noopener">create an incoming webhook</a> on your :app account. Click on the <strong>Test :app Integration</strong> button to confirm your settings are correct before saving. ', '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 <a href=":webhook_link" target="_new" rel="noopener">create an incoming webhook</a> on your :app account. Click on the <strong>Test :app Integration</strong> button to confirm your settings are correct before saving. ',
'slack_integration_help_button' => 'Once you have saved your :app information, a test button will appear.', 'webhook_integration_help_button' => 'Once you have saved your :app information, a test button will appear.',
'slack_test_help' => 'Test whether your :app integration is configured correctly. YOU MUST SAVE YOUR UPDATED :app SETTINGS FIRST.', 'webhook_test_help' => 'Test whether your :app integration is configured correctly. YOU MUST SAVE YOUR UPDATED :app SETTINGS FIRST.',
'snipe_version' => 'Snipe-IT version', 'snipe_version' => 'Snipe-IT version',
'support_footer' => 'Support Footer Links ', 'support_footer' => 'Support Footer Links ',
'support_footer_help' => 'Specify who sees the links to the Snipe-IT Support info and Users Manual', 'support_footer_help' => 'Specify who sees the links to the Snipe-IT Support info and Users Manual',

View file

@ -1,6 +1,6 @@
{{-- Page title --}} {{-- Page title --}}
@section('title') @section('title')
{{ trans('admin/settings/general.slack_title', ['app' => $integration_app ]) }} {{ trans('admin/settings/general.webhook_title', ['app' => $webhook_selected->name ]) }}
@parent @parent
@stop @stop
@ -12,18 +12,18 @@
{{-- Page content --}} {{-- Page content --}}
@section('content') @section('content')
<div class="row"> <div class="row">
<div class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2"> <div class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2">
<div class="panel box box-default"> <div class="panel box box-default">
<div class="box-header with-border"> <div class="box-header with-border">
<h2 class="box-title"> <h2 class="box-title">
<i class="{{$icon}}"></i> {{ trans('admin/settings/general.slack',['app' => $integration_app ]) }} <i class="{{$icon}}"></i> {{ trans('admin/settings/general.webhook',['app' => $webhook_selected->name ]) }}
</h2> </h2>
</div> </div>
<div class="box-body"> <div class="box-body">
<div class="col-md-12"> <div class="col-md-12">
<p> <p>
{!! trans('admin/settings/general.slack_integration_help',array('slack_link' => $webhook_link, 'app' => $integration_app )) !!} {!! trans('admin/settings/general.webhook_integration_help',array('webhook_link' => $webhook_link, 'app' => $webhook_selected )) !!}
</p> </p>
<br> <br>
</div> </div>
@ -54,100 +54,118 @@
<div class="col-md-3"> <div class="col-md-3">
<label>Integration Option</label> <label>Integration Option</label>
</div> </div>
<div class="col-md-6" > <div class="col-md-6">
<select wire:model="webhook_selected" {{-- <select wire:model="webhook_options"--}}
aria-label="webhook_selected" {{-- aria-label="webhook_options"--}}
class="form-control " {{-- class="form-control "--}}
> {{-- >--}}
<option value="">{{ trans('admin/settings/general.no_default_group') }}</option> {{-- <option>{{ trans('admin/settings/general.no_default_group') }}</option>--}}
@foreach ($webhook_options as $key => $webhook_option) {{-- @foreach ($webhook_options[$keys] as $webhook_options)--}}
<option value="{{$webhook_option}}" {{ $key ? 'selected' : '' }}> {{-- <option value="{{$key}}" {{ $key ? 'selected' : '' }}>--}}
{{ $key }} {{-- {{ $key }}--}}
</option> {{-- </option>--}}
@endforeach {{-- @endforeach--}}
</select>
{{var_dump($webhook_selected)}} {{-- </select>--}}
</div><br><br><br> </div>
<br><br><br>
</div> </div>
<form class="form-horizontal" role="form" wire:submit.prevent="submit"> <form class="form-horizontal" role="form" wire:submit.prevent="submit">
{{csrf_field()}} {{csrf_field()}}
<!--slack endpoint--> <!--Webhook endpoint-->
<div class="form-group{{ $errors->has('slack_endpoint') ? ' error' : '' }}"> <div class="form-group{{ $errors->has('webhook_endpoint') ? ' error' : '' }}">
<div class="col-md-2"> <div class="col-md-2">
{{ Form::label('slack_endpoint', trans('admin/settings/general.slack_endpoint',['app' => $integration_app ])) }} {{ Form::label('webhook_endpoint', trans('admin/settings/general.webhook_endpoint',['app' => $webhook_selected ])) }}
</div> </div>
<div class="col-md-8 required"> <div class="col-md-8 required">
@if (config('app.lock_passwords')===true) @if (config('app.lock_passwords')===true)
<p class="text-warning"><i class="fas fa-lock"></i> {{ trans('general.feature_disabled') }}</p> <p class="text-warning"><i
<input type="text" wire:model="slack_endpoint" class= 'form-control' placeholder="https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX" {{old('slack_endpoint', $slack_endpoint)}}> class="fas fa-lock"></i> {{ trans('general.feature_disabled') }}</p>
<input type="text" wire:model="webhook_endpoint" class='form-control'
placeholder="https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX" {{old('webhook_endpoint', $webhook_endpoint)}}>
@else @else
<input type="text" wire:model="slack_endpoint" class= 'form-control' placeholder="https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX" {{old('slack_endpoint', $slack_endpoint)}}> <input type="text" wire:model="webhook_endpoint" class='form-control'
placeholder="https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX" {{old('webhook_endpoint', $webhook_endpoint)}}>
@endif @endif
{!! $errors->first('slack_endpoint', '<span class="alert-msg" aria-hidden="true">:message</span>') !!} {!! $errors->first('webhook_endpoint', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
</div> </div>
</div> </div>
<!-- slack channel --> <!-- Webhook channel -->
<div class="form-group{{ $errors->has('slack_channel') ? ' error' : '' }}"> <div class="form-group{{ $errors->has('webhook_channel') ? ' error' : '' }}">
<div class="col-md-2"> <div class="col-md-2">
{{ Form::label('slack_channel', trans('admin/settings/general.slack_channel',['app' => $integration_app ])) }} {{ Form::label('webhook_channel', trans('admin/settings/general.webhook_channel',['app' => $webhook_selected ])) }}
</div> </div>
<div class="col-md-8 required"> <div class="col-md-8 required">
@if (config('app.lock_passwords')===true) @if (config('app.lock_passwords')===true)
<input type="text" wire:model="slack_channel" class='form-control' placeholder="#IT-Ops" value="{{old('slack_channel', $slack_channel)}}"> <input type="text" wire:model="webhook_channel" class='form-control'
<p class="text-warning"><i class="fas fa-lock"></i> {{ trans('general.feature_disabled') }}</p> placeholder="#IT-Ops"
value="{{old('webhook_channel', $webhook_channel)}}">
<p class="text-warning"><i
class="fas fa-lock"></i> {{ trans('general.feature_disabled') }}</p>
@else @else
<input type="text" wire:model="slack_channel" class= 'form-control' placeholder="#IT-Ops" value="{{old('slack_channel', $slack_channel)}}"> <input type="text" wire:model="webhook_channel" class='form-control'
placeholder="#IT-Ops"
value="{{old('webhook_channel', $webhook_channel)}}">
@endif @endif
{!! $errors->first('slack_channel', '<span class="alert-msg" aria-hidden="true">:message</span>') !!} {!! $errors->first('webhook_channel', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
</div> </div>
</div> </div>
<!-- slack botname --> <!-- Webhook botname -->
<div class="form-group{{ $errors->has('slack_botname') ? ' error' : '' }}"> <div class="form-group{{ $errors->has('webhook_botname') ? ' error' : '' }}">
<div class="col-md-2"> <div class="col-md-2">
{{ Form::label('slack_botname', trans('admin/settings/general.slack_botname',['app' => $integration_app ])) }} {{ Form::label('webhook_botname', trans('admin/settings/general.webhook_botname',['app' => $webhook_selected ])) }}
</div> </div>
<div class="col-md-8"> <div class="col-md-8">
@if (config('app.lock_passwords')===true) @if (config('app.lock_passwords')===true)
<input type="text" wire:model="slack_botname" class= 'form-control' placeholder="Snipe-Bot" {{old('slack_botname', $slack_botname)}}> <input type="text" wire:model="webhook_botname" class='form-control'
<p class="text-warning"><i class="fas fa-lock"></i> {{ trans('general.feature_disabled') }}</p> placeholder="Snipe-Bot" {{old('webhook_botname', $webhook_botname)}}>
<p class="text-warning"><i
class="fas fa-lock"></i> {{ trans('general.feature_disabled') }}</p>
@else @else
<input type="text" wire:model="slack_botname" class= 'form-control' placeholder="Snipe-Bot" {{old('slack_botname', $slack_botname)}}> <input type="text" wire:model="webhook_botname" class='form-control'
placeholder="Snipe-Bot" {{old('webhook_botname', $webhook_botname)}}>
@endif @endif
{!! $errors->first('slack_botname', '<span class="alert-msg" aria-hidden="true">:message</span>') !!} {!! $errors->first('webhook_botname', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
</div><!--col-md-10--> </div><!--col-md-10-->
</div> </div>
<!--Slack Integration Test--> <!--Webhook Integration Test-->
@if($slack_endpoint != null && $slack_channel != null) @if($webhook_endpoint != null && $webhook_channel != null)
<div class="form-group"> <div class="form-group">
<div class="col-md-offset-2 col-md-8"> <div class="col-md-offset-2 col-md-8">
<a href="#" wire:click.prevent="testSlack" class="btn btn-default btn-sm pull-left"><span>{!! trans('admin/settings/general.slack_test',['app' => $integration_app ]) !!}</span></a> <a href="#" wire:click.prevent="testWebhook"
<div wire:loading><span style="padding-left: 5px; font-size: 20px"><i class="fas fa-spinner fa-spin"></i></span></div> class="btn btn-default btn-sm pull-left"><span>{!! trans('admin/settings/general.webhook_test',['app' => $webhook_selected ]) !!}</span></a>
<div wire:loading><span style="padding-left: 5px; font-size: 20px"><i
class="fas fa-spinner fa-spin"></i></span></div>
</div> </div>
</div> </div>
@endif @endif
<div class="box-footer" style="margin-top: 45px;"> <div class="box-footer" style="margin-top: 45px;">
<div class="text-right col-md-12"> <div class="text-right col-md-12">
<a class="btn btn-link text-left" href="{{ route('settings.index') }}">{{ trans('button.cancel') }}</a> <a class="btn btn-link text-left"
<button type="submit" {{$isDisabled}} class="btn btn-primary"><i class="fas fa-check icon-white" aria-hidden="true"></i> {{ trans('general.save') }}</button> href="{{ route('settings.index') }}">{{ trans('button.cancel') }}</a>
<button type="submit" {{$isDisabled}} class="btn btn-primary"><i
class="fas fa-check icon-white"
aria-hidden="true"></i> {{ trans('general.save') }}</button>
</div> </div>
</div><!--box-footer--> </div><!--box-footer-->
</form> </form>
</div> <!-- /box --> </div> <!-- /box -->
</div> <!-- /.col-md-8--> </div> <!-- /.col-md-8-->
</div> <!-- /.row--> </div> <!-- /.row-->
@stop @stop
@push('scripts') @push('scripts')
<script> <script>
// function formatText (icon) { // function formatText (icon) {
// return $('<span><i class="fab ' + $(icon.element).data('icon') + '"></i> ' + icon.text + '</span>'); // return $('<span><i class="fab ' + $(icon.element).data('icon') + '"></i> ' + icon.text + '</span>');
// }; // };
@ -166,5 +184,5 @@
// }); // });
</script> </script>
@endpush @endpush