2017-01-12 19:40:20 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Helpers\Helper;
|
2021-06-29 02:28:42 -07:00
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2017-01-12 19:40:20 -08:00
|
|
|
use App\Http\Controllers\Controller;
|
2017-01-26 20:08:06 -08:00
|
|
|
use App\Http\Transformers\LocationsTransformer;
|
2017-10-26 21:50:01 -07:00
|
|
|
use App\Http\Transformers\SelectlistTransformer;
|
2021-06-10 13:15:52 -07:00
|
|
|
use App\Models\Location;
|
|
|
|
use Illuminate\Http\Request;
|
2020-04-22 06:39:41 -07:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
use Illuminate\Support\Collection;
|
2017-01-12 19:40:20 -08:00
|
|
|
|
|
|
|
class LocationsController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2017-01-13 07:40:08 -08:00
|
|
|
public function index(Request $request)
|
2017-01-12 19:40:20 -08:00
|
|
|
{
|
|
|
|
$this->authorize('view', Location::class);
|
2017-10-30 19:21:35 -07:00
|
|
|
$allowed_columns = [
|
2021-06-10 13:15:52 -07:00
|
|
|
'id', 'name', 'address', 'address2', 'city', 'state', 'country', 'zip', 'created_at',
|
|
|
|
'updated_at', 'manager_id', 'image',
|
2022-10-26 09:39:03 -07:00
|
|
|
'assigned_assets_count', 'users_count', 'assets_count','assigned_assets_count', 'assets_count', 'rtd_assets_count', 'currency', 'ldap_ou', ];
|
2017-01-13 07:40:08 -08:00
|
|
|
|
2020-04-22 06:39:41 -07:00
|
|
|
$locations = Location::with('parent', 'manager', 'children')->select([
|
2017-01-13 07:40:08 -08:00
|
|
|
'locations.id',
|
|
|
|
'locations.name',
|
|
|
|
'locations.address',
|
|
|
|
'locations.address2',
|
|
|
|
'locations.city',
|
|
|
|
'locations.state',
|
|
|
|
'locations.zip',
|
|
|
|
'locations.country',
|
|
|
|
'locations.parent_id',
|
2017-05-22 17:27:00 -07:00
|
|
|
'locations.manager_id',
|
2017-03-10 22:09:21 -08:00
|
|
|
'locations.created_at',
|
|
|
|
'locations.updated_at',
|
2017-10-25 22:35:58 -07:00
|
|
|
'locations.image',
|
2020-11-17 22:17:07 -08:00
|
|
|
'locations.ldap_ou',
|
2021-06-10 13:15:52 -07:00
|
|
|
'locations.currency',
|
2019-05-24 16:01:12 -07:00
|
|
|
])->withCount('assignedAssets as assigned_assets_count')
|
2022-10-26 09:39:03 -07:00
|
|
|
->withCount('assets as assets_count')
|
2022-10-26 00:16:06 -07:00
|
|
|
->withCount('rtd_assets as rtd_assets_count')
|
2020-04-22 06:39:41 -07:00
|
|
|
->withCount('users as users_count');
|
2017-01-13 07:40:08 -08:00
|
|
|
|
2019-05-23 17:39:50 -07:00
|
|
|
if ($request->filled('search')) {
|
2017-01-13 07:40:08 -08:00
|
|
|
$locations = $locations->TextSearch($request->input('search'));
|
|
|
|
}
|
|
|
|
|
2022-06-28 19:07:11 -07:00
|
|
|
if ($request->filled('name')) {
|
|
|
|
$locations->where('locations.name', '=', $request->input('name'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('address')) {
|
|
|
|
$locations->where('locations.address', '=', $request->input('address'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('address2')) {
|
|
|
|
$locations->where('locations.address2', '=', $request->input('address2'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('city')) {
|
|
|
|
$locations->where('locations.city', '=', $request->input('city'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('zip')) {
|
|
|
|
$locations->where('locations.zip', '=', $request->input('zip'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->filled('country')) {
|
|
|
|
$locations->where('locations.country', '=', $request->input('country'));
|
|
|
|
}
|
|
|
|
|
2023-04-12 11:28:46 -07:00
|
|
|
// Make sure the offset and limit are actually integers and do not exceed system limits
|
2023-04-15 17:28:25 -07:00
|
|
|
$offset = ($request->input('offset') > $locations->count()) ? $locations->count() : abs($request->input('offset'));
|
2023-04-16 08:46:39 -07:00
|
|
|
$limit = app('api_limit_value');
|
2019-09-03 14:02:08 -07:00
|
|
|
|
2017-01-13 07:40:08 -08:00
|
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
|
|
|
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
|
2018-02-21 02:17:18 -08:00
|
|
|
|
2023-04-15 17:28:25 -07:00
|
|
|
|
2023-04-12 11:28:46 -07:00
|
|
|
|
2018-02-21 02:17:18 -08:00
|
|
|
switch ($request->input('sort')) {
|
|
|
|
case 'parent':
|
|
|
|
$locations->OrderParent($order);
|
|
|
|
break;
|
2018-02-21 05:09:40 -08:00
|
|
|
case 'manager':
|
|
|
|
$locations->OrderManager($order);
|
|
|
|
break;
|
2018-02-21 02:17:18 -08:00
|
|
|
default:
|
|
|
|
$locations->orderBy($sort, $order);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-13 07:40:08 -08:00
|
|
|
|
|
|
|
$total = $locations->count();
|
|
|
|
$locations = $locations->skip($offset)->take($limit)->get();
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-26 20:08:06 -08:00
|
|
|
return (new LocationsTransformer)->transformLocations($locations, $total);
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
2021-06-29 02:28:42 -07:00
|
|
|
* @param \App\Http\Requests\ImageUploadRequest $request
|
2017-01-12 19:40:20 -08:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2021-06-29 02:28:42 -07:00
|
|
|
public function store(ImageUploadRequest $request)
|
2017-01-12 19:40:20 -08:00
|
|
|
{
|
|
|
|
$this->authorize('create', Location::class);
|
|
|
|
$location = new Location;
|
|
|
|
$location->fill($request->all());
|
2021-06-29 02:28:42 -07:00
|
|
|
$location = $request->handleImages($location);
|
2017-01-12 19:40:20 -08:00
|
|
|
|
|
|
|
if ($location->save()) {
|
2017-08-08 12:36:28 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', (new LocationsTransformer)->transformLocation($location), trans('admin/locations/message.create.success')));
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-12 19:40:20 -08:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $location->getErrors()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$this->authorize('view', Location::class);
|
2020-04-22 06:39:41 -07:00
|
|
|
$location = Location::with('parent', 'manager', 'children')
|
2018-09-21 15:50:14 -07:00
|
|
|
->select([
|
|
|
|
'locations.id',
|
|
|
|
'locations.name',
|
|
|
|
'locations.address',
|
|
|
|
'locations.address2',
|
|
|
|
'locations.city',
|
|
|
|
'locations.state',
|
|
|
|
'locations.zip',
|
|
|
|
'locations.country',
|
|
|
|
'locations.parent_id',
|
|
|
|
'locations.manager_id',
|
|
|
|
'locations.created_at',
|
|
|
|
'locations.updated_at',
|
|
|
|
'locations.image',
|
2021-06-10 13:15:52 -07:00
|
|
|
'locations.currency',
|
2018-09-21 15:50:14 -07:00
|
|
|
])
|
2019-05-24 16:01:12 -07:00
|
|
|
->withCount('assignedAssets as assigned_assets_count')
|
2022-10-26 09:39:03 -07:00
|
|
|
->withCount('assets as assets_count')
|
2022-10-26 00:16:06 -07:00
|
|
|
->withCount('rtd_assets as rtd_assets_count')
|
|
|
|
->withCount('users as users_count')
|
|
|
|
->findOrFail($id);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-26 21:02:59 -08:00
|
|
|
return (new LocationsTransformer)->transformLocation($location);
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
2021-06-29 02:28:42 -07:00
|
|
|
* @param \App\Http\Requests\ImageUploadRequest $request
|
2017-01-12 19:40:20 -08:00
|
|
|
* @param int $id
|
2020-04-22 06:39:41 -07:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2017-01-12 19:40:20 -08:00
|
|
|
*/
|
2021-06-29 02:28:42 -07:00
|
|
|
public function update(ImageUploadRequest $request, $id)
|
2017-01-12 19:40:20 -08:00
|
|
|
{
|
2018-07-12 18:28:02 -07:00
|
|
|
$this->authorize('update', Location::class);
|
2017-01-12 19:40:20 -08:00
|
|
|
$location = Location::findOrFail($id);
|
2020-04-22 06:39:41 -07:00
|
|
|
|
2017-01-12 19:40:20 -08:00
|
|
|
$location->fill($request->all());
|
2021-06-29 02:28:42 -07:00
|
|
|
$location = $request->handleImages($location);
|
2020-04-22 06:39:41 -07:00
|
|
|
|
|
|
|
if ($location->isValid()) {
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2020-04-22 06:39:41 -07:00
|
|
|
$location->save();
|
2017-10-01 12:57:04 -07:00
|
|
|
return response()->json(
|
|
|
|
Helper::formatStandardApiResponse(
|
|
|
|
'success',
|
|
|
|
(new LocationsTransformer)->transformLocation($location),
|
|
|
|
trans('admin/locations/message.update.success')
|
|
|
|
)
|
|
|
|
);
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $location->getErrors()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
$this->authorize('delete', Location::class);
|
|
|
|
$location = Location::findOrFail($id);
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $location->isDeletable()) {
|
2020-05-23 10:36:02 -07:00
|
|
|
return response()
|
2021-06-10 13:15:52 -07:00
|
|
|
->json(Helper::formatStandardApiResponse('error', null, trans('admin/companies/message.assoc_users')));
|
2020-05-23 10:36:02 -07:00
|
|
|
}
|
2017-01-12 19:40:20 -08:00
|
|
|
$this->authorize('delete', $location);
|
|
|
|
$location->delete();
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-10-01 12:57:04 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/locations/message.delete.success')));
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|
2017-10-26 02:28:17 -07:00
|
|
|
|
|
|
|
/**
|
2017-10-26 21:50:01 -07:00
|
|
|
* Gets a paginated collection for the select2 menus
|
2017-10-26 02:28:17 -07:00
|
|
|
*
|
2019-10-02 03:56:56 -07:00
|
|
|
* This is handled slightly differently as of ~4.7.8-pre, as
|
|
|
|
* we have to do some recursive magic to get the hierarchy to display
|
|
|
|
* properly when looking at the parent/child relationship in the
|
|
|
|
* rich menus.
|
|
|
|
*
|
|
|
|
* This means we can't use the normal pagination that we use elsewhere
|
|
|
|
* in our selectlists, since we have to get the full set before we can
|
|
|
|
* determine which location is parent/child/grandchild, etc.
|
|
|
|
*
|
|
|
|
* This also means that hierarchy display gets a little funky when people
|
|
|
|
* use the Select2 search functionality, but there's not much we can do about
|
|
|
|
* that right now.
|
|
|
|
*
|
|
|
|
* As a result, instead of paginating as part of the query, we have to grab
|
|
|
|
* the entire data set, and then invoke a paginator manually and pass that
|
|
|
|
* through to the SelectListTransformer.
|
|
|
|
*
|
2020-04-22 06:39:41 -07:00
|
|
|
* Many thanks to @uberbrady for the help getting this working better.
|
|
|
|
* Recursion still sucks, but I guess he doesn't have to get in the
|
|
|
|
* sea... this time.
|
|
|
|
*
|
2017-10-26 02:28:17 -07:00
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2017-10-26 21:50:01 -07:00
|
|
|
* @since [v4.0.16]
|
|
|
|
* @see \App\Http\Transformers\SelectlistTransformer
|
2017-10-26 02:28:17 -07:00
|
|
|
*/
|
2020-04-22 06:39:41 -07:00
|
|
|
public function selectlist(Request $request)
|
2017-10-26 02:28:17 -07:00
|
|
|
{
|
|
|
|
|
2022-02-11 11:46:14 -08:00
|
|
|
$this->authorize('view.selectlists');
|
|
|
|
|
2017-10-26 02:28:17 -07:00
|
|
|
$locations = Location::select([
|
|
|
|
'locations.id',
|
|
|
|
'locations.name',
|
2020-04-22 06:39:41 -07:00
|
|
|
'locations.parent_id',
|
2017-10-26 02:28:17 -07:00
|
|
|
'locations.image',
|
2020-04-22 06:39:41 -07:00
|
|
|
]);
|
2017-10-26 02:28:17 -07:00
|
|
|
|
2019-10-02 03:56:56 -07:00
|
|
|
$page = 1;
|
|
|
|
if ($request->filled('page')) {
|
|
|
|
$page = $request->input('page');
|
|
|
|
}
|
|
|
|
|
2020-04-22 06:39:41 -07:00
|
|
|
if ($request->filled('search')) {
|
|
|
|
$locations = $locations->where('locations.name', 'LIKE', '%'.$request->input('search').'%');
|
|
|
|
}
|
2017-10-26 02:28:17 -07:00
|
|
|
|
2020-04-22 06:39:41 -07:00
|
|
|
$locations = $locations->orderBy('name', 'ASC')->get();
|
2017-10-26 02:28:17 -07:00
|
|
|
|
2019-10-02 03:56:56 -07:00
|
|
|
$locations_with_children = [];
|
2019-12-06 13:14:10 -08:00
|
|
|
|
2017-10-26 02:28:17 -07:00
|
|
|
foreach ($locations as $location) {
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! array_key_exists($location->parent_id, $locations_with_children)) {
|
2019-10-02 03:56:56 -07:00
|
|
|
$locations_with_children[$location->parent_id] = [];
|
|
|
|
}
|
|
|
|
$locations_with_children[$location->parent_id][] = $location;
|
2017-10-26 02:28:17 -07:00
|
|
|
}
|
2017-10-26 03:43:28 -07:00
|
|
|
|
2019-12-06 13:14:10 -08:00
|
|
|
if ($request->filled('search')) {
|
2021-06-10 13:15:52 -07:00
|
|
|
$locations_formatted = $locations;
|
2019-12-06 13:14:10 -08:00
|
|
|
} else {
|
|
|
|
$location_options = Location::indenter($locations_with_children);
|
|
|
|
$locations_formatted = new Collection($location_options);
|
|
|
|
}
|
2019-10-02 03:56:56 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$paginated_results = new LengthAwarePaginator($locations_formatted->forPage($page, 500), $locations_formatted->count(), 500, $page, []);
|
2019-10-02 03:56:56 -07:00
|
|
|
|
2020-04-22 06:39:41 -07:00
|
|
|
//return [];
|
|
|
|
return (new SelectlistTransformer)->transformSelectlist($paginated_results);
|
2017-10-26 02:28:17 -07:00
|
|
|
}
|
2020-05-23 10:36:02 -07:00
|
|
|
}
|