From 875d0dd3433b17c77864ffebe2c4f6bd68359a4c Mon Sep 17 00:00:00 2001 From: snipe Date: Sat, 11 Mar 2017 04:26:01 -0800 Subject: [PATCH] Added advanced search to assets --- app/Http/Controllers/Api/AssetsController.php | 14 +- app/Http/Transformers/AssetsTransformer.php | 2 +- app/Models/Asset.php | 136 ++++++++++++++++++ app/Presenters/AssetPresenter.php | 12 +- resources/views/hardware/index.blade.php | 2 + .../views/partials/bootstrap-table.blade.php | 4 + 6 files changed, 158 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index ce45246f25..9ac086376d 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -72,6 +72,12 @@ class AssetsController extends Controller 'purchase_cost' ]; + $filter = array(); + if ($request->has('filter')) { + $filter = json_decode($request->input('filter')); + } + + $all_custom_fields = CustomField::all(); //used as a 'cache' of custom fields throughout this page load foreach ($all_custom_fields as $field) { $allowed_columns[]=$field->db_column_name(); @@ -81,8 +87,14 @@ class AssetsController extends Controller 'assetLoc', 'assetstatus', 'defaultLoc', 'assetlog', 'company', 'model.category', 'model.manufacturer', 'model.fieldset', 'assigneduser'); - if ($request->has('search')) { + // If we should search on everything + if (($request->has('search')) && (count($filter) == 0)) { $assets->TextSearch($request->input('search')); + // otherwise loop through the filters and search strictly on them + } else { + if (count($filter) > 0) { + $assets->ByFilter($filter); + } } if ($request->has('status_id')) { diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index 9d1a299ca7..826b3751d2 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -30,7 +30,7 @@ class AssetsTransformer 'asset_tag' => e($asset->asset_tag), 'serial' => e($asset->serial), 'model' => ($asset->model) ? ['id' => $asset->model->id,'name'=> e($asset->model->name)] : '', - 'model_number' => ($asset->model_number) ? e($asset->model_number) : null, + 'model_number' => ($asset->model) ? e($asset->model->model_number) : null, 'status_label' => ($asset->assetstatus) ? ['id' => $asset->assetstatus->id,'name'=> e($asset->assetstatus->name)] : null, 'category' => ($asset->model->category) ? ['id' => $asset->model->category->id,'name'=> e($asset->model->category->name)] : null, 'manufacturer' => ($asset->model->manufacturer) ? ['id' => $asset->model->manufacturer->id,'name'=> e($asset->model->manufacturer->name)] : null, diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 2e653a145c..961e6fa6ad 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -775,6 +775,142 @@ class Asset extends Depreciable }); } + + + /** + * Query builder scope to search on text filters for complex Bootstrap Tables API + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param text $filter JSON array of search keys and terms + * + * @return \Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeByFilter($query, $filter) + { + + return $query->where(function ($query) use ($filter) { + foreach ($filter as $key => $search_val) { + \Log::debug('filter '.$key.' for '.$search_val); + + if ($key =='asset_tag') { + \Log::debug('search on tag for '.$search_val); + $query->where('assets.asset_tag', 'LIKE', '%'.$search_val.'%'); + } + + if ($key =='name') { + \Log::debug('search on name for '.$search_val); + $query->where('assets.name', 'LIKE', '%'.$search_val.'%'); + } + + if ($key =='product_key') { + \Log::debug('search on key for '.$search_val); + $query->where('assets.serial', 'LIKE', '%'.$search_val.'%'); + } + + if ($key =='purchase_date') { + $query->where('assets.purchase_date', 'LIKE', '%'.$search_val.'%'); + } + + if ($key =='purchase_cost') { + $query->where('assets.purchase_cost', 'LIKE', '%'.$search_val.'%'); + } + + if ($key =='notes') { + $query->where('assets.notes', 'LIKE', '%'.$search_val.'%'); + } + + if ($key =='order_number') { + \Log::debug('search on order number for '.$search_val); + $query->where('assets.order_number', 'LIKE', '%'.$search_val.'%'); + } + + if ($key =='status_label') { + $query->whereHas('assetstatus', function ($query) use ($search_val) { + $query->where('status_labels.name', 'LIKE', '%' . $search_val . '%'); + }); + } + + if ($key =='location') { + $query->whereHas('defaultLoc', function ($query) use ($search_val) { + $query->where('locations.name', 'LIKE', '%' . $search_val . '%'); + }); + } + + if ($key =='checkedout_to') { + $query->whereHas('assigneduser', function ($query) use ($search) { + $query->where(function ($query) use ($search) { + $query->where('users.first_name', 'LIKE', '%' . $search . '%') + ->orWhere('users.last_name', 'LIKE', '%' . $search . '%'); + }); + }); + } + + + if ($key =='manufacturer') { + \Log::debug('search on manufacturer for '.$search_val); + $query->whereHas('model', function ($query) use ($search_val) { + $query->whereHas('manufacturer', function ($query) use ($search_val) { + $query->where(function ($query) use ($search_val) { + $query->where('manufacturers.name', 'LIKE', '%'.$search_val.'%'); + }); + }); + }); + } + + if ($key =='category') { + \Log::debug('search on model category for '.$search_val); + $query->whereHas('model', function ($query) use ($search) { + $query->whereHas('category', function ($query) use ($search) { + $query->where(function ($query) use ($search_val) { + $query->where('categories.name', 'LIKE', '%' . $search_val . '%') + ->orWhere('models.name', 'LIKE', '%' . $search_val . '%') + ->orWhere('models.model_number', 'LIKE', '%' . $search_val . '%'); + }); + }); + }); + } + + if ($key =='model') { + \Log::debug('search on model name for '.$search_val); + $query->where(function ($query) use ($search_val) { + $query->whereHas('model', function ($query) use ($search_val) { + $query->where('models.name', 'LIKE', '%' . $search_val . '%'); + }); + }); + } + + if ($key =='model_number') { + \Log::debug('search on model number for '.$search_val); + $query->where(function ($query) use ($search_val) { + $query->whereHas('model', function ($query) use ($search_val) { + $query->where('models.model_number', 'LIKE', '%' . $search_val . '%'); + }); + }); + } + + + if ($key =='company') { + \Log::debug('search on company for '.$search_val); + $query->where(function ($query) use ($search_val) { + $query->whereHas('company', function ($query) use ($search_val) { + $query->where('companies.name', 'LIKE', '%' . $search_val . '%'); + }); + }); + } + + + } + + foreach (CustomField::all() as $field) { + if (array_key_exists($field->db_column_name(), $filter)) { + $query->orWhere($field->db_column_name(), 'LIKE', "%$search_val%"); + } + } + + }); + } + + /** * Query builder scope to order on model * diff --git a/app/Presenters/AssetPresenter.php b/app/Presenters/AssetPresenter.php index fe49543b5f..d772f81cbe 100644 --- a/app/Presenters/AssetPresenter.php +++ b/app/Presenters/AssetPresenter.php @@ -77,8 +77,7 @@ class AssetPresenter extends Presenter "searchable" => true, "sortable" => true, "title" => trans('admin/models/table.modelnumber'), - "visible" => false, - "formatter" => null + "visible" => false ], [ "field" => "category", "searchable" => true, @@ -107,14 +106,7 @@ class AssetPresenter extends Presenter "title" => trans('admin/hardware/table.location'), "visible" => true, "formatter" => "locationsLinkObjFormatter" - ], [ - "field" => "category", - "searchable" => true, - "sortable" => true, - "title" => trans('general.category'), - "visible" => true, - "formatter" => "categoriesLinkObjFormatter" - ], [ + ], [ "field" => "manufacturer", "searchable" => true, "sortable" => true, diff --git a/resources/views/hardware/index.blade.php b/resources/views/hardware/index.blade.php index 5d094ba38f..969195a765 100755 --- a/resources/views/hardware/index.blade.php +++ b/resources/views/hardware/index.blade.php @@ -74,6 +74,8 @@ data-toolbar="#toolbar" class="table table-striped snipe-table" id="table" + data-advanced-search="true" + data-id-table="advancedTable" data-url="{{ route('api.assets.index', array('status' => e(Input::get('status')), 'order_number'=>e(Input::get('order_number')), diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php index fe6fa39db7..5207bd1bd1 100644 --- a/resources/views/partials/bootstrap-table.blade.php +++ b/resources/views/partials/bootstrap-table.blade.php @@ -8,6 +8,7 @@ +