From 5e1c736d4982b1e7c9bf15f848210d5e2fcedb2d Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 19 Sep 2024 18:04:50 +0100 Subject: [PATCH] Added created_by for manufacturers Signed-off-by: snipe --- .../Api/ManufacturersController.php | 53 ++++++++++++++++--- .../Transformers/ManufacturersTransformer.php | 4 ++ app/Models/Manufacturer.php | 15 ++++++ app/Presenters/ManufacturerPresenter.php | 19 +++++-- 4 files changed, 81 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Api/ManufacturersController.php b/app/Http/Controllers/Api/ManufacturersController.php index 48dd12b313..f111ef6c83 100644 --- a/app/Http/Controllers/Api/ManufacturersController.php +++ b/app/Http/Controllers/Api/ManufacturersController.php @@ -25,11 +25,42 @@ class ManufacturersController extends Controller public function index(Request $request) : JsonResponse | array { $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( - ['id', 'name', 'url', 'support_url', 'warranty_lookup_url', 'support_email', 'support_phone', 'created_at', 'updated_at', 'image', 'deleted_at'] - )->withCount('assets as assets_count')->withCount('licenses as licenses_count')->withCount('consumables as consumables_count')->withCount('accessories as accessories_count'); + $manufacturers = Manufacturer::select([ + 'id', + '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') { $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 $offset = ($request->input('offset') > $manufacturers->count()) ? $manufacturers->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'; - $manufacturers->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': + $manufacturers = $manufacturers->OrderByCreatedBy($order); + break; + default: + $manufacturers = $manufacturers->orderBy($column_sort, $order); + break; + } $total = $manufacturers->count(); $manufacturers = $manufacturers->skip($offset)->take($limit)->get(); diff --git a/app/Http/Transformers/ManufacturersTransformer.php b/app/Http/Transformers/ManufacturersTransformer.php index 9c84fd50fe..e08aaa7436 100644 --- a/app/Http/Transformers/ManufacturersTransformer.php +++ b/app/Http/Transformers/ManufacturersTransformer.php @@ -36,6 +36,10 @@ class ManufacturersTransformer 'licenses_count' => (int) $manufacturer->licenses_count, 'consumables_count' => (int) $manufacturer->consumables_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'), 'updated_at' => Helper::getFormattedDateObject($manufacturer->updated_at, 'datetime'), 'deleted_at' => Helper::getFormattedDateObject($manufacturer->deleted_at, 'datetime'), diff --git a/app/Models/Manufacturer.php b/app/Models/Manufacturer.php index 85907f7ddb..6fe7426514 100755 --- a/app/Models/Manufacturer.php +++ b/app/Models/Manufacturer.php @@ -105,4 +105,19 @@ class Manufacturer extends SnipeModel { 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); + } } diff --git a/app/Presenters/ManufacturerPresenter.php b/app/Presenters/ManufacturerPresenter.php index 07a22c9ea4..ea29974f34 100644 --- a/app/Presenters/ManufacturerPresenter.php +++ b/app/Presenters/ManufacturerPresenter.php @@ -126,6 +126,13 @@ class ManufacturerPresenter extends Presenter 'class' => 'css-accessory', ], [ + 'field' => 'created_by', + 'searchable' => false, + 'sortable' => true, + 'title' => trans('general.created_by'), + 'visible' => false, + 'formatter' => 'usersLinkObjFormatter', + ], [ 'field' => 'created_at', 'searchable' => true, 'sortable' => true, @@ -133,9 +140,15 @@ class ManufacturerPresenter extends Presenter '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,