From 541a5e6776dfd362e98b6e794c14e911aa84099d Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 1 Feb 2017 18:50:28 -0800 Subject: [PATCH] Groups API starter Edit/Create still broken for some reason --- .../Api/DepreciationsController.php | 92 ------------- app/Http/Controllers/Api/GroupsController.php | 121 ++++++++++++++++++ .../Controllers/DepreciationsController.php | 41 ------ app/Http/Controllers/GroupsController.php | 72 +---------- app/Http/Transformers/GroupsTransformer.php | 35 +++++ resources/views/groups/edit.blade.php | 10 +- resources/views/groups/index.blade.php | 8 +- .../views/partials/bootstrap-table.blade.php | 3 +- routes/api.php | 14 ++ routes/web.php | 25 ++-- 10 files changed, 198 insertions(+), 223 deletions(-) create mode 100644 app/Http/Controllers/Api/GroupsController.php create mode 100644 app/Http/Transformers/GroupsTransformer.php diff --git a/app/Http/Controllers/Api/DepreciationsController.php b/app/Http/Controllers/Api/DepreciationsController.php index 88af8c61b8..30ef61bc67 100644 --- a/app/Http/Controllers/Api/DepreciationsController.php +++ b/app/Http/Controllers/Api/DepreciationsController.php @@ -120,96 +120,4 @@ class DepreciationsController extends Controller - /** - * Show a count of assets by status label for pie chart - * - * @author [A. Gianotto] [] - * @since [v3.0] - * @return \Illuminate\Http\Response - */ - - public function getAssetCountByDepreciation() - { - - $statusLabels = Depreciation::with('assets')->get(); - $labels=[]; - $points=[]; - $colors=[]; - foreach ($statusLabels as $statusLabel) { - if ($statusLabel->assets()->count() > 0) { - $labels[]=$statusLabel->name; - $points[]=$statusLabel->assets()->whereNull('assigned_to')->count(); - if ($statusLabel->color!='') { - $colors[]=$statusLabel->color; - } - } - } - $labels[]='Deployed'; - $points[]=Asset::whereNotNull('assigned_to')->count(); - - $colors_array = array_merge($colors, Helper::chartColors()); - - $result= [ - "labels" => $labels, - "datasets" => [ [ - "data" => $points, - "backgroundColor" => $colors_array, - "hoverBackgroundColor" => $colors_array - ]] - ]; - return $result; - } - - /** - * Display the specified resource. - * - * @author [A. Gianotto] [] - * @since [v4.0] - * @param int $id - * @return \Illuminate\Http\Response - */ - public function assets(Request $request, $id) - { - $this->authorize('view', Depreciation::class); - $this->authorize('index', Asset::class); - $assets = Asset::where('status_id','=',$id); - - $allowed_columns = [ - 'id', - 'name' - ]; - - $offset = request('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'; - $assets->orderBy($sort, $order); - - $total = $assets->count(); - $assets = $assets->skip($offset)->take($limit)->get(); - - - return (new AssetsTransformer)->transformAssets($assets, $total); - } - - - /** - * Returns a boolean response based on whether the status label - * is one that is deployable. - * - * This is used by the hardware create/edit view to determine whether - * we should provide a dropdown of users for them to check the asset out to. - * - * @author [A. Gianotto] [] - * @since [v4.0] - * @return Bool - */ - public function checkIfDeployable($id) { - $depreciation = Depreciation::findOrFail($id); - if ($depreciation->getDepreciationType()=='deployable') { - return '1'; - } - - return '0'; - } } diff --git a/app/Http/Controllers/Api/GroupsController.php b/app/Http/Controllers/Api/GroupsController.php new file mode 100644 index 0000000000..cde7b21e9b --- /dev/null +++ b/app/Http/Controllers/Api/GroupsController.php @@ -0,0 +1,121 @@ +] + * @since [v4.0] + * @return \Illuminate\Http\Response + */ + public function index(Request $request) + { + $this->authorize('view', Group::class); + $allowed_columns = ['id','name','created_at']; + + $groups = Group::select('id','name','permissions')->withCount('users'); + + if ($request->has('search')) { + $groups = $groups->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'; + $groups->orderBy($sort, $order); + + $total = $groups->count(); + $groups = $groups->skip($offset)->take($limit)->get(); + return (new GroupsTransformer)->transformGroups($groups, $total); + } + + + /** + * Store a newly created resource in storage. + * + * @author [A. Gianotto] [] + * @since [v4.0] + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function store(Request $request) + { + $this->authorize('create', Group::class); + $group = new Group; + $group->fill($request->all()); + + if ($group->save()) { + return response()->json(Helper::formatStandardApiResponse('success', $group, trans('admin/groups/message.create.success'))); + } + return response()->json(Helper::formatStandardApiResponse('error', null, $group->getErrors())); + + } + + /** + * Display the specified resource. + * + * @author [A. Gianotto] [] + * @since [v4.0] + * @param int $id + * @return \Illuminate\Http\Response + */ + public function show($id) + { + $this->authorize('view', Group::class); + $group = Group::findOrFail($id); + return $group; + } + + + /** + * Update the specified resource in storage. + * + * @author [A. Gianotto] [] + * @since [v4.0] + * @param \Illuminate\Http\Request $request + * @param int $id + * @return \Illuminate\Http\Response + */ + public function update(Request $request, $id) + { + $this->authorize('edit', Group::class); + $group = Group::findOrFail($id); + $group->fill($request->all()); + + if ($group->save()) { + return response()->json(Helper::formatStandardApiResponse('success', $group, trans('admin/groups/message.update.success'))); + } + + return response()->json(Helper::formatStandardApiResponse('error', null, $group->getErrors())); + } + + /** + * Remove the specified resource from storage. + * + * @author [A. Gianotto] [] + * @since [v4.0] + * @param int $id + * @return \Illuminate\Http\Response + */ + public function destroy($id) + { + $this->authorize('delete', Group::class); + $group = Group::findOrFail($id); + $this->authorize('delete', $group); + $group->delete(); + return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/groups/message.delete.success'))); + + } + + +} diff --git a/app/Http/Controllers/DepreciationsController.php b/app/Http/Controllers/DepreciationsController.php index d3bad01fb9..133592fb08 100755 --- a/app/Http/Controllers/DepreciationsController.php +++ b/app/Http/Controllers/DepreciationsController.php @@ -156,45 +156,4 @@ class DepreciationsController extends Controller } - /** - * Generates the JSON used to display the depreciation listing. - * - * @see DepreciationsController::getIndex() - * @author [A. Gianotto] [] - * @param Request $request - * @return String JSON - * @internal param string $status - * @since [v1.2] - */ - public function getDatatable(Request $request) - { - $depreciations = Depreciation::select(array('id','name','months')); - - if ($request->has('search')) { - $depreciations = $depreciations->TextSearch(e($request->input('search'))); - } - - $offset = request('offset', 0); - $limit = request('limit', 50); - - $allowed_columns = ['id','name','months']; - $order = $request->input('order') === 'asc' ? 'asc' : 'desc'; - $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; - - $depreciations->orderBy($sort, $order); - - $depreciationsCount = $depreciations->count(); - $depreciations = $depreciations->skip($offset)->take($limit)->get(); - - $rows = array(); - - foreach ($depreciations as $depreciation) { - $rows[] = $depreciation->present()->forDataTable(); - } - - $data = array('total' => $depreciationsCount, 'rows' => $rows); - - return $data; - - } } diff --git a/app/Http/Controllers/GroupsController.php b/app/Http/Controllers/GroupsController.php index 0afd063010..7c8abcfaa8 100755 --- a/app/Http/Controllers/GroupsController.php +++ b/app/Http/Controllers/GroupsController.php @@ -64,13 +64,14 @@ class GroupsController extends Controller */ public function postCreate() { + exit; // create a new group instance $group = new Group(); $group->name = e(Input::get('name')); $group->permissions = json_encode(Input::get('permission')); if ($group->save()) { - return redirect()->to("admin/groups")->with('success', trans('admin/groups/message.success.create')); + return redirect()->route("groups.index")->with('success', trans('admin/groups/message.success.create')); } return redirect()->back()->withInput()->withErrors($group->getErrors()); } @@ -142,73 +143,4 @@ class GroupsController extends Controller return redirect()->route('groups')->with('error', trans('general.feature_disabled')); } - - /** - * Generates the JSON used to display the User Group listing. - * - * @author [A. Gianotto] [] - * @since [v2.0] - * @return String JSON - */ - public function getDatatable() - { - - $offset = request('offset', 0); - $limit = request('limit', 50); - - if (Input::get('sort')=='name') { - $sort = 'first_name'; - } else { - $sort = e(Input::get('sort')); - } - - // Grab all the groups - $groups = Group::with('users')->orderBy('name', 'ASC'); - - if (Input::has('search')) { - $groups = $users->TextSearch(e(Input::get('search'))); - } - - $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; - - $allowed_columns = [ - 'name','created_at' - ]; - - $sort = in_array($sort, $allowed_columns) ? $sort : 'name'; - $groups = $groups->orderBy($sort, $order); - - $groupsCount = $groups->count(); - $groups = $groups->skip($offset)->take($limit)->get(); - $rows = array(); - - foreach ($groups as $group) { - $actions = ''; - $actions .= Helper::generateDatatableButton('edit', route('update/group', $group->id)); - - if (!config('app.lock_passwords')) { - $actions .= Helper::generateDatatableButton( - 'delete', - route('delete/group', $group->id), - true, /*enabled*/ - trans('admin/groups/message.delete.confirm'), - $group->name - ); - } else { - $actions .= ' '; - } - - $actions .= ''; - - $rows[] = array( - 'id' => $group->id, - 'name' => $group->name, - 'users' => $group->users->count(), - 'created_at' => $group->created_at->format('Y-m-d'), - 'actions' => ($actions) ? $actions : '', - ); - } - $data = array('total'=>$groupsCount, 'rows'=>$rows); - return $data; - } } diff --git a/app/Http/Transformers/GroupsTransformer.php b/app/Http/Transformers/GroupsTransformer.php new file mode 100644 index 0000000000..f8cd90e2f7 --- /dev/null +++ b/app/Http/Transformers/GroupsTransformer.php @@ -0,0 +1,35 @@ +transformDatatables($array); + } + + public function transformGroup (Group $group) + { + $array = [ + 'id' => e($group->id), + 'name' => e($group->name), + 'permissions' => ($group->permissions) ? json_decode($group->permissions, true) : null, + 'users_count' => $group->users_count, + 'created_at' => $group->created_at, + 'updated_at' => $group->updated_at, + ]; + + return $array; + } + + + +} diff --git a/resources/views/groups/edit.blade.php b/resources/views/groups/edit.blade.php index ecf91e8ff4..38d137fed1 100755 --- a/resources/views/groups/edit.blade.php +++ b/resources/views/groups/edit.blade.php @@ -3,7 +3,9 @@ 'updateText' => trans('admin/groups/titles.update'), 'helpTitle' => trans('admin/groups/general.about_groups_title'), 'helpText' => trans('admin/groups/general.about_groups_text'), - 'item' => $group + 'item' => $group, + 'formAction' => ($group) ? route('groups.update', ['accessory' => $group->id]) : route('groups.store'), + ]) @section('content')