Clean up the fieldset experience for custom fields for users

This commit is contained in:
Brady Wetherington 2023-09-21 14:56:29 +01:00
parent 58656fbaec
commit 3a3f26ba8b
4 changed files with 28 additions and 7 deletions

View file

@ -659,7 +659,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
];
/**

View file

@ -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;
@ -33,6 +34,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();
@ -68,7 +73,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,15 @@ 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'));
}

View file

@ -153,12 +153,16 @@
<!-- 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
<!-- Show in list view -->
<div class="col-md-9 col-md-offset-3">
@ -210,7 +214,7 @@
</div>
@if ($fieldsets->count() > 0)
@if ($fieldsets->count() > 0 && Request::query('tab') != 1)
<!-- begin fieldset columns -->
<div class="col-md-4">

View file

@ -33,6 +33,9 @@
</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">
@ -134,6 +137,7 @@
</div> <!-- .row-->
@endif
@endcan
@can('view', \App\Models\CustomField::class)
<div class="row">
@ -222,7 +226,8 @@
<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" data-tooltip="true" title="{{ trans('general.update') }}">
<a href="{{ route('fields.edit', $field->id) }}?tab={{ array_search($field->type, Helper::$itemtypes_having_custom_fields) }}"
class="btn btn-warning btn-sm" data-tooltip="true" title="{{ trans('general.update') }}">
<i class="fas fa-pencil-alt" aria-hidden="true"></i>
<span class="sr-only">{{ trans('button.edit') }}</span>
</a>
@ -230,7 +235,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" data-tooltip="true" title="{{ trans('general.cannot_be_deleted') }}" disabled>
<i class="fas fa-trash" aria-hidden="true"></i>
<span class="sr-only">{{ trans('button.delete') }}</span></button>