From b5b8865b36e7609d2c888bb7125bfefe75ce1cbf Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 21 Oct 2015 15:43:12 -0700 Subject: [PATCH] Locations in bootstrap tables for #1266 --- app/config/version.php | 4 +- app/controllers/admin/LocationsController.php | 73 ++++++++++++ app/models/Location.php | 28 ++++- app/routes.php | 16 ++- app/views/backend/locations/index.blade.php | 104 +++++++++++------- 5 files changed, 177 insertions(+), 48 deletions(-) diff --git a/app/config/version.php b/app/config/version.php index 064f7f6ef6..c5773f8794 100644 --- a/app/config/version.php +++ b/app/config/version.php @@ -1,5 +1,5 @@ 'v2.0-262', - 'hash_version' => 'v2.0-262-gcab5dba', + 'app_version' => 'v2.0-264', + 'hash_version' => 'v2.0-264-g36a3d65', ); \ No newline at end of file diff --git a/app/controllers/admin/LocationsController.php b/app/controllers/admin/LocationsController.php index 0b3c0b4aac..01f24afdee 100755 --- a/app/controllers/admin/LocationsController.php +++ b/app/controllers/admin/LocationsController.php @@ -254,6 +254,79 @@ class LocationsController extends AdminController } + public function getDatatable() + { + $locations = Location::select(array('id','name','address','address2','city','state','zip','country')) + ->whereNull('deleted_at'); + + if (Input::has('search')) { + $locations = $locations->TextSearch(e(Input::get('search'))); + } + + if (Input::has('offset')) { + $offset = e(Input::get('offset')); + } else { + $offset = 0; + } + + if (Input::has('limit')) { + $limit = e(Input::get('limit')); + } else { + $limit = 50; + } + + + $allowed_columns = ['name','address','city','state','country']; + $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; + $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at'; + + $locations->orderBy($sort, $order); + + $locationsCount = $locations->count(); + $locations = $locations->skip($offset)->take($limit)->get(); + + $rows = array(); + + foreach($locations as $location) { + $actions = ''; + + $rows[] = array( + 'name' => link_to('admin/locations/'.$location->id.'/view', $location->name), + 'parent' => ($location->parent) ? $location->parent->name : '', + 'assets' => ($location->assets->count() + $location->assignedassets->count()), + 'address' => ($location->address) ? $location->address: '', + 'city' => $location->city, + 'state' => $location->state, + 'country' => $location->country, + 'actions' => $actions + ); + } + + $data = array('total' => $locationsCount, 'rows' => $rows); + + return $data; + + } + + public function getDataView($locationId) + { + $consumable = Consumable::find($consumableID); + $consumable_users = $consumable->users; + $count = $consumable_users->count(); + + $rows = array(); + + foreach ($consumable_users as $user) { + $rows[] = array( + 'name' => link_to('/admin/users/'.$user->id.'/view', $user->fullName()) + ); + } + + $data = array('total' => $count, 'rows' => $rows); + + return $data; + } + } diff --git a/app/models/Location.php b/app/models/Location.php index 8a83eaa809..880ffe34aa 100755 --- a/app/models/Location.php +++ b/app/models/Location.php @@ -29,7 +29,7 @@ class Location extends Elegant } public function parent() { - return $this->belongsTo('Location', 'parent_id'); + return $this->hasOne('Location', 'parent_id'); } public function childLocations() { @@ -88,5 +88,31 @@ class Location extends Elegant return $location_options; } + /** + * Query builder scope to search on text + * + * @param Illuminate\Database\Query\Builder $query Query builder instance + * @param text $search Search term + * + * @return Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeTextsearch($query, $search) + { + + return $query->where('name', 'LIKE', "%$search%") + ->orWhere('address', 'LIKE', "%$search%") + ->orWhere('city', 'LIKE', "%$search%") + ->orWhere('state', 'LIKE', "%$search%") + ->orWhere('zip', 'LIKE', "%$search%") + ->orWhere(function($query) use ($search) { + $query->whereHas('parent', function($query) use ($search) { + $query->where(function($query) use ($search) { + $query->where('name','LIKE','%'.$search.'%'); + }); + }); + }); + + } + } diff --git a/app/routes.php b/app/routes.php index f901736f67..871e04df96 100755 --- a/app/routes.php +++ b/app/routes.php @@ -39,11 +39,17 @@ [ 'as' => 'api.accessories.view', 'uses' => 'AccessoriesController@getDataView' ] ); } ); - /*---Consumables API---*/ - Route::group(array('prefix'=>'consumables'), function () { - Route::get('list', array('as'=>'api.consumables.list', 'uses'=>'ConsumablesController@getDatatable')); - Route::get('{consumableID}/view', array('as'=>'api.consumables.view', 'uses'=>'ConsumablesController@getDataView')); - }); + /*---Consumables API---*/ + Route::group(array('prefix'=>'consumables'), function () { + Route::get('list', array('as'=>'api.consumables.list', 'uses'=>'ConsumablesController@getDatatable')); + Route::get('{consumableID}/view', array('as'=>'api.consumables.view', 'uses'=>'ConsumablesController@getDataView')); + }); + + /*---Locations API---*/ + Route::group(array('prefix'=>'locations'), function () { + Route::get('list', array('as'=>'api.locations.list', 'uses'=>'LocationsController@getDatatable')); + Route::get('{$locationID}/view', array('as'=>'api.locations.view', 'uses'=>'LocationsController@getDataView')); + }); /*---Users API---*/ Route::group( [ 'prefix' => 'users' ], function () { diff --git a/app/views/backend/locations/index.blade.php b/app/views/backend/locations/index.blade.php index f3d3d24fd4..332b41b0ad 100755 --- a/app/views/backend/locations/index.blade.php +++ b/app/views/backend/locations/index.blade.php @@ -18,47 +18,71 @@ Locations ::
- - - - - - - - - - - - - @foreach ($locations as $location) - - - - - - - - - @endforeach - -
@lang('admin/locations/table.name')@lang('admin/locations/table.parent')@lang('general.assets')@lang('admin/locations/table.address')@lang('admin/locations/table.city'), - @lang('admin/locations/table.state') - @lang('admin/locations/table.country')@lang('table.actions')
{{{ $location->name }}} - @if ($location->parent) - {{{ $location->parent->name }}} - @endif - {{{ ($location->assets->count() + $location->assignedassets->count()) }}}{{{ $location->address }}} - @if($location->address2 != '') - , {{{ $location->address2 }}} - @endif - {{{ $location->city }}}, {{{ strtoupper($location->state) }}} {{{ strtoupper($location->country) }}} - - - -
+ + + + + + + + + + + + + +
@lang('admin/locations/table.name')@lang('admin/locations/table.parent')@lang('general.assets')@lang('admin/locations/table.address')@lang('admin/locations/table.city') + + @lang('admin/locations/table.state') + + @lang('admin/locations/table.country'){{ Lang::get('table.actions') }}
+@section('moar_scripts') + + + + + + + +@stop @stop