Skip to content

A free open source IT asset/license management system

Added selectlist transformer for select2 API calls

snipe7d11cb074890
diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php
index d4b66041a3..01696d57ab 100644
--- a/app/Http/Controllers/Api/AssetModelsController.php
+++ b/app/Http/Controllers/Api/AssetModelsController.php
@@ -8,6 +8,7 @@ use App\Helpers\Helper;
use Illuminate\Http\Request;
use App\Http\Transformers\AssetModelsTransformer;
use App\Http\Transformers\AssetsTransformer;
+use App\Http\Transformers\SelectlistTransformer;
/**
@@ -162,4 +163,39 @@ class AssetModelsController extends Controller
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/assetmodels/message.delete.success')));
}
+
+ /**
+ * Gets a paginated collection for the select2 menus
+ *
+ * @author [A. Gianotto] [<[email protected]>]
+ * @since [v4.0.16]
+ * @see \App\Http\Transformers\SelectlistTransformer
+ *
+ */
+ public function selectlist(Request $request)
+ {
+ $this->authorize('view', AssetModel::class);
+
+ $assetmodels = AssetModel::select([
+ 'models.id',
+ 'models.name',
+ 'models.image',
+ 'models.model_number',
+ ]);
+
+
+ if ($request->has('search')) {
+ $assetmodels = $assetmodels->where('models.name', 'LIKE', '%'.$request->get('search').'%')
+ ->orWhere('models.model_number', 'LIKE', '%'.$request->get('search').'%');
+ }
+ $assetmodels = $assetmodels->paginate(50);
+
+ foreach ($assetmodels as $assetmodel) {
+ $assetmodel->use_text = $assetmodel->present()->modelName;
+ $assetmodel->use_image = ($assetmodel->image) ? url('/').'/uploads/models/'.$assetmodel->image : null;
+ }
+
+ return (new SelectlistTransformer)->transformSelectlist($assetmodels);
+ }
+
}
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php
index f6ceb10739..b2b7f93e78 100644
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@ -30,6 +30,7 @@ use Str;
use TCPDF;
use Validator;
use View;
+use App\Http\Transformers\SelectlistTransformer;
/**
@@ -263,14 +264,14 @@ class AssetsController extends Controller
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200);
}
+
/**
- * Display a formatted JSON response for the select2 menus
+ * Gets a paginated collection for the select2 menus
*
- * @todo: create a transformer for handling these responses
* @author [A. Gianotto] [<[email protected]>]
- * @since [v4.0]
+ * @since [v4.0.16]
+ * @see \App\Http\Transformers\SelectlistTransformer
*
- * @return \Illuminate\Http\Response
*/
public function selectlist(Request $request)
{
@@ -293,32 +294,17 @@ class AssetsController extends Controller
}
$assets = $assets->paginate(50);
- $assets_array = [];
+ // Loop through and set some custom properties for the transformer to use.
+ // This lets us have more flexibility in special cases like assets, where
+ // they may not have a ->name value but we want to display something anyway
foreach ($assets as $asset) {
- $assets_array[] =
- [
- 'id' => (int) $asset->id,
- 'text' => e($asset->present()->fullName),
- 'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
- ];
+ $asset->use_text = $asset->present()->fullName;
+ $asset->use_image = ($asset->getImageUrl()) ? $asset->getImageUrl() : null;
}
- array_unshift($assets_array, ['id'=> '', 'text'=> trans('general.select_asset'), 'image' => null]);
-
- $results = [
- 'items' => $assets_array,
- 'pagination' =>
- [
- 'more' => ($assets->currentPage() >= $assets->lastPage()) ? false : true,
- 'per_page' => $assets->perPage()
- ],
- 'total_count' => $assets->total(),
- 'page' => $assets->currentPage(),
- 'page_count' => $assets->lastPage()
- ];
+ return (new SelectlistTransformer)->transformSelectlist($assets);
- return $results;
}
diff --git a/app/Http/Controllers/Api/CompaniesController.php b/app/Http/Controllers/Api/CompaniesController.php
index 582c3b6974..cf8d09065c 100644
--- a/app/Http/Controllers/Api/CompaniesController.php
+++ b/app/Http/Controllers/Api/CompaniesController.php
@@ -7,6 +7,7 @@ use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Helpers\Helper;
use App\Models\Company;
+use App\Http\Transformers\SelectlistTransformer;
class CompaniesController extends Controller
{
@@ -143,13 +144,12 @@ class CompaniesController extends Controller
}
/**
- * Display a formatted JSON response for the select2 menus
+ * Gets a paginated collection for the select2 menus
*
- * @todo: create a transformer for handling these responses
* @author [A. Gianotto] [<[email protected]>]
- * @since [v4.0]
+ * @since [v4.0.16]
+ * @see \App\Http\Transformers\SelectlistTransformer
*
- * @return \Illuminate\Http\Response
*/
public function selectlist(Request $request)
{
@@ -161,37 +161,20 @@ class CompaniesController extends Controller
'companies.image',
]);
-
if ($request->has('search')) {
$companies = $companies->where('companies.name', 'LIKE', '%'.$request->get('search').'%');
}
- $companies = $companies->paginate(50);
- $companies_array = [];
+ $companies = $companies->orderBy('name', 'ASC')->paginate(50);
+ // Loop through and set some custom properties for the transformer to use.
+ // This lets us have more flexibility in special cases like assets, where
+ // they may not have a ->name value but we want to display something anyway
foreach ($companies as $company) {
- $companies_array[] =
- [
- 'id' => (int) $company->id,
- 'text' => e($company->name),
- 'image' => ($company->image) ? url('/').'/uploads/companies/'.$company->image : null,
- ];
+ $company->use_text = $company->name;
+ $company->use_image = ($company->image) ? url('/').'/uploads/companies/'.$company->image : null;
}
- array_unshift($companies_array, ['id'=> '', 'text'=> trans('general.select_company'), 'image' => null]);
-
- $results = [
- 'items' => $companies_array,
- 'pagination' =>
- [
- 'more' => ($companies->currentPage() >= $companies->lastPage()) ? false : true,
- 'per_page' => $companies->perPage()
- ],
- 'total_count' => $companies->total(),
- 'page' => $companies->currentPage(),
- 'page_count' => $companies->lastPage()
- ];
-
- return $results;
+ return (new SelectlistTransformer)->transformSelectlist($companies);
}
}
diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php
index 381eedb810..1e47cf7506 100644
--- a/app/Http/Controllers/Api/LocationsController.php
+++ b/app/Http/Controllers/Api/LocationsController.php
@@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
use App\Helpers\Helper;
use App\Models\Location;
use App\Http\Transformers\LocationsTransformer;
+use App\Http\Transformers\SelectlistTransformer;
class LocationsController extends Controller
{
@@ -141,13 +142,12 @@ class LocationsController extends Controller
}
/**
- * Display a formatted JSON response for the select2 menus
+ * Gets a paginated collection for the select2 menus
*
- * @todo: create a transformer for handling these responses
* @author [A. Gianotto] [<[email protected]>]
- * @since [v4.0]
+ * @since [v4.0.16]
+ * @see \App\Http\Transformers\SelectlistTransformer
*
- * @return \Illuminate\Http\Response
*/
public function selectlist(Request $request)
{
@@ -159,38 +159,22 @@ class LocationsController extends Controller
'locations.image',
]);
-
if ($request->has('search')) {
$locations = $locations->where('locations.name', 'LIKE', '%'.$request->get('search').'%');
}
- $locations = $locations->paginate(50);
- $locations_array = [];
+ $locations = $locations->orderBy('name', 'ASC')->paginate(50);
+ // Loop through and set some custom properties for the transformer to use.
+ // This lets us have more flexibility in special cases like assets, where
+ // they may not have a ->name value but we want to display something anyway
foreach ($locations as $location) {
- $locations_array[] =
- [
- 'id' => (int) $location->id,
- 'text' => e($location->name),
- 'image' => ($location->image) ? url('/').'/uploads/locations/'.$location->image : null,
- ];
+ $location->use_text = $location->name;
+ $location->use_image = ($location->image) ? url('/').'/uploads/locations/'.$location->image : null;
}
- array_unshift($locations_array, ['id'=> '', 'text'=> trans('general.select_location'), 'image' => null]);
-
- $results = [
- 'items' => $locations_array,
- 'pagination' =>
- [
- 'more' => ($locations->currentPage() >= $locations->lastPage()) ? false : true,
- 'per_page' => $locations->perPage()
- ],
- 'total_count' => $locations->total(),
- 'page' => $locations->currentPage(),
- 'page_count' => $locations->lastPage()
- ];
-
- return $results;
+ return (new SelectlistTransformer)->transformSelectlist($locations);
+
}
}
diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php
index 19911bccb6..ece53b0a69 100644
--- a/app/Http/Controllers/Api/UsersController.php
+++ b/app/Http/Controllers/Api/UsersController.php
@@ -11,6 +11,7 @@ use App\Helpers\Helper;
use App\Http\Requests\SaveUserRequest;
use App\Models\Asset;
use App\Http\Transformers\AssetsTransformer;
+use App\Http\Transformers\SelectlistTransformer;
class UsersController extends Controller
{
@@ -111,12 +112,12 @@ class UsersController extends Controller
/**
- * Display a listing of the resource.
+ * Gets a paginated collection for the select2 menus
*
* @author [A. Gianotto] [<[email protected]>]
- * @since [v4.0]
+ * @since [v4.0.16]
+ * @see \App\Http\Transformers\SelectlistTransformer
*
- * @return \Illuminate\Http\Response
*/
public function selectlist(Request $request)
{
@@ -145,7 +146,7 @@ class UsersController extends Controller
$users = $users->orderBy('last_name', 'asc');
$users = $users->paginate(50);
- $users_array = [];
+
foreach ($users as $user) {
$name_str = '';
@@ -158,28 +159,12 @@ class UsersController extends Controller
$name_str .= ' (#'.e($user->employee_num).')';
}
- $users_array[] =
- [
- 'id' => $user->id,
- 'text' => $name_str,
- 'image' => ($user->present()->gravatar) ? $user->present()->gravatar : null,
- ];
+ $user->use_text = $name_str;
+ $user->use_image = ($user->present()->gravatar) ? $user->present()->gravatar : null;
}
- array_unshift($users_array, ['id'=> '', 'text'=> trans('general.select_user'), 'image' => null]);
- $results = [
- 'items' => $users_array,
- 'pagination' =>
- [
- 'more' => ($users->currentPage() >= $users->lastPage()) ? false : true,
- 'per_page' => $users->perPage()
- ],
- 'total_count' => $users->total(),
- 'page' => $users->currentPage(),
- 'page_count' => $users->lastPage()
- ];
-
- return $results;
+ return (new SelectlistTransformer)->transformSelectlist($users);
+
}
diff --git a/app/Http/Transformers/SelectlistTransformer.php b/app/Http/Transformers/SelectlistTransformer.php
new file mode 100644
index 0000000000..5ac059e352
--- /dev/null
+++ b/app/Http/Transformers/SelectlistTransformer.php
@@ -0,0 +1,51 @@
+<?php
+namespace App\Http\Transformers;
+use Illuminate\Pagination\LengthAwarePaginator;
+
+/**
+ * Class SelectlistTransformer
+ *
+ * This handles the standardized formatting of the API response we need to provide for
+ * the rich (text and images) Select2 javascript.
+ *
+ * @package App\Http\Transformers
+ * @author [A. Gianotto] [<[email protected]>]
+ * @since [v4.0.16]
+ * @return \Illuminate\Http\Response
+ */
+
+class SelectlistTransformer
+{
+
+ public function transformSelectlist (LengthAwarePaginator $select_items)
+ {
+ $items_array = [];
+
+ // Loop through the paginated collection to set the array values
+ foreach ($select_items as $select_item) {
+ $items_array[]= [
+ 'id' => (int) $select_item->id,
+ 'text' => e($select_item->use_text),
+ 'image' => ($select_item->use_image) ? e($select_item->use_image) : null,
+
+ ];
+
+ }
+
+ $results = [
+ 'items' => $items_array,
+ 'pagination' =>
+ [
+ 'more' => ($select_items->currentPage() >= $select_items->lastPage()) ? false : true,
+ 'per_page' => $select_items->perPage()
+ ],
+ 'total_count' => $select_items->total(),
+ 'page' => $select_items->currentPage(),
+ 'page_count' => $select_items->lastPage()
+ ];
+
+ return $results;
+
+ }
+
+}