Skip to content

Commit

Permalink
Merge pull request #990 from madd15/patch-4
Browse files Browse the repository at this point in the history
Table search experiments
  • Loading branch information
snipe committed Jul 30, 2015
2 parents 43af451 + 032d795 commit 8bedaf7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
28 changes: 22 additions & 6 deletions app/controllers/admin/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1089,18 +1089,35 @@ public function getDatatable($status = null)
$assets->where('order_number','=',e(Input::get('order_number')));
}

$assets = $assets->orderBy('asset_tag', 'ASC');

if (Input::has('search')) {
$assets = $assets->TextSearch(Input::get('search'));
}

if (Input::has('sort') && Input::has('order')){
$order = Input::get('order');
$sort = Input::get('sort');

switch ($sort)
{
case 'model':
$assets = $assets->OrderModels($order);
break;
default:
$assets = $assets->orderBy($sort, $order);
break;
}
} else {
$assets = $assets->orderBy('asset_tag', 'ASC');
}

$assetCount = $assets->count();
$assets = $assets->skip(Input::get('offset'))->take(Input::get('limit'))->get();


$rows = array();
$i=0;
foreach ($assets as $asset) {
$inout = '';
$actions = '';
if ($asset->deleted_at=='') {
$actions = '<div style=" white-space: nowrap;"><a href="'.route('clone/hardware', $asset->id).'" class="btn btn-info btn-sm" title="Clone asset"><i class="fa fa-files-o"></i></a> <a href="'.route('update/hardware', $asset->id).'" class="btn btn-warning btn-sm"><i class="fa fa-pencil icon-white"></i></a> <a data-html="false" class="btn delete-asset btn-danger btn-sm" data-toggle="modal" href="'.route('delete/hardware', $asset->id).'" data-content="'.Lang::get('admin/hardware/message.delete.confirm').'" data-title="'.Lang::get('general.delete').' '.htmlspecialchars($asset->asset_tag).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></div>';
} elseif ($asset->model->deleted_at=='') {
Expand Down Expand Up @@ -1130,10 +1147,9 @@ public function getDatatable($status = null)
'notes' => $asset->notes,
'order' => ($asset->order_number) ? '<a href="../hardware/?order_number='.$asset->order_number.'">'.$asset->order_number.'</a>' : '',
'checkout_date' => (($asset->assigned_to!='')&&($asset->assetlog->first())) ? $asset->assetlog->first()->created_at->format('Y-m-d') : '',
'change' => $inout,
'actions' => $actions
'change' => ($inout) ? $inout : '',
'actions' => ($actions) ? $actions : ''
);
$i++;
}

$data = array('total'=>$assetCount, 'rows'=>$rows);
Expand Down
57 changes: 44 additions & 13 deletions app/models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,7 @@ public function scopeDeleted($query)
{
return $query->whereNotNull('deleted_at');
}



/**
* Query builder scope to search on text
*
Expand All @@ -402,18 +401,50 @@ public function scopeDeleted($query)
* @return Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeTextSearch($query, $search)
{


return $query->where(function($query) use ($search)
{
{
$search = explode('+', $search);

$query->where('name','LIKE','%'.$search.'%')
return $query->where(function($query) use ($search)
{
foreach ($search as $search) {
$query->whereHas('model', function($query) use ($search) {
$query->join('categories','models.category_id','=','categories.id')
->where('categories.name','LIKE','%'.$search.'%')
->orWhere('models.name','LIKE','%'.$search.'%');
})->orWhere(function($query) use ($search) {
$query->whereHas('assetstatus', function($query) use ($search) {
$query->where('name','LIKE','%'.$search.'%');
});
})->orWhere(function($query) use ($search) {
$query->whereHas('defaultLoc', function($query) use ($search) {
$query->where('name','LIKE','%'.$search.'%');
});
})->orWhere(function($query) use ($search) {
$query->whereHas('assigneduser', function($query) use ($search) {
$query->where('first_name','LIKE','%'.$search.'%')
->orWhere('last_name','LIKE','%'.$search.'%');
});
})->orWhere('name','LIKE','%'.$search.'%')
->orWhere('asset_tag','LIKE','%'.$search.'%')
->orWhere('serial','LIKE','%'.$search.'%');
});

}

->orWhere('serial','LIKE','%'.$search.'%')
->orWhere('order_number','LIKE','%'.$search.'%');
}
});
}

/**
* Query builder scope to order on model
*
* @param Illuminate\Database\Query\Builder $query Query builder instance
* @param text $order Order
*
* @return Illuminate\Database\Query\Builder Modified query builder
*/
public function scopeOrderModels($query, $order)
{
return $query->where(function($query) use ($order)
{
$query->join('models', 'assets.model_id', '=', 'models.id')->orderBy('models.name', $order);
});
}
}

0 comments on commit 8bedaf7

Please sign in to comment.