diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index 5ac958a8ac..012f40e399 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -288,7 +288,7 @@ class AssetModelsController extends Controller public function show($modelId = null) { $this->authorize('view', AssetModel::class); - $model = AssetModel::withTrashed()->find($modelId); + $model = AssetModel::withTrashed()->withCount('assets')->find($modelId); if (isset($model->id)) { return view('models/view', compact('model')); diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index 2947344a50..158f318133 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -49,15 +49,86 @@ class BulkAssetsController extends Controller 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 = []; diff --git a/resources/views/models/view.blade.php b/resources/views/models/view.blade.php index 29cfd99c11..a98e14b4ee 100755 --- a/resources/views/models/view.blade.php +++ b/resources/views/models/view.blade.php @@ -43,7 +43,7 @@ @@ -342,7 +342,7 @@ @endcan @can('delete', \App\Models\AssetModel::class) - @if ($model->assets->count() > 0) + @if ($model->assets_count > 0)
diff --git a/resources/views/partials/asset-bulk-actions.blade.php b/resources/views/partials/asset-bulk-actions.blade.php index d56c7dc394..d46b4e525b 100644 --- a/resources/views/partials/asset-bulk-actions.blade.php +++ b/resources/views/partials/asset-bulk-actions.blade.php @@ -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) --}} - + {{-- The sort and order will only be used if the cookie is actually empty (like on first-use) --}} + +