2017-01-12 19:40:20 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Models\Location;
|
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;
|
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 = [
|
|
|
|
'id','name','address','address2','city','state','country','zip','created_at',
|
2018-02-21 02:17:18 -08:00
|
|
|
'updated_at','manager_id','image',
|
2018-02-16 13:22:55 -08:00
|
|
|
'assigned_assets_count','users_count','assets_count','currency'];
|
2017-01-13 07:40:08 -08:00
|
|
|
|
2017-10-01 12:57:04 -07:00
|
|
|
$locations = Location::with('parent', 'manager', 'childLocations')->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',
|
2017-01-13 07:40:08 -08:00
|
|
|
'locations.currency'
|
2017-11-03 19:40:40 -07:00
|
|
|
])->withCount('assignedAssets')
|
2017-10-01 12:57:04 -07:00
|
|
|
->withCount('assets')
|
|
|
|
->withCount('users');
|
2017-01-13 07:40:08 -08:00
|
|
|
|
|
|
|
if ($request->has('search')) {
|
|
|
|
$locations = $locations->TextSearch($request->input('search'));
|
|
|
|
}
|
|
|
|
|
2018-02-21 02:17:18 -08:00
|
|
|
|
|
|
|
|
2017-01-13 07:40:08 -08:00
|
|
|
$offset = $request->input('offset', 0);
|
|
|
|
$limit = $request->input('limit', 50);
|
|
|
|
$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
|
|
|
|
|
|
|
switch ($request->input('sort')) {
|
|
|
|
case 'parent':
|
|
|
|
$locations->OrderParent($order);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$locations->orderBy($sort, $order);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-13 07:40:08 -08:00
|
|
|
|
|
|
|
$total = $locations->count();
|
|
|
|
$locations = $locations->skip($offset)->take($limit)->get();
|
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]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorize('create', Location::class);
|
|
|
|
$location = new Location;
|
|
|
|
$location->fill($request->all());
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
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);
|
|
|
|
$location = Location::findOrFail($id);
|
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]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
|
|
|
$this->authorize('edit', Location::class);
|
|
|
|
$location = Location::findOrFail($id);
|
|
|
|
$location->fill($request->all());
|
|
|
|
|
|
|
|
if ($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);
|
|
|
|
$this->authorize('delete', $location);
|
|
|
|
$location->delete();
|
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
|
|
|
*
|
|
|
|
* @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
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function selectlist(Request $request)
|
|
|
|
{
|
|
|
|
|
|
|
|
$locations = Location::select([
|
|
|
|
'locations.id',
|
|
|
|
'locations.name',
|
|
|
|
'locations.image',
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($request->has('search')) {
|
|
|
|
$locations = $locations->where('locations.name', 'LIKE', '%'.$request->get('search').'%');
|
|
|
|
}
|
|
|
|
|
2017-10-26 21:50:01 -07:00
|
|
|
$locations = $locations->orderBy('name', 'ASC')->paginate(50);
|
2017-10-26 02:28:17 -07:00
|
|
|
|
2017-10-26 21:50:01 -07:00
|
|
|
// 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
|
2017-10-26 02:28:17 -07:00
|
|
|
foreach ($locations as $location) {
|
2017-10-26 21:50:01 -07:00
|
|
|
$location->use_text = $location->name;
|
|
|
|
$location->use_image = ($location->image) ? url('/').'/uploads/locations/'.$location->image : null;
|
2017-10-26 02:28:17 -07:00
|
|
|
}
|
2017-10-26 03:43:28 -07:00
|
|
|
|
2017-10-26 21:50:01 -07:00
|
|
|
return (new SelectlistTransformer)->transformSelectlist($locations);
|
|
|
|
|
2017-10-26 02:28:17 -07:00
|
|
|
}
|
|
|
|
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|