Added created_by to status label

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-09-19 18:22:11 +01:00
parent 5e1c736d49
commit 727806372c
5 changed files with 151 additions and 18 deletions

View file

@ -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();

View file

@ -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'),
];

View file

@ -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);
}
}

View file

@ -0,0 +1,115 @@
<?php
namespace App\Presenters;
/**
* Class StatusLabelPresenter
*/
class StatusLabelPresenter extends Presenter
{
/**
* Json Column Layout for bootstrap table
* @return string
*/
public static function dataTableLayout()
{
$layout = [
[
'field' => '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);
}
}

View file

@ -25,6 +25,7 @@
<div class="table-responsive">
<table
data-columns="{{ \App\Presenters\StatusLabelPresenter::dataTableLayout() }}"
data-cookie-id-table="statuslabelsTable"
data-pagination="true"
data-id-table="statuslabelsTable"
@ -44,19 +45,6 @@
"fileName": "export-statuslabels-{{ date('Y-m-d') }}",
"ignoreColumn": ["actions","image","change","checkbox","checkincheckout","icon"]
}'>
<thead>
<tr>
<th data-sortable="true" data-field="id" data-visible="false">{{ trans('general.id') }}</th>
<th data-sortable="true" data-field="name" data-formatter="statuslabelsAssetLinkFormatter">{{ trans('admin/statuslabels/table.name') }}</th>
<th data-sortable="false" data-field="type" data-formatter="statusLabelTypeFormatter">{{ trans('admin/statuslabels/table.status_type') }}</th>
<th data-sortable="true" data-field="assets_count">{{ trans('general.assets') }}</th>
<th data-sortable="true" data-field="color" data-formatter="colorSqFormatter">{{ trans('admin/statuslabels/table.color') }}</th>
<th class="text-center" data-sortable="true" data-field="show_in_nav" data-formatter="trueFalseFormatter">{{ trans('admin/statuslabels/table.show_in_nav') }}</th>
<th class="text-center" data-sortable="true" data-field="default_label" data-formatter="trueFalseFormatter">{{ trans('admin/statuslabels/table.default_label') }}</th>
<th data-sortable="true" data-field="notes" data-visible="false">{{ trans('general.notes') }}</th>
<th data-formatter="statuslabelsActionsFormatter" data-searchable="false" data-sortable="false" data-field="actions">{{ trans('table.actions') }}</th>
</tr>
</thead>
</table>
</div>
</div>