mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-11 22:07:29 -08:00
Added advanced search to assets
This commit is contained in:
parent
119e0772c1
commit
875d0dd343
|
@ -72,6 +72,12 @@ class AssetsController extends Controller
|
||||||
'purchase_cost'
|
'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
|
$all_custom_fields = CustomField::all(); //used as a 'cache' of custom fields throughout this page load
|
||||||
foreach ($all_custom_fields as $field) {
|
foreach ($all_custom_fields as $field) {
|
||||||
$allowed_columns[]=$field->db_column_name();
|
$allowed_columns[]=$field->db_column_name();
|
||||||
|
@ -81,8 +87,14 @@ class AssetsController extends Controller
|
||||||
'assetLoc', 'assetstatus', 'defaultLoc', 'assetlog', 'company',
|
'assetLoc', 'assetstatus', 'defaultLoc', 'assetlog', 'company',
|
||||||
'model.category', 'model.manufacturer', 'model.fieldset', 'assigneduser');
|
'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'));
|
$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')) {
|
if ($request->has('status_id')) {
|
||||||
|
|
|
@ -30,7 +30,7 @@ class AssetsTransformer
|
||||||
'asset_tag' => e($asset->asset_tag),
|
'asset_tag' => e($asset->asset_tag),
|
||||||
'serial' => e($asset->serial),
|
'serial' => e($asset->serial),
|
||||||
'model' => ($asset->model) ? ['id' => $asset->model->id,'name'=> e($asset->model->name)] : '',
|
'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,
|
'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,
|
'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,
|
'manufacturer' => ($asset->model->manufacturer) ? ['id' => $asset->model->manufacturer->id,'name'=> e($asset->model->manufacturer->name)] : null,
|
||||||
|
|
|
@ -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
|
* Query builder scope to order on model
|
||||||
*
|
*
|
||||||
|
|
|
@ -77,8 +77,7 @@ class AssetPresenter extends Presenter
|
||||||
"searchable" => true,
|
"searchable" => true,
|
||||||
"sortable" => true,
|
"sortable" => true,
|
||||||
"title" => trans('admin/models/table.modelnumber'),
|
"title" => trans('admin/models/table.modelnumber'),
|
||||||
"visible" => false,
|
"visible" => false
|
||||||
"formatter" => null
|
|
||||||
], [
|
], [
|
||||||
"field" => "category",
|
"field" => "category",
|
||||||
"searchable" => true,
|
"searchable" => true,
|
||||||
|
@ -107,14 +106,7 @@ class AssetPresenter extends Presenter
|
||||||
"title" => trans('admin/hardware/table.location'),
|
"title" => trans('admin/hardware/table.location'),
|
||||||
"visible" => true,
|
"visible" => true,
|
||||||
"formatter" => "locationsLinkObjFormatter"
|
"formatter" => "locationsLinkObjFormatter"
|
||||||
], [
|
], [
|
||||||
"field" => "category",
|
|
||||||
"searchable" => true,
|
|
||||||
"sortable" => true,
|
|
||||||
"title" => trans('general.category'),
|
|
||||||
"visible" => true,
|
|
||||||
"formatter" => "categoriesLinkObjFormatter"
|
|
||||||
], [
|
|
||||||
"field" => "manufacturer",
|
"field" => "manufacturer",
|
||||||
"searchable" => true,
|
"searchable" => true,
|
||||||
"sortable" => true,
|
"sortable" => true,
|
||||||
|
|
|
@ -74,6 +74,8 @@
|
||||||
data-toolbar="#toolbar"
|
data-toolbar="#toolbar"
|
||||||
class="table table-striped snipe-table"
|
class="table table-striped snipe-table"
|
||||||
id="table"
|
id="table"
|
||||||
|
data-advanced-search="true"
|
||||||
|
data-id-table="advancedTable"
|
||||||
data-url="{{ route('api.assets.index',
|
data-url="{{ route('api.assets.index',
|
||||||
array('status' => e(Input::get('status')),
|
array('status' => e(Input::get('status')),
|
||||||
'order_number'=>e(Input::get('order_number')),
|
'order_number'=>e(Input::get('order_number')),
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
<script src="{{ asset('assets/js/jspdf.min.js') }}"></script>
|
<script src="{{ asset('assets/js/jspdf.min.js') }}"></script>
|
||||||
<script src="{{ asset('assets/js/jspdf.plugin.autotable.js') }}"></script>
|
<script src="{{ asset('assets/js/jspdf.plugin.autotable.js') }}"></script>
|
||||||
<script src="{{ asset('assets/js/extensions/export/jquery.base64.js') }}"></script>
|
<script src="{{ asset('assets/js/extensions/export/jquery.base64.js') }}"></script>
|
||||||
|
<script src="{{ asset('assets/js/extensions/toolbar/bootstrap-table-toolbar.js') }}"></script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
$('.snipe-table').bootstrapTable({
|
$('.snipe-table').bootstrapTable({
|
||||||
|
@ -54,6 +55,8 @@ $('.snipe-table').bootstrapTable({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
maintainSelected: true,
|
maintainSelected: true,
|
||||||
paginationFirstText: "{{ trans('general.first') }}",
|
paginationFirstText: "{{ trans('general.first') }}",
|
||||||
paginationLastText: "{{ trans('general.last') }}",
|
paginationLastText: "{{ trans('general.last') }}",
|
||||||
|
@ -64,6 +67,7 @@ $('.snipe-table').bootstrapTable({
|
||||||
},
|
},
|
||||||
pageList: ['10','25','50','100','150','200','500','1000'],
|
pageList: ['10','25','50','100','150','200','500','1000'],
|
||||||
icons: {
|
icons: {
|
||||||
|
advancedSearchIcon: 'fa fa-search-plus',
|
||||||
paginationSwitchDown: 'fa-caret-square-o-down',
|
paginationSwitchDown: 'fa-caret-square-o-down',
|
||||||
paginationSwitchUp: 'fa-caret-square-o-up',
|
paginationSwitchUp: 'fa-caret-square-o-up',
|
||||||
columns: 'fa-columns',
|
columns: 'fa-columns',
|
||||||
|
|
Loading…
Reference in a new issue