Merge pull request #15518 from snipe/sort_by_numeric_custom_fields

Fixed #11634 - Sort by numeric columns for numeric custom fields
This commit is contained in:
snipe 2024-09-19 20:32:03 +01:00 committed by GitHub
commit 454796e52a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -372,7 +372,29 @@ class AssetsController extends Controller
$assets->OrderAssigned($order); $assets->OrderAssigned($order);
break; break;
default: default:
$numeric_sort = false;
// Search through the custom fields array to see if we're sorting on a custom field
if (array_search($column_sort, $all_custom_fields->pluck('db_column')->toArray()) !== false) {
// Check to see if this is a numeric field type
foreach ($all_custom_fields as $field) {
if (($field->db_column == $sort_override) && ($field->format == 'NUMERIC')) {
$numeric_sort = true;
break;
}
}
// This may not work for all databases, but it works for MySQL
if ($numeric_sort) {
$assets->orderByRaw($sort_override . ' * 1 ' . $order);
} else {
$assets->orderBy($sort_override, $order);
}
} else {
$assets->orderBy($column_sort, $order); $assets->orderBy($column_sort, $order);
}
break; break;
} }