mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-09 23:24:06 -08:00
Clean up the fieldset experience for custom fields for users
This commit is contained in:
parent
8428b2a04a
commit
bf6c48d695
|
@ -583,7 +583,7 @@ class Helper
|
|||
static $itemtypes_having_custom_fields = [
|
||||
0 => \App\Models\Asset::class,
|
||||
1 => \App\Models\User::class,
|
||||
2 => \App\Models\Accessory::class
|
||||
// 2 => \App\Models\Accessory::class
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,6 +6,7 @@ use App\Helpers\Helper;
|
|||
use App\Http\Requests\CustomFieldRequest;
|
||||
use App\Models\CustomField;
|
||||
use App\Models\CustomFieldset;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Http\Request;
|
||||
use Redirect;
|
||||
|
@ -34,6 +35,10 @@ class CustomFieldsController extends Controller
|
|||
public function index(Request $request)
|
||||
{
|
||||
$this->authorize('view', CustomField::class);
|
||||
if ( $request->input('tab') == 1 ) {
|
||||
// Users section, make sure to auto-create the first fieldset if so
|
||||
CustomFieldset::firstOrCreate(['type' => Helper::$itemtypes_having_custom_fields[1]], ['name' => 'default']);
|
||||
}
|
||||
|
||||
$fieldsets = CustomFieldset::with('fields')->where("type", Helper::$itemtypes_having_custom_fields[$request->get('tab',0)])->get(); //cannot eager-load 'customizable' because it's not a relation
|
||||
$fields = CustomField::with('fieldset')->where("type", Helper::$itemtypes_having_custom_fields[$request->get('tab',0)])->get();
|
||||
|
@ -69,7 +74,7 @@ class CustomFieldsController extends Controller
|
|||
public function create(Request $request)
|
||||
{
|
||||
$this->authorize('create', CustomField::class);
|
||||
$fieldsets = CustomFieldset::get();
|
||||
$fieldsets = CustomFieldset::where('type', Helper::$itemtypes_having_custom_fields[$request->get('tab')])->get();
|
||||
|
||||
return view('custom_fields.fields.edit', [
|
||||
'predefinedFormats' => Helper::predefined_formats(),
|
||||
|
@ -128,7 +133,10 @@ class CustomFieldsController extends Controller
|
|||
|
||||
// Sync fields with fieldsets
|
||||
$fieldset_array = $request->input('associate_fieldsets');
|
||||
if ($request->has('associate_fieldsets') && (is_array($fieldset_array))) {
|
||||
if ($request->get('tab') == 1 ) {
|
||||
$fieldset_array = [CustomFieldset::firstOrCreate(['type' => User::class],['name' => 'default'])->id => true];
|
||||
}
|
||||
if (($request->has('associate_fieldsets') || $request->get('tab') == 1) && (is_array($fieldset_array))) {
|
||||
$field->fieldset()->sync(array_keys($fieldset_array));
|
||||
} else {
|
||||
$field->fieldset()->sync([]);
|
||||
|
@ -188,11 +196,16 @@ class CustomFieldsController extends Controller
|
|||
if ($field = CustomField::find($field_id)) {
|
||||
$this->authorize('delete', $field);
|
||||
|
||||
if ($field->type == User::class) {
|
||||
$field->fieldset()->detach(); // remove from 'default' group (and others, if they exist in the future!)
|
||||
}
|
||||
if (($field->fieldset) && ($field->fieldset->count() > 0)) {
|
||||
return redirect()->back()->withErrors(['message' => 'Field is in-use']);
|
||||
}
|
||||
$type = $field->type;
|
||||
$field->delete();
|
||||
return redirect()->route('fields.index',['tab' => Request::query('tab',0)])
|
||||
return redirect()->route('fields.index',['tab' => array_search($type, Helper::$itemtypes_having_custom_fields)])
|
||||
|
||||
->with('success', trans('admin/custom_fields/message.field.delete.success'));
|
||||
}
|
||||
|
||||
|
|
|
@ -120,16 +120,16 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<!-- Auto-Add to Future Fieldsets -->
|
||||
<div class="form-group {{ $errors->has('auto_add_to_fieldsets') ? ' has-error' : '' }}" id="auto_add_to_fieldsets">
|
||||
@if (Request::query('tab') != 1)
|
||||
<div class="col-md-9 col-md-offset-3">
|
||||
<label class="form-control">
|
||||
<input type="checkbox" name="auto_add_to_fieldsets" aria-label="auto_add_to_fieldsets" value="1"{{ (old('auto_add_to_fieldsets') || $field->auto_add_to_fieldsets) ? ' checked="checked"' : '' }}>
|
||||
{{ trans('admin/custom_fields/general.auto_add_to_fieldsets') }}
|
||||
</label>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<div class="col-md-9 col-md-offset-3">
|
||||
<label class="form-control">
|
||||
|
@ -182,7 +182,7 @@
|
|||
|
||||
</div>
|
||||
|
||||
@if ($fieldsets->count() > 0)
|
||||
@if ($fieldsets->count() > 0 && Request::query('tab') != 1)
|
||||
<!-- begin fieldset columns -->
|
||||
<div class="col-md-4">
|
||||
|
||||
|
|
|
@ -23,13 +23,16 @@
|
|||
{{-- TODO - generalize this so it's less 'hardcoded' --}}
|
||||
<li {!! !Request::query('tab') ? 'class="active"': '' !!}><a href="{{ route("fields.index",["tab" => 0]) }}">Asset Custom Fields</a></li>
|
||||
<li {!! Request::query('tab') == 1 ? 'class="active"': '' !!}><a href="{{ route("fields.index",["tab" => 1]) }}">Users</a></li>
|
||||
<li {!! Request::query('tab') == 2 ? 'class="active"': '' !!}><a href="{{ route("fields.index",["tab" => 2]) }}">Accessories</a></li>
|
||||
{{-- <li {!! Request::query('tab') == 2 ? 'class="active"': '' !!}><a href="{{ route("fields.index",["tab" => 2]) }}">Accessories</a></li>--}}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{-- Do not show fieldsets for Users' customf ields --}}
|
||||
@if(Request::query('tab') != 1)
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="box box-default">
|
||||
|
@ -119,6 +122,7 @@
|
|||
|
||||
|
||||
</div> <!-- .row-->
|
||||
@endif
|
||||
@endcan
|
||||
@can('view', \App\Models\CustomField::class)
|
||||
<div class="row">
|
||||
|
@ -201,7 +205,7 @@
|
|||
<nobr>
|
||||
{{ Form::open(array('route' => array('fields.destroy', $field->id), 'method' => 'delete', 'style' => 'display:inline-block')) }}
|
||||
@can('update', $field)
|
||||
<a href="{{ route('fields.edit', $field->id) }}" class="btn btn-warning btn-sm">
|
||||
<a href="{{ route('fields.edit', $field->id) }}?tab={{ array_search($field->type, Helper::$itemtypes_having_custom_fields) }}" class="btn btn-warning btn-sm">
|
||||
<i class="fas fa-pencil-alt" aria-hidden="true"></i>
|
||||
<span class="sr-only">{{ trans('button.edit') }}</span>
|
||||
</a>
|
||||
|
@ -209,7 +213,7 @@
|
|||
|
||||
@can('delete', $field)
|
||||
|
||||
@if($field->fieldset->count()>0)
|
||||
@if($field->fieldset->count()>0 && Request::query('tab') != 1 )
|
||||
<button type="submit" class="btn btn-danger btn-sm disabled" disabled>
|
||||
<i class="fas fa-trash" aria-hidden="true"></i>
|
||||
<span class="sr-only">{{ trans('button.delete') }}</span></button>
|
||||
|
|
Loading…
Reference in a new issue