Enhance the system link/register selection

This commit is contained in:
Deon George 2023-10-05 11:11:21 +11:00
parent 32c0088339
commit 495a27cfed
3 changed files with 40 additions and 115 deletions

View File

@ -826,11 +826,11 @@ class SystemController extends Controller
if ($request->isMethod('GET'))
return view('user.system.register');
if ($request->action === 'register' && $request->system_id)
if ($request->action === 'register' && $request->name && is_numeric($request->name))
return view('user.system.widget.register_confirm')
->with('o',System::findOrFail($request->system_id));
->with('o',System::findOrFail($request->name));
$o = System::findOrNew($request->system_id);
$o = System::findOrNew(is_numeric($request->system_id) ? $request->system_id : NULL);
// If the system exists, and we are 'register', we'll start the address claim process
if ($o->exists && $request->action === 'Link') {

View File

@ -8,7 +8,7 @@ use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use App\Classes\FTN\Packet;
use App\Models\System;
use App\Models\{Setup,System};
// @todo rename to SystemRegisterRequest
class SystemRegister extends FormRequest
@ -22,9 +22,17 @@ class SystemRegister extends FormRequest
*/
public function authorize(Request $request)
{
$this->so = System::findOrNew($request->system_id);
$this->so = new System;
return Gate::allows($this->so->users->count() ? 'update' : 'register',$this->so);
if (is_numeric($request->name)) {
$this->so = System::findOrNew($request->name);
// Cannot claim this site
if ($this->so->id === Setup::findOrFail(config('app.id'))->system_id)
return FALSE;
}
return Gate::allows(is_numeric($request->name) && $this->so->users->count() ? 'update' : 'register',$this->so);
}
public function messages(): array

View File

@ -19,12 +19,18 @@
<div class="col-4">
<label for="system" class="form-label">BBS Name</label>
<div class="input-group has-validation">
<span class="input-group-text"><i class="bi bi-pc"></i></span>
<input type="text" style="z-index: 0" class="form-control @error('name') is-invalid @enderror" id="name" placeholder="BBS Name" name="name" value="{{ old('name') }}" required autofocus>
<span id="search-icon" style="width: 0;"><i style="border-radius: 50%;" class="spinner-border spinner-border-sm text-dark d-none"></i></span>
<div id="system_search_results"></div>
<span class="input-group-text"><i class="bi bi-laptop-fill"></i></span>
<select style="width: 80%;" class="form-select @error('system_id') is-invalid @enderror" id="system" name="id" required>
<option value="">&nbsp;</option>
@foreach (\App\Models\System::select(['systems.id','systems.name'])
->active()
->whereRaw('id NOT IN (SELECT system_id FROM system_user)')
->cursor() as $oo)
<option value="{{ $oo->id }}" @if(old('id')===$oo->id)selected @endif>{{ $oo->name }}</option>
@endforeach
</select>
<span class="invalid-feedback" role="alert">
@error('name')
@error('system_id')
{{ $message }}
@else
BBS Name is required.
@ -66,45 +72,20 @@
@endsection
@section('page-css')
<style>
input#name + span {
left: -1.5em;
top: 0.5em;
position:relative
}
div#system_search_results ul {
color:#eeeeee;
background-color:#292929;
font-size: .85rem;
padding: 0 5px 0 5px;
z-index: 99;
top: -0.5em;
left: 31em !important;
}
div#system_search_results ul li.dropdown-header {
display: block;
color: #fff !important;
}
div#system_search_results ul li,
div#system_search_results ul li a {
display: block;
color: #aaa !important;
margin: 0 !important;
border: 0 !important;
width: inherit;
text-indent: 0 !important;
padding-left: 0 !important;
}
div#system_search_results ul li:hover {
padding-left: 0;
text-indent: 0;
}
div#system_search_results ul li:before {
content:""!important
}
</style>
@css('select2')
@append
@section('page-scripts')
@js('select2')
<script type="text/javascript">
$(document).ready(function() {
$('#system').select2({
placeholder: 'See if your BBS exists',
tags: true,
});
});
</script>
<script>
var system_id;
var noauth = new bootstrap.Modal(document.getElementById('no-auth'), {});
@ -119,7 +100,7 @@
$.ajax({
url : '{{ url('user/system/register') }}',
type : 'POST',
data : { system_id: system_id,name: $('#name').val(),action: 'register',old: {!! json_encode(old()) !!} },
data : { system_id: system_id,name: $('#system').val(),action: 'register',old: {!! json_encode(old()) !!} },
dataType : 'json',
async : true,
cache : false,
@ -136,7 +117,7 @@
@if($errors->count())
@foreach($errors->keys() as $key)
validation('{{ $key }}','{{ $errors->first($key) }}');
validation('{{ $key }}','{{ $errors->first($key) }}');
@endforeach
@endif
@ -171,78 +152,14 @@
}
$(document).ready(function() {
$('input[id=name]').typeahead({
autoSelect: false,
scrollHeight: 10,
theme: 'bootstrap5',
delay: 500,
minLength: 3,
items: {{ $search_limit ?? 5 }},
fitToElement: false,
selectOnBlur: false,
appendTo: "#system_search_results",
source: function (query,process) {
systemsearch('{{ url('systems/orphan') }}',query,process);
},
matcher: function () { return true; },
// Disable sorting and just return the items (items should by the ajax method)
sorter: function(items) {
return items;
},
updater: function (item) {
system_id = item.id;
return item.name;
},
})
.on('keyup keypress', function(event) {
var key = event.keyCode || event.which;
if (key === 13) {
event.preventDefault();
return false;
}
});
$('button[name=submit]').on('click',function() {
icon = $(this).parent().find('i');
if (! $('#name').val())
if (! $('#system').val())
return;
getform(icon);
})
});
var c=0;
var systemsearch = _.debounce(function(url,query,process,icon){
icon = $('#search-icon').find('i');
$.ajax({
url : url,
type : 'GET',
data : 'term=' + query,
dataType : 'JSON',
async : true,
cache : false,
beforeSend : function() {
if (c++ === 0) {
icon.removeClass('d-none');
}
},
success : function(data) {
// if json is null, means no match, won't do again.
if(data==null || (data.length===0)) return;
process(data);
},
complete : function() {
if (--c === 0) {
icon.addClass('d-none');
}
}
})
}, 500);
</script>
@append