2017-05-22 21:32:33 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Department;
|
|
|
|
use App\Http\Transformers\DepartmentsTransformer;
|
2017-05-23 01:08:55 -07:00
|
|
|
use App\Helpers\Helper;
|
|
|
|
use Auth;
|
2017-10-28 09:21:39 -07:00
|
|
|
use App\Http\Transformers\SelectlistTransformer;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2017-05-22 21:32:33 -07:00
|
|
|
|
|
|
|
class DepartmentsController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorize('view', Department::class);
|
2018-02-21 05:04:20 -08:00
|
|
|
$allowed_columns = ['id','name','image','users_count'];
|
2017-05-22 21:32:33 -07:00
|
|
|
|
|
|
|
$departments = Department::select([
|
2018-02-21 05:04:20 -08:00
|
|
|
'departments.id',
|
|
|
|
'departments.name',
|
|
|
|
'departments.location_id',
|
|
|
|
'departments.company_id',
|
|
|
|
'departments.manager_id',
|
|
|
|
'departments.created_at',
|
|
|
|
'departments.updated_at',
|
|
|
|
'departments.image'
|
2018-07-24 22:40:05 -07:00
|
|
|
])->with('users')->with('location')->with('manager')->with('company')->withCount('users as users_count');
|
2017-05-22 21:32:33 -07:00
|
|
|
|
2018-07-24 22:51:31 -07:00
|
|
|
if ($request->filled('search')) {
|
2017-05-22 21:32:33 -07:00
|
|
|
$departments = $departments->TextSearch($request->input('search'));
|
|
|
|
}
|
|
|
|
|
2019-02-14 15:01:34 -08:00
|
|
|
$offset = (($departments) && (request('offset') > $departments->count())) ? 0 : request('offset', 0);
|
2017-05-22 21:32:33 -07:00
|
|
|
$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 05:04:20 -08:00
|
|
|
|
|
|
|
switch ($request->input('sort')) {
|
|
|
|
case 'location':
|
|
|
|
$departments->OrderLocation($order);
|
|
|
|
break;
|
|
|
|
case 'manager':
|
|
|
|
$departments->OrderManager($order);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$departments->orderBy($sort, $order);
|
|
|
|
break;
|
|
|
|
}
|
2017-05-22 21:32:33 -07:00
|
|
|
|
|
|
|
$total = $departments->count();
|
|
|
|
$departments = $departments->skip($offset)->take($limit)->get();
|
|
|
|
return (new DepartmentsTransformer)->transformDepartments($departments, $total);
|
|
|
|
|
|
|
|
}
|
2017-05-23 01:08:55 -07: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', Department::class);
|
|
|
|
$department = new Department;
|
|
|
|
$department->fill($request->all());
|
|
|
|
$department->user_id = Auth::user()->id;
|
2018-07-24 22:51:31 -07:00
|
|
|
$department->manager_id = ($request->filled('manager_id' ) ? $request->input('manager_id') : null);
|
2017-05-23 01:08:55 -07:00
|
|
|
|
|
|
|
if ($department->save()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $department, trans('admin/departments/message.create.success')));
|
|
|
|
}
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $department->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', Department::class);
|
|
|
|
$department = Department::findOrFail($id);
|
|
|
|
return (new DepartmentsTransformer)->transformDepartment($department);
|
|
|
|
}
|
2017-05-23 02:45:51 -07:00
|
|
|
|
2018-10-31 14:31:57 -07:00
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v5.0]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
|
|
|
$this->authorize('update', Department::class);
|
|
|
|
$department = Department::findOrFail($id);
|
|
|
|
$department->fill($request->all());
|
|
|
|
|
|
|
|
if ($department->save()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $department, trans('admin/departments/message.update.success')));
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $department->getErrors()));
|
|
|
|
}
|
|
|
|
|
2017-05-23 02:45:51 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-31 14:31:57 -07:00
|
|
|
* Validates and deletes selected department.
|
2017-05-23 02:45:51 -07:00
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $locationId
|
2018-10-31 14:31:57 -07:00
|
|
|
* @since [v4.0]
|
2017-05-23 02:45:51 -07:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2017-08-22 14:15:13 -07:00
|
|
|
$department = Department::findOrFail($id);
|
2017-05-23 02:45:51 -07:00
|
|
|
|
2018-07-12 18:28:02 -07:00
|
|
|
$this->authorize('delete', $department);
|
|
|
|
|
2017-05-23 02:45:51 -07:00
|
|
|
if ($department->users->count() > 0) {
|
2017-08-22 14:15:13 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/departments/message.assoc_users')));
|
2017-05-23 02:45:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$department->delete();
|
2017-08-22 14:15:13 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/departments/message.delete.success')));
|
2017-05-23 02:45:51 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-28 09:21:39 -07:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
|
|
|
|
$departments = Department::select([
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'image',
|
|
|
|
]);
|
|
|
|
|
2018-07-24 22:51:31 -07:00
|
|
|
if ($request->filled('search')) {
|
2017-10-28 09:21:39 -07:00
|
|
|
$departments = $departments->where('name', 'LIKE', '%'.$request->get('search').'%');
|
|
|
|
}
|
|
|
|
|
|
|
|
$departments = $departments->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 ($departments as $department) {
|
2018-09-29 21:33:52 -07:00
|
|
|
$department->use_image = ($department->image) ? Storage::disk('public')->url('departments/'.$department->image, $department->image) : null;
|
2017-10-28 09:21:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return (new SelectlistTransformer)->transformSelectlist($departments);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-22 21:32:33 -07:00
|
|
|
}
|