Merge pull request #17292 from Godmartinz/fail_with_inputs

FIXED: #17194 Return to bulk edit with errors and inputs
This commit is contained in:
snipe
2025-07-21 12:03:52 +01:00
committed by GitHub
4 changed files with 80 additions and 17 deletions

View File

@@ -133,8 +133,17 @@ class Handler extends ExceptionHandler
// This is traaaaash but it handles models that are not found while using route model binding :(
// The only alternative is to set that at *each* route, which is crazypants
if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
$ids = method_exists($e, 'getIds') ? $e->getIds() : [];
// This gets the MVC model name from the exception and formats in a way that's less fugly
if (in_array('bulkedit', $ids, true)) {
$error_array = session()->get('bulk_asset_errors');
return redirect()
->route('hardware.bulkedit')
->withErrors($error_array, 'bulk_asset_errors')
->withInput();
}
// This gets the MVC model name from the exception and formats in a way that's less fugly
$model_name = strtolower(implode(" ", preg_split('/(?=[A-Z])/', last(explode('\\', $e->getModel())))));
$route = str_plural(strtolower(last(explode('\\', $e->getModel())))).'.index';

View File

@@ -161,6 +161,7 @@ class BulkAssetsController extends Controller
$models = $assets->unique('model_id');
$modelNames = [];
foreach($models as $model) {
$modelNames[] = $model->model->name;
}
@@ -196,7 +197,6 @@ class BulkAssetsController extends Controller
case 'edit':
$this->authorize('update', Asset::class);
return view('hardware/bulk')
->with('assets', $asset_ids)
->with('statuslabel_list', Helper::statusLabelList())
@@ -224,11 +224,8 @@ class BulkAssetsController extends Controller
$error_array = array();
// Get the back url from the session and then destroy the session
$bulk_back_url = route('hardware.index');
if ($request->session()->has('bulk_back_url')) {
$bulk_back_url = $request->session()->pull('bulk_back_url');
}
$bulk_back_url = $request->session()->pull('bulk_back_url', url()->previous());
$custom_field_columns = CustomField::all()->pluck('db_column')->toArray();
@@ -543,7 +540,13 @@ class BulkAssetsController extends Controller
} // end asset foreach
if ($has_errors > 0) {
return redirect($bulk_back_url)->with('bulk_asset_errors', $error_array);
session()->put('bulkedit_ids', $request->input('ids'));
session()->put('bulk_asset_errors',$error_array);
return redirect()
->route('hardware.bulkedit')
->with('bulk_asset_errors', $error_array)
->withInput();
}
return redirect($bulk_back_url)->with('success', trans('admin/hardware/message.update.success'));
@@ -735,4 +738,33 @@ class BulkAssetsController extends Controller
}
return false;
}
public function bulkEditForm(): View|RedirectResponse
{
$this->authorize('update', Asset::class);
$asset_ids = session()->pull('bulkedit_ids', []);
if (empty($asset_ids)) {
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.update.no_assets_selected'));
}
$assets = Asset::with('model')->withTrashed()->whereIn('id', $asset_ids)->get();
if ($assets->isEmpty()) {
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.update.assets_do_not_exist_or_are_invalid'));
}
$models = $assets->unique('model_id');
$modelNames = [];
foreach ($models as $model) {
$modelNames[] = $model->model->name;
}
return view('hardware/bulk')
->with('assets', $asset_ids)
->with('statuslabel_list', Helper::statusLabelList())
->with('models', $models->pluck(['model']))
->with('modelNames', $modelNames);
}
}

View File

@@ -57,21 +57,42 @@
@endif
@elseif ($field->element=='checkbox')
<!-- Checkboxes -->
@php
$fieldName = $field->db_column_name();
$oldValues = old($fieldName);
$defaultValues = array_map('trim', explode(',', $field->defaultValue($model->id)));
$currentValues = isset($item) ? array_map('trim', explode(',', $item->{$fieldName})) : $defaultValues;
$selectedValues = is_array($oldValues) ? $oldValues : $currentValues;
@endphp
@foreach ($field->formatFieldValuesAsArray() as $key => $value)
<label class="form-control">
<input type="checkbox"
name="{{ $fieldName }}[]"
value="{{ $key }}"
{{ in_array($key, $selectedValues) ? 'checked' : '' }}>
{{ $value }}
</label>
@endforeach
@elseif ($field->element=='radio')
@php
$fieldName = $field->db_column_name();
$oldValue = old($fieldName);
$default = trim($field->defaultValue($model->id));
$current = isset($item) ? trim($item->{$fieldName}) : $default;
$selectedValue = $oldValue !== null ? $oldValue : $current;
@endphp
@foreach ($field->formatFieldValuesAsArray() as $key => $value)
<label class="form-control">
<input type="checkbox" value="{{ $value }}" name="{{ $field->db_column_name() }}[]" {{ isset($item) ? (in_array($value, array_map('trim', explode(',', $item->{$field->db_column_name()}))) ? ' checked="checked"' : '') : (old($field->db_column_name()) != '' ? ' checked="checked"' : (in_array($key, array_map('trim', explode(',', $field->defaultValue($model->id)))) ? ' checked="checked"' : '')) }}>
<input type="radio"
name="{{ $fieldName }}"
value="{{ $key }}"
{{ $selectedValue == $key ? 'checked' : '' }}>
{{ $value }}
</label>
@endforeach
@elseif ($field->element=='radio')
@foreach ($field->formatFieldValuesAsArray() as $value)
<label class="form-control">
<input type="radio" value="{{ $value }}" name="{{ $field->db_column_name() }}" {{ isset($item) ? ($item->{$field->db_column_name()} == $value ? ' checked="checked"' : '') : (old($field->db_column_name()) != '' ? ' checked="checked"' : (in_array($value, explode(', ', $field->defaultValue($model->id))) ? ' checked="checked"' : '')) }}>
{{ $value }}
</label>
@endforeach
<button type="button"
class="btn btn-default btn-xs clear-radio"
data-target-name="{{ $field->db_column_name() }}">

View File

@@ -152,6 +152,7 @@ Route::group(
Route::delete('{asset}/showfile/{fileId}/delete',
[AssetFilesController::class, 'destroy']
)->name('delete/assetfile')->withTrashed();
Route::get('hardware/bulkedit', [BulkAssetsController::class, 'bulkEditForm'])->name('hardware.bulkedit');
Route::post(
'bulkedit',