A free open source IT asset/license management system
Department controllers and transformer
snipe659e60fd12e6
diff --git a/app/Http/Controllers/Api/DepartmentsController.php b/app/Http/Controllers/Api/DepartmentsController.phpnew file mode 100644index 0000000000..8d82fdd107--- /dev/null+++ b/app/Http/Controllers/Api/DepartmentsController.php@@ -0,0 +1,45 @@+<?php++namespace App\Http\Controllers\Api;++use Illuminate\Http\Request;+use App\Http\Controllers\Controller;+use App\Models\Department;+use App\Http\Transformers\DepartmentsTransformer;++class DepartmentsController extends Controller+{+ /**+ * Display a listing of the resource.+ *+ * @author [A. Gianotto] [<[email protected]>]+ * @since [v4.0]+ * @return \Illuminate\Http\Response+ */+ public function index(Request $request)+ {+ $this->authorize('view', Department::class);+ $allowed_columns = ['id','name'];++ $departments = Department::select([+ 'id',+ 'name',+ 'location_id'+ ])->with('users')->withCount('assets')->withCount('users');++ if ($request->has('search')) {+ $departments = $departments->TextSearch($request->input('search'));+ }++ $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';+ $departments->orderBy($sort, $order);++ $total = $departments->count();+ $departments = $departments->skip($offset)->take($limit)->get();+ return (new DepartmentsTransformer)->transformDepartments($departments, $total);++ }+}diff --git a/app/Http/Controllers/DepartmentsController.php b/app/Http/Controllers/DepartmentsController.phpnew file mode 100644index 0000000000..4d24bc7e51--- /dev/null+++ b/app/Http/Controllers/DepartmentsController.php@@ -0,0 +1,58 @@+<?php++namespace App\Http\Controllers;++use Illuminate\Http\Request;+use App\Models\Department;++class DepartmentsController extends Controller+{+ public function __construct()+ {+ $this->middleware('auth');+ parent::__construct();+ }++ /**+ * Returns a view that invokes the ajax tables which actually contains+ * the content for the assets listing, which is generated in getDatatable.+ *+ * @author [A. Gianotto] [<[email protected]>]+ * @see AssetController::getDatatable() method that generates the JSON response+ * @since [v1.0]+ * @return View+ */+ public function index(Request $request)+ {+ $this->authorize('index', Department::class);+ if ($request->has('company_id')) {+ $company = Company::find($request->input('company_id'));+ } else {+ $company = null;+ }+ return view('departments/index')->with('company',$company);++ }+++ /**+ * Store a newly created resource in storage.+ *+ * @author [A. Gianotto] [<[email protected]>]+ * @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());++ if ($department->save()) {+ return response()->json(Helper::formatStandardApiResponse('success', $department, trans('admin/department/message.create.success')));+ }+ return response()->json(Helper::formatStandardApiResponse('error', null, $department->getErrors()));++ }+}diff --git a/app/Http/Transformers/DepartmentsTranformer.php b/app/Http/Transformers/DepartmentsTranformer.phpnew file mode 100644index 0000000000..17141628d7--- /dev/null+++ b/app/Http/Transformers/DepartmentsTranformer.php@@ -0,0 +1,66 @@+<?php+namespace App\Http\Transformers;++use App\Models\Department;+use Illuminate\Database\Eloquent\Collection;+use Gate;+use App\Helpers\Helper;++class DepartmentsTransformer+{++ public function transformDepartments (Collection $locations, $total)+ {+ $array = array();+ foreach ($locations as $location) {+ $array[] = self::transformDepartment($location);+ }+ return (new DatatablesTransformer)->transformDatatables($array, $total);+ }++ public function transformDepartment (Department $location = null)+ {+ if ($location) {++ $assets_arr = [];+ foreach($location->assets() as $asset) {+ $assets_arr = ['id' => $asset->id];+ }++ $children_arr = [];+ foreach($location->childDepartments() as $child) {+ $children_arr = ['id' => $child->id];+ }++ $array = [+ 'id' => e($location->id),+ 'name' => e($location->name),+ 'address' => e($location->address),+ 'city' => e($location->city),+ 'state' => e($location->state),+ 'assets_checkedout' => $location->assets()->count(),+ 'assets_default' => $location->assignedassets()->count(),+ 'country' => e($location->country),+ 'assets' => $assets_arr,+ 'created_at' => Helper::getFormattedDateObject($location->created_at, 'datetime'),+ 'updated_at' => Helper::getFormattedDateObject($location->updated_at, 'datetime'),+ 'parent_id' => e($location->parent_id),+ 'children' => $children_arr,+ ];++ $permissions_array['available_actions'] = [+ 'update' => Gate::allows('update', Department::class) ? true : false,+ 'delete' => Gate::allows('delete', Department::class) ? true : false,+ ];++ $array += $permissions_array;++ return $array;+ }+++ }++++}