mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-24 21:24:13 -08:00
Added selectlist transformer for select2 API calls
This commit is contained in:
parent
9c29ee9c6d
commit
7d11cb0748
|
@ -8,6 +8,7 @@ use App\Helpers\Helper;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Http\Transformers\AssetModelsTransformer;
|
use App\Http\Transformers\AssetModelsTransformer;
|
||||||
use App\Http\Transformers\AssetsTransformer;
|
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')));
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/assetmodels/message.delete.success')));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a paginated collection for the select2 menus
|
||||||
|
*
|
||||||
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ use Str;
|
||||||
use TCPDF;
|
use TCPDF;
|
||||||
use Validator;
|
use Validator;
|
||||||
use View;
|
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);
|
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] [<snipe@snipe.net>]
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||||
* @since [v4.0]
|
* @since [v4.0.16]
|
||||||
|
* @see \App\Http\Transformers\SelectlistTransformer
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
*/
|
||||||
public function selectlist(Request $request)
|
public function selectlist(Request $request)
|
||||||
{
|
{
|
||||||
|
@ -293,32 +294,17 @@ class AssetsController extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
$assets = $assets->paginate(50);
|
$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) {
|
foreach ($assets as $asset) {
|
||||||
$assets_array[] =
|
$asset->use_text = $asset->present()->fullName;
|
||||||
[
|
$asset->use_image = ($asset->getImageUrl()) ? $asset->getImageUrl() : null;
|
||||||
'id' => (int) $asset->id,
|
|
||||||
'text' => e($asset->present()->fullName),
|
|
||||||
'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
array_unshift($assets_array, ['id'=> '', 'text'=> trans('general.select_asset'), 'image' => null]);
|
return (new SelectlistTransformer)->transformSelectlist($assets);
|
||||||
|
|
||||||
$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 $results;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ use Illuminate\Http\Request;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
|
use App\Http\Transformers\SelectlistTransformer;
|
||||||
|
|
||||||
class CompaniesController extends Controller
|
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] [<snipe@snipe.net>]
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||||
* @since [v4.0]
|
* @since [v4.0.16]
|
||||||
|
* @see \App\Http\Transformers\SelectlistTransformer
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
*/
|
||||||
public function selectlist(Request $request)
|
public function selectlist(Request $request)
|
||||||
{
|
{
|
||||||
|
@ -161,37 +161,20 @@ class CompaniesController extends Controller
|
||||||
'companies.image',
|
'companies.image',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
if ($request->has('search')) {
|
if ($request->has('search')) {
|
||||||
$companies = $companies->where('companies.name', 'LIKE', '%'.$request->get('search').'%');
|
$companies = $companies->where('companies.name', 'LIKE', '%'.$request->get('search').'%');
|
||||||
}
|
}
|
||||||
|
|
||||||
$companies = $companies->paginate(50);
|
$companies = $companies->orderBy('name', 'ASC')->paginate(50);
|
||||||
$companies_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 ($companies as $company) {
|
foreach ($companies as $company) {
|
||||||
$companies_array[] =
|
$company->use_text = $company->name;
|
||||||
[
|
$company->use_image = ($company->image) ? url('/').'/uploads/companies/'.$company->image : null;
|
||||||
'id' => (int) $company->id,
|
|
||||||
'text' => e($company->name),
|
|
||||||
'image' => ($company->image) ? url('/').'/uploads/companies/'.$company->image : null,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
array_unshift($companies_array, ['id'=> '', 'text'=> trans('general.select_company'), 'image' => null]);
|
return (new SelectlistTransformer)->transformSelectlist($companies);
|
||||||
|
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use App\Models\Location;
|
use App\Models\Location;
|
||||||
use App\Http\Transformers\LocationsTransformer;
|
use App\Http\Transformers\LocationsTransformer;
|
||||||
|
use App\Http\Transformers\SelectlistTransformer;
|
||||||
|
|
||||||
class LocationsController extends Controller
|
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] [<snipe@snipe.net>]
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||||
* @since [v4.0]
|
* @since [v4.0.16]
|
||||||
|
* @see \App\Http\Transformers\SelectlistTransformer
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
*/
|
||||||
public function selectlist(Request $request)
|
public function selectlist(Request $request)
|
||||||
{
|
{
|
||||||
|
@ -159,38 +159,22 @@ class LocationsController extends Controller
|
||||||
'locations.image',
|
'locations.image',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
if ($request->has('search')) {
|
if ($request->has('search')) {
|
||||||
$locations = $locations->where('locations.name', 'LIKE', '%'.$request->get('search').'%');
|
$locations = $locations->where('locations.name', 'LIKE', '%'.$request->get('search').'%');
|
||||||
}
|
}
|
||||||
|
|
||||||
$locations = $locations->paginate(50);
|
$locations = $locations->orderBy('name', 'ASC')->paginate(50);
|
||||||
$locations_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 ($locations as $location) {
|
foreach ($locations as $location) {
|
||||||
$locations_array[] =
|
$location->use_text = $location->name;
|
||||||
[
|
$location->use_image = ($location->image) ? url('/').'/uploads/locations/'.$location->image : null;
|
||||||
'id' => (int) $location->id,
|
|
||||||
'text' => e($location->name),
|
|
||||||
'image' => ($location->image) ? url('/').'/uploads/locations/'.$location->image : null,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
array_unshift($locations_array, ['id'=> '', 'text'=> trans('general.select_location'), 'image' => null]);
|
return (new SelectlistTransformer)->transformSelectlist($locations);
|
||||||
|
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ use App\Helpers\Helper;
|
||||||
use App\Http\Requests\SaveUserRequest;
|
use App\Http\Requests\SaveUserRequest;
|
||||||
use App\Models\Asset;
|
use App\Models\Asset;
|
||||||
use App\Http\Transformers\AssetsTransformer;
|
use App\Http\Transformers\AssetsTransformer;
|
||||||
|
use App\Http\Transformers\SelectlistTransformer;
|
||||||
|
|
||||||
class UsersController extends Controller
|
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] [<snipe@snipe.net>]
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||||
* @since [v4.0]
|
* @since [v4.0.16]
|
||||||
|
* @see \App\Http\Transformers\SelectlistTransformer
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
*/
|
||||||
public function selectlist(Request $request)
|
public function selectlist(Request $request)
|
||||||
{
|
{
|
||||||
|
@ -145,7 +146,7 @@ class UsersController extends Controller
|
||||||
|
|
||||||
$users = $users->orderBy('last_name', 'asc');
|
$users = $users->orderBy('last_name', 'asc');
|
||||||
$users = $users->paginate(50);
|
$users = $users->paginate(50);
|
||||||
$users_array = [];
|
|
||||||
|
|
||||||
foreach ($users as $user) {
|
foreach ($users as $user) {
|
||||||
$name_str = '';
|
$name_str = '';
|
||||||
|
@ -158,28 +159,12 @@ class UsersController extends Controller
|
||||||
$name_str .= ' (#'.e($user->employee_num).')';
|
$name_str .= ' (#'.e($user->employee_num).')';
|
||||||
}
|
}
|
||||||
|
|
||||||
$users_array[] =
|
$user->use_text = $name_str;
|
||||||
[
|
$user->use_image = ($user->present()->gravatar) ? $user->present()->gravatar : null;
|
||||||
'id' => $user->id,
|
|
||||||
'text' => $name_str,
|
|
||||||
'image' => ($user->present()->gravatar) ? $user->present()->gravatar : null,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
array_unshift($users_array, ['id'=> '', 'text'=> trans('general.select_user'), 'image' => null]);
|
return (new SelectlistTransformer)->transformSelectlist($users);
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
51
app/Http/Transformers/SelectlistTransformer.php
Normal file
51
app/Http/Transformers/SelectlistTransformer.php
Normal file
|
@ -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] [<snipe@snipe.net>]
|
||||||
|
* @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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue