diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index 9b2deade86..bfe6298174 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -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 ]; /** diff --git a/app/Http/Controllers/CustomFieldsController.php b/app/Http/Controllers/CustomFieldsController.php index 61eba6b7f9..f4e89c84c1 100644 --- a/app/Http/Controllers/CustomFieldsController.php +++ b/app/Http/Controllers/CustomFieldsController.php @@ -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')); } diff --git a/resources/views/custom_fields/fields/edit.blade.php b/resources/views/custom_fields/fields/edit.blade.php index ad833223cb..51a1597fcf 100644 --- a/resources/views/custom_fields/fields/edit.blade.php +++ b/resources/views/custom_fields/fields/edit.blade.php @@ -120,16 +120,16 @@ - -