diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index f070d938c9..1a7354a3e6 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -27,9 +27,23 @@ class AccessoriesController extends Controller public function index(Request $request) { $this->authorize('view', Accessory::class); - $allowed_columns = ['id','name','model_number','eol','notes','created_at','min_amt','company_id']; + + // This array is what determines which fields should be allowed to be sorted on ON the table itself, no relations + // Relations will be handled in query scopes a little further down. + $allowed_columns = + [ + 'id', + 'name', + 'model_number', + 'eol', + 'notes', + 'created_at', + 'min_amt', + 'company_id' + ]; - $accessories = Accessory::with('category', 'company', 'manufacturer', 'users', 'location'); + + $accessories = Accessory::with('category', 'company', 'manufacturer', 'users', 'location', 'supplier'); if ($request->filled('search')) { $accessories = $accessories->TextSearch($request->input('search')); @@ -62,24 +76,32 @@ class AccessoriesController extends Controller // Check to make sure the limit is not higher than the max allowed ((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results'); - $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'); + $column_sort = in_array($sort_override, $allowed_columns) ? $sort_override : 'created_at'; - switch ($sort) { + switch ($sort_override) { case 'category': $accessories = $accessories->OrderCategory($order); break; case 'company': $accessories = $accessories->OrderCompany($order); break; + case 'location': + $accessories = $accessories->OrderLocation($order); + break; + case 'manufacturer': + $accessories = $accessories->OrderManufacturer($order); + break; + case 'supplier': + $accessories = $accessories->OrderSupplier($order); + break; default: - $accessories = $accessories->orderBy($sort, $order); + $accessories = $accessories->orderBy($column_sort, $order); break; } - $accessories->orderBy($sort, $order); - + $total = $accessories->count(); $accessories = $accessories->skip($offset)->take($limit)->get(); return (new AccessoriesTransformer)->transformAccessories($accessories, $total); diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php index 6bacee9df7..ccd23570ed 100644 --- a/app/Http/Controllers/Api/ComponentsController.php +++ b/app/Http/Controllers/Api/ComponentsController.php @@ -26,8 +26,25 @@ class ComponentsController extends Controller public function index(Request $request) { $this->authorize('view', Component::class); + + // This array is what determines which fields should be allowed to be sorted on ON the table itself, no relations + // Relations will be handled in query scopes a little further down. + $allowed_columns = + [ + 'id', + 'name', + 'min_amt', + 'order_number', + 'serial', + 'purchase_date', + 'purchase_cost', + 'qty', + 'image', + ]; + + $components = Company::scopeCompanyables(Component::select('components.*') - ->with('company', 'location', 'category')); + ->with('company', 'location', 'category', 'assets')); if ($request->filled('search')) { $components = $components->TextSearch($request->input('search')); @@ -52,11 +69,12 @@ class ComponentsController extends Controller // Check to make sure the limit is not higher than the max allowed ((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results'); - $allowed_columns = ['id','name','min_amt','order_number','serial','purchase_date','purchase_cost','company','category','qty','location','image']; + $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'); + $column_sort = in_array($sort_override, $allowed_columns) ? $sort_override : 'created_at'; - switch ($sort) { + switch ($sort_override) { case 'category': $components = $components->OrderCategory($order); break; @@ -67,7 +85,7 @@ class ComponentsController extends Controller $components = $components->OrderCompany($order); break; default: - $components = $components->orderBy($sort, $order); + $components = $components->orderBy($column_sort, $order); break; } diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php index f4c9da3df8..913bc1bd51 100644 --- a/app/Http/Controllers/Api/ConsumablesController.php +++ b/app/Http/Controllers/Api/ConsumablesController.php @@ -25,6 +25,26 @@ class ConsumablesController extends Controller public function index(Request $request) { $this->authorize('index', Consumable::class); + + // This array is what determines which fields should be allowed to be sorted on ON the table itself, no relations + // Relations will be handled in query scopes a little further down. + $allowed_columns = + [ + 'id', + 'name', + 'order_number', + 'min_amt', + 'purchase_date', + 'purchase_cost', + 'company', + 'category', + 'model_number', + 'item_no', + 'qty', + 'image', + ]; + + $consumables = Company::scopeCompanyables( Consumable::select('consumables.*') ->with('company', 'location', 'category', 'users', 'manufacturer') @@ -62,12 +82,14 @@ class ConsumablesController extends Controller // Check to make sure the limit is not higher than the max allowed ((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results'); - $allowed_columns = ['id','name','order_number','min_amt','purchase_date','purchase_cost','company','category','model_number', 'item_no', 'manufacturer','location','qty','image']; + $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'); + $column_sort = in_array($sort_override, $allowed_columns) ? $sort_override : 'created_at'; - switch ($sort) { + switch ($sort_override) { case 'category': $consumables = $consumables->OrderCategory($order); break; @@ -81,7 +103,7 @@ class ConsumablesController extends Controller $consumables = $consumables->OrderCompany($order); break; default: - $consumables = $consumables->orderBy($sort, $order); + $consumables = $consumables->orderBy($column_sort, $order); break; } diff --git a/app/Models/Accessory.php b/app/Models/Accessory.php index f05f9461ee..1abac4f68c 100755 --- a/app/Models/Accessory.php +++ b/app/Models/Accessory.php @@ -378,4 +378,17 @@ class Accessory extends SnipeModel { return $query->leftJoin('manufacturers', 'accessories.manufacturer_id', '=', 'manufacturers.id')->orderBy('manufacturers.name', $order); } + + /** + * Query builder scope to order on supplier + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param text $order Order + * + * @return \Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeOrderSupplier($query, $order) + { + return $query->leftJoin('suppliers', 'accessories.supplier_id', '=', 'suppliers.id')->orderBy('suppliers.name', $order); + } }