diff --git a/app/Http/Controllers/Api/StatuslabelsController.php b/app/Http/Controllers/Api/StatuslabelsController.php index ce61d653f5..754ebf7323 100644 --- a/app/Http/Controllers/Api/StatuslabelsController.php +++ b/app/Http/Controllers/Api/StatuslabelsController.php @@ -25,9 +25,17 @@ class StatuslabelsController extends Controller public function index(Request $request) : array { $this->authorize('view', Statuslabel::class); - $allowed_columns = ['id', 'name', 'created_at', 'assets_count', 'color', 'notes', 'default_label']; + $allowed_columns = [ + 'id', + 'name', + 'created_at', + 'assets_count', + 'color', + 'notes', + 'default_label' + ]; - $statuslabels = Statuslabel::withCount('assets as assets_count'); + $statuslabels = Statuslabel::with('adminuser')->withCount('assets as assets_count'); if ($request->filled('search')) { $statuslabels = $statuslabels->TextSearch($request->input('search')); @@ -54,10 +62,18 @@ class StatuslabelsController extends Controller // Make sure the offset and limit are actually integers and do not exceed system limits $offset = ($request->input('offset') > $statuslabels->count()) ? $statuslabels->count() : app('api_offset_value'); $limit = app('api_limit_value'); - $order = $request->input('order') === 'asc' ? 'asc' : 'desc'; - $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; - $statuslabels->orderBy($sort, $order); + $sort_override = $request->input('sort'); + $column_sort = in_array($sort_override, $allowed_columns) ? $sort_override : 'created_at'; + + switch ($sort_override) { + case 'created_by': + $statuslabels = $statuslabels->OrderByCreatedBy($order); + break; + default: + $statuslabels = $statuslabels->orderBy($column_sort, $order); + break; + } $total = $statuslabels->count(); $statuslabels = $statuslabels->skip($offset)->take($limit)->get(); diff --git a/app/Http/Transformers/StatuslabelsTransformer.php b/app/Http/Transformers/StatuslabelsTransformer.php index 41dd336068..751edb7016 100644 --- a/app/Http/Transformers/StatuslabelsTransformer.php +++ b/app/Http/Transformers/StatuslabelsTransformer.php @@ -30,6 +30,10 @@ class StatuslabelsTransformer 'default_label' => ($statuslabel->default_label == '1') ? true : false, 'assets_count' => (int) $statuslabel->assets_count, 'notes' => e($statuslabel->notes), + 'created_by' => ($statuslabel->adminuser) ? [ + 'id' => (int) $statuslabel->adminuser->id, + 'name'=> e($statuslabel->adminuser->present()->fullName()), + ] : null, 'created_at' => Helper::getFormattedDateObject($statuslabel->created_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($statuslabel->updated_at, 'datetime'), ]; diff --git a/app/Models/Statuslabel.php b/app/Models/Statuslabel.php index 0f8a0b6075..c1bcc3042d 100755 --- a/app/Models/Statuslabel.php +++ b/app/Models/Statuslabel.php @@ -64,6 +64,11 @@ class Statuslabel extends SnipeModel return $this->hasMany(\App\Models\Asset::class, 'status_id'); } + public function adminuser() + { + return $this->belongsTo(\App\Models\User::class, 'created_by'); + } + /** * Gets the status label type * @@ -161,4 +166,9 @@ class Statuslabel extends SnipeModel return $statustype; } + + public function scopeOrderByCreatedBy($query, $order) + { + return $query->leftJoin('users as admin_sort', 'status_labels.created_by', '=', 'admin_sort.id')->select('status_labels.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order); + } } diff --git a/app/Presenters/StatusLabelPresenter.php b/app/Presenters/StatusLabelPresenter.php new file mode 100644 index 0000000000..2e43400041 --- /dev/null +++ b/app/Presenters/StatusLabelPresenter.php @@ -0,0 +1,115 @@ + 'id', + 'searchable' => false, + 'sortable' => true, + 'switchable' => true, + 'title' => trans('general.id'), + 'visible' => false, + ], [ + 'field' => 'name', + 'searchable' => true, + 'sortable' => true, + 'switchable' => false, + 'title' => trans('general.name'), + 'visible' => true, + 'formatter' => 'statuslabelsAssetLinkFormatter', + ],[ + 'field' => 'type', + 'searchable' => false, + 'sortable' => false, + 'switchable' => false, + 'title' => trans('admin/statuslabels/table.status_type'), + 'visible' => true, + 'formatter' => 'statusLabelTypeFormatter', + ], [ + 'field' => 'assets_count', + 'searchable' => false, + 'sortable' => true, + 'switchable' => false, + 'title' => trans('general.assets'), + 'visible' => true, + ], [ + 'field' => 'color', + 'searchable' => false, + 'sortable' => true, + 'switchable' => true, + 'title' => trans('admin/statuslabels/table.color'), + 'visible' => true, + 'formatter' => 'colorSqFormatter', + ], [ + 'field' => 'show_in_nav', + 'searchable' => false, + 'sortable' => true, + 'switchable' => true, + 'title' => trans('admin/statuslabels/table.show_in_nav'), + 'visible' => true, + 'formatter' => 'trueFalseFormatter', + ], [ + 'field' => 'default_label', + 'searchable' => false, + 'sortable' => true, + 'switchable' => true, + 'title' => trans('admin/statuslabels/table.default_label'), + 'visible' => true, + 'formatter' => 'trueFalseFormatter', + ],[ + 'field' => 'notes', + 'searchable' => true, + 'sortable' => true, + 'switchable' => true, + 'title' => trans('general.notes'), + 'visible' => false, + ], [ + 'field' => 'created_by', + 'searchable' => false, + 'sortable' => true, + 'title' => trans('general.created_by'), + 'visible' => false, + 'formatter' => 'usersLinkObjFormatter', + ], [ + 'field' => 'created_at', + 'searchable' => true, + 'sortable' => true, + 'switchable' => true, + 'title' => trans('general.created_at'), + 'visible' => false, + 'formatter' => 'dateDisplayFormatter', + ], [ + 'field' => 'updated_at', + 'searchable' => true, + 'sortable' => true, + 'switchable' => true, + 'title' => trans('general.updated_at'), + 'visible' => false, + 'formatter' => 'dateDisplayFormatter', + ], [ + 'field' => 'actions', + 'searchable' => false, + 'sortable' => false, + 'switchable' => false, + 'title' => trans('table.actions'), + 'formatter' => 'statuslabelsActionsFormatter', + ], + ]; + + return json_encode($layout); + } + + +} diff --git a/resources/views/statuslabels/index.blade.php b/resources/views/statuslabels/index.blade.php index 7fdf4fc9a5..b5ded48234 100755 --- a/resources/views/statuslabels/index.blade.php +++ b/resources/views/statuslabels/index.blade.php @@ -25,6 +25,7 @@
{{ trans('general.id') }} | -{{ trans('admin/statuslabels/table.name') }} | -{{ trans('admin/statuslabels/table.status_type') }} | -{{ trans('general.assets') }} | -{{ trans('admin/statuslabels/table.color') }} | -{{ trans('admin/statuslabels/table.show_in_nav') }} | -{{ trans('admin/statuslabels/table.default_label') }} | -{{ trans('general.notes') }} | -{{ trans('table.actions') }} | -
---|