Skip to content

Commit

Permalink
Merge pull request #14162 from snipe/bug/sc-24511
Browse files Browse the repository at this point in the history
Fixed #14114 - correctly sort related fields for labels
  • Loading branch information
snipe authored Jan 24, 2024
2 parents 5909654 + 105d489 commit 57a8630
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 13 deletions.
79 changes: 75 additions & 4 deletions app/Http/Controllers/Assets/BulkAssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,86 @@ public function edit(Request $request)
return redirect()->back()->with('error', trans('admin/hardware/message.update.no_assets_selected'));
}

$asset_ids = $request->input('ids');

// Figure out where we need to send the user after the update is complete, and store that in the session
$bulk_back_url = request()->headers->get('referer');
session(['bulk_back_url' => $bulk_back_url]);

$allowed_columns = [
'id',
'name',
'asset_tag',
'serial',
'model_number',
'last_checkout',
'notes',
'expected_checkin',
'order_number',
'image',
'assigned_to',
'created_at',
'updated_at',
'purchase_date',
'purchase_cost',
'last_audit_date',
'next_audit_date',
'warranty_months',
'checkout_counter',
'checkin_counter',
'requests_counter',
'byod',
'asset_eol_date',
];

$asset_ids = $request->input('ids');
// Using the 'short-ternary' A/K/A "Elvis operator" '?:' here because ->input() might return an empty string
list($sortname,$sortdir) = explode(" ",$request->input('sort') ?: 'id ASC');
$assets = Asset::with('assignedTo', 'location', 'model')->whereIn('id', $asset_ids)->orderBy($sortname, $sortdir)->get();

/**
* Make sure the column is allowed, and if it's a custom field, make sure we strip the custom_fields. prefix
*/
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort_override = str_replace('custom_fields.', '', $request->input('sort'));

// This handles all of the pivot sorting below (versus the assets.* fields in the allowed_columns array)
$column_sort = in_array($sort_override, $allowed_columns) ? $sort_override : 'assets.id';

$assets = Asset::with('assignedTo', 'location', 'model')->whereIn('assets.id', $asset_ids);

switch ($sort_override) {
case 'model':
$assets->OrderModels($order);
break;
case 'model_number':
$assets->OrderModelNumber($order);
break;
case 'category':
$assets->OrderCategory($order);
break;
case 'manufacturer':
$assets->OrderManufacturer($order);
break;
case 'company':
$assets->OrderCompany($order);
break;
case 'location':
$assets->OrderLocation($order);
case 'rtd_location':
$assets->OrderRtdLocation($order);
break;
case 'status_label':
$assets->OrderStatus($order);
break;
case 'supplier':
$assets->OrderSupplier($order);
break;
case 'assigned_to':
$assets->OrderAssigned($order);
break;
default:
$assets->orderBy($column_sort, $order);
break;
}

$assets = $assets->get();

$models = $assets->unique('model_id');
$modelNames = [];
Expand Down
5 changes: 3 additions & 2 deletions resources/views/partials/asset-bulk-actions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
'id' => (isset($id_formname)) ? $id_formname : 'assetsBulkForm',
]) }}

{{-- The 'id ASC' will only be used if the cookie is actually empty (like on first-use) --}}
<input name="sort" type="hidden" value="id ASC">
{{-- The sort and order will only be used if the cookie is actually empty (like on first-use) --}}
<input name="sort" type="hidden" value="assets.id">
<input name="order" type="hidden" value="asc">
<label for="bulk_actions">
<span class="sr-only">
{{ trans('button.bulk_actions') }}
Expand Down
16 changes: 9 additions & 7 deletions resources/views/partials/bootstrap-table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,24 @@ function dateRowCheckStyle(value) {
// Initialize sort-order for bulk actions (label-generation) for snipe-tables
$('.snipe-table').each(function (i, table) {
table_cookie_segment = $(table).data('cookie-id-table');
name = '';
direction = '';
sort = '';
order = '';
cookies = document.cookie.split(";");
for(i in cookies) {
cookiedef = cookies[i].split("=", 2);
cookiedef[0] = cookiedef[0].trim();
if (cookiedef[0] == table_cookie_segment + ".bs.table.sortOrder") {
direction = cookiedef[1];
order = cookiedef[1];
}
if (cookiedef[0] == table_cookie_segment + ".bs.table.sortName") {
name = cookiedef[1];
sort = cookiedef[1];
}
}
if (name && direction) {
if (sort && order) {
domnode = $($(this).data('bulk-form-id')).get(0);
if ( domnode && domnode.elements && domnode.elements.sort ) {
domnode.elements.sort.value = name + " " + direction;
domnode.elements.sort.value = sort;
domnode.elements.order.value = order;
}
}
});
Expand All @@ -190,7 +191,8 @@ function dateRowCheckStyle(value) {
domnode = $($(this).data('bulk-form-id')).get(0);
// make safe in case there isn't a bulk-form-id, or it's not found, or has no 'sort' element
if ( domnode && domnode.elements && domnode.elements.sort ) {
domnode.elements.sort.value = name + " " + order;
domnode.elements.sort.value = name;
domnode.elements.order.value = order;
}
});
Expand Down

0 comments on commit 57a8630

Please sign in to comment.