Added created_by for manufacturers

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-09-19 18:04:50 +01:00
parent 6579fa72da
commit 5e1c736d49
4 changed files with 81 additions and 10 deletions

View file

@ -25,11 +25,42 @@ class ManufacturersController extends Controller
public function index(Request $request) : JsonResponse | array public function index(Request $request) : JsonResponse | array
{ {
$this->authorize('view', Manufacturer::class); $this->authorize('view', Manufacturer::class);
$allowed_columns = ['id', 'name', 'url', 'support_url', 'support_email', 'warranty_lookup_url', 'support_phone', 'created_at', 'updated_at', 'image', 'assets_count', 'consumables_count', 'components_count', 'licenses_count']; $allowed_columns = [
'id',
'name',
'url',
'support_url',
'support_email',
'warranty_lookup_url',
'support_phone',
'created_at',
'updated_at',
'image',
'assets_count',
'consumables_count',
'components_count',
'licenses_count'
];
$manufacturers = Manufacturer::select( $manufacturers = Manufacturer::select([
['id', 'name', 'url', 'support_url', 'warranty_lookup_url', 'support_email', 'support_phone', 'created_at', 'updated_at', 'image', 'deleted_at'] 'id',
)->withCount('assets as assets_count')->withCount('licenses as licenses_count')->withCount('consumables as consumables_count')->withCount('accessories as accessories_count'); 'name',
'url',
'support_url',
'warranty_lookup_url',
'support_email',
'support_phone',
'created_by',
'created_at',
'updated_at',
'image',
'deleted_at',
])
->with('adminuser')
->withCount('assets as assets_count')
->withCount('licenses as licenses_count')
->withCount('consumables as consumables_count')
->withCount('accessories as accessories_count');
if ($request->input('deleted') == 'true') { if ($request->input('deleted') == 'true') {
$manufacturers->onlyTrashed(); $manufacturers->onlyTrashed();
@ -66,10 +97,18 @@ class ManufacturersController extends Controller
// Make sure the offset and limit are actually integers and do not exceed system limits // Make sure the offset and limit are actually integers and do not exceed system limits
$offset = ($request->input('offset') > $manufacturers->count()) ? $manufacturers->count() : app('api_offset_value'); $offset = ($request->input('offset') > $manufacturers->count()) ? $manufacturers->count() : app('api_offset_value');
$limit = app('api_limit_value'); $limit = app('api_limit_value');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; $sort_override = $request->input('sort');
$manufacturers->orderBy($sort, $order); $column_sort = in_array($sort_override, $allowed_columns) ? $sort_override : 'created_at';
switch ($sort_override) {
case 'created_by':
$manufacturers = $manufacturers->OrderByCreatedBy($order);
break;
default:
$manufacturers = $manufacturers->orderBy($column_sort, $order);
break;
}
$total = $manufacturers->count(); $total = $manufacturers->count();
$manufacturers = $manufacturers->skip($offset)->take($limit)->get(); $manufacturers = $manufacturers->skip($offset)->take($limit)->get();

View file

@ -36,6 +36,10 @@ class ManufacturersTransformer
'licenses_count' => (int) $manufacturer->licenses_count, 'licenses_count' => (int) $manufacturer->licenses_count,
'consumables_count' => (int) $manufacturer->consumables_count, 'consumables_count' => (int) $manufacturer->consumables_count,
'accessories_count' => (int) $manufacturer->accessories_count, 'accessories_count' => (int) $manufacturer->accessories_count,
'created_by' => ($manufacturer->adminuser) ? [
'id' => (int) $manufacturer->adminuser->id,
'name'=> e($manufacturer->adminuser->present()->fullName()),
] : null,
'created_at' => Helper::getFormattedDateObject($manufacturer->created_at, 'datetime'), 'created_at' => Helper::getFormattedDateObject($manufacturer->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($manufacturer->updated_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($manufacturer->updated_at, 'datetime'),
'deleted_at' => Helper::getFormattedDateObject($manufacturer->deleted_at, 'datetime'), 'deleted_at' => Helper::getFormattedDateObject($manufacturer->deleted_at, 'datetime'),

View file

@ -105,4 +105,19 @@ class Manufacturer extends SnipeModel
{ {
return $this->hasMany(\App\Models\Consumable::class, 'manufacturer_id'); return $this->hasMany(\App\Models\Consumable::class, 'manufacturer_id');
} }
public function adminuser()
{
return $this->belongsTo(\App\Models\User::class, 'created_by');
}
/**
* Query builder scope to order on the user that created it
*/
public function scopeOrderByCreatedBy($query, $order)
{
return $query->leftJoin('users as admin_sort', 'manufacturers.created_by', '=', 'admin_sort.id')->select('manufacturers.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
}
} }

View file

@ -126,6 +126,13 @@ class ManufacturerPresenter extends Presenter
'class' => 'css-accessory', 'class' => 'css-accessory',
], ],
[ [
'field' => 'created_by',
'searchable' => false,
'sortable' => true,
'title' => trans('general.created_by'),
'visible' => false,
'formatter' => 'usersLinkObjFormatter',
], [
'field' => 'created_at', 'field' => 'created_at',
'searchable' => true, 'searchable' => true,
'sortable' => true, 'sortable' => true,
@ -133,9 +140,15 @@ class ManufacturerPresenter extends Presenter
'title' => trans('general.created_at'), 'title' => trans('general.created_at'),
'visible' => false, 'visible' => false,
'formatter' => 'dateDisplayFormatter', 'formatter' => 'dateDisplayFormatter',
], ], [
'field' => 'updated_at',
[ 'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.updated_at'),
'visible' => false,
'formatter' => 'dateDisplayFormatter',
], [
'field' => 'actions', 'field' => 'actions',
'searchable' => false, 'searchable' => false,
'sortable' => false, 'sortable' => false,