From 47c70141c793fb147a6edc0e59f09afc53f9cd09 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 18 May 2016 19:08:16 -0700 Subject: [PATCH] Gross copypasta fix for #1932 --- app/Http/Controllers/CategoriesController.php | 226 ++++++++++++++---- app/Http/routes.php | 12 +- resources/views/categories/view.blade.php | 4 +- 3 files changed, 188 insertions(+), 54 deletions(-) diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php index f67562021b..ded9c66157 100755 --- a/app/Http/Controllers/CategoriesController.php +++ b/app/Http/Controllers/CategoriesController.php @@ -300,33 +300,10 @@ class CategoriesController extends Controller return $data; } - /** - * Returns JSON response that contains the data for the category detail page. - * - * @todo Refactor this nastiness. Assets do not behave - * the same as accessories, etc. Need to figure out if I should - * make separate controllers, or what. Too much copypasta if I try to work it in here. - * @author [A. Gianotto] [] - * @see CategoriesController::getView() method that generates the view - * @param int $categoryId - * @since [v1.8] - * @return String JSON - */ - public function getDataView($categoryId) - { - - $category = Category::find($categoryId); - - if ($category->category_type =='asset') { - $category_assets = $category->assets; - } elseif ($category->category_type =='accessory') { - $category_assets = $category->accessories; - } elseif ($category->category_type =='consumable') { - $category_assets = $category->consumables; - } elseif ($category->category_type =='component') { - $category_assets = $category->components; - } + public function getDataViewAssets($categoryID) { + $category = Category::with('assets.company')->find($categoryID); + $category_assets = $category->assets; if (Input::has('search')) { $category_assets = $category_assets->TextSearch(e(Input::get('search'))); @@ -354,22 +331,11 @@ class CategoriesController extends Controller foreach ($category_assets as $asset) { - if ($category->category_type=='asset') { - $category_type = 'hardware'; - } else { - $category_type = $category->category_type; - } - $actions = ''; $inout=''; if ($asset->deleted_at=='') { - $actions = '
'; - if ($category_type != 'component') { - $actions .= ' '; - } - $actions .= ' '; - $actions .= '
'; + $actions = '
'; } elseif ($asset->deleted_at!='') { $actions = ''; } @@ -377,27 +343,189 @@ class CategoriesController extends Controller if ($asset->assetstatus) { if ($asset->assetstatus->deployable != 0) { if (($asset->assigned_to !='') && ($asset->assigned_to > 0)) { - $inout = ''.trans('general.checkin').''; + $inout = ''.Lang::get('general.checkin').''; } else { - $inout = ''.trans('general.checkout').''; + $inout = ''.Lang::get('general.checkout').''; } } } $rows[] = array( - 'id' => $asset->id, - 'name' => (string)link_to('/hardware/'.$asset->id.'/view', e($asset->name)), - //'model' => $asset->model->name, - 'asset_tag' => e($asset->asset_tag), - 'serial' => e($asset->serial), - 'assigned_to' => ($asset->assigneduser) ? (string)link_to(config('app.url').'/admin/users/'.$asset->assigneduser->id.'/view', $asset->assigneduser->fullName()): '', - 'change' => $inout, - 'actions' => $actions, - 'companyName' => Company::getName($asset), + 'id' => $asset->id, + 'name' => (string)link_to('/hardware/'.$asset->id.'/view', $asset->showAssetName()), + 'model' => $asset->model->name, + 'asset_tag' => $asset->asset_tag, + 'serial' => $asset->serial, + 'assigned_to' => ($asset->assigneduser) ? link_to('/admin/users/'.$asset->assigneduser->id.'/view', $asset->assigneduser->fullName()): '', + 'change' => $inout, + 'actions' => $actions, + 'companyName' => Company::getName($asset), ); } $data = array('total' => $count, 'rows' => $rows); return $data; } + + + + public function getDataViewAccessories($categoryID) { + + $category = Category::with('accessories.company')->find($categoryID); + $category_assets = $category->accessories; + + if (Input::has('search')) { + $category_assets = $category_assets->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; + } + + $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; + + $allowed_columns = ['id','name','serial','asset_tag']; + $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at'; + $count = $category_assets->count(); + + $rows = array(); + + foreach ($category_assets as $asset) { + + $actions = ''; + $inout=''; + + if ($asset->deleted_at=='') { + $actions = '
'; + } + + + + $rows[] = array( + 'id' => $asset->id, + 'name' => (string)link_to_route('view/accessory', $asset->name, [$asset->id]), + 'actions' => $actions, + 'companyName' => Company::getName($asset), + ); + } + + $data = array('total' => $count, 'rows' => $rows); + return $data; + } + + + public function getDataViewConsumables($categoryID) { + + $category = Category::with('accessories.company')->find($categoryID); + $category_assets = $category->consumables; + + if (Input::has('search')) { + $category_assets = $category_assets->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; + } + + $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; + + $allowed_columns = ['id','name','serial','asset_tag']; + $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at'; + $count = $category_assets->count(); + + $rows = array(); + + foreach ($category_assets as $asset) { + + $actions = ''; + $inout=''; + + if ($asset->deleted_at=='') { + $actions = '
'; + } + + + + $rows[] = array( + 'id' => $asset->id, + 'name' => (string) link_to_route('view/consumable', $asset->name, [$asset->id]), + 'actions' => $actions, + 'companyName' => Company::getName($asset), + ); + } + + $data = array('total' => $count, 'rows' => $rows); + return $data; + } + + public function getDataViewComponent($categoryID) { + + $category = Category::with('accessories.company')->find($categoryID); + $category_assets = $category->components; + + if (Input::has('search')) { + $category_assets = $category_assets->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; + } + + $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; + + $allowed_columns = ['id','name','serial','asset_tag']; + $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at'; + $count = $category_assets->count(); + + $rows = array(); + + foreach ($category_assets as $asset) { + + $actions = ''; + $inout=''; + + if ($asset->deleted_at=='') { + $actions = '
'; + } + + + + $rows[] = array( + 'id' => $asset->id, + 'name' => (string)link_to_route('view/accessory', $asset->name, [$asset->id]), + 'actions' => $actions, + 'companyName' => Company::getName($asset), + ); + } + + $data = array('total' => $count, 'rows' => $rows); + return $data; + } + + + } diff --git a/app/Http/routes.php b/app/Http/routes.php index 6cd763b180..46483ca2b4 100755 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -137,10 +137,14 @@ Route::group([ 'prefix' => 'api', 'middleware' => 'auth' ], function () { Route::group([ 'prefix' => 'categories' ], function () { Route::get('list', [ 'as' => 'api.categories.list', 'uses' => 'CategoriesController@getDatatable' ]); - Route::get( - '{categoryID}/view', - [ 'as' => 'api.categories.view', 'uses' => 'CategoriesController@getDataView' ] - ); + Route::get( '{categoryID}/asset/view', + [ 'as' => 'api.categories.asset.view', 'uses' => 'CategoriesController@getDataViewAssets' ] ); + Route::get( '{categoryID}/accessory/view', + [ 'as' => 'api.categories.accessory.view', 'uses' => 'CategoriesController@getDataViewAccessories' ] ); + Route::get( '{categoryID}/consumable/view', + [ 'as' => 'api.categories.consumable.view', 'uses' => 'CategoriesController@getDataViewConsumables' ] ); + Route::get( '{categoryID}/component/view', + [ 'as' => 'api.categories.component.view', 'uses' => 'CategoriesController@getDataViewComponent' ] ); }); /*-- Suppliers API (mostly for creating new ones in-line while creating an asset) --*/ diff --git a/resources/views/categories/view.blade.php b/resources/views/categories/view.blade.php index b64a33cb18..07e895d55c 100644 --- a/resources/views/categories/view.blade.php +++ b/resources/views/categories/view.blade.php @@ -33,7 +33,7 @@ @@ -44,11 +44,13 @@ + @if ($category->category_type=='asset') + @endif
{{ trans('general.id') }} {{ trans('general.name') }}{{ trans('admin/hardware/form.model') }} {{ trans('general.asset_tag') }} {{ trans('admin/hardware/form.serial') }} {{ trans('general.user') }} {{ trans('admin/hardware/table.change') }}{{ trans('table.actions') }}