Groups API starter

Edit/Create still broken for some reason
This commit is contained in:
snipe 2017-02-01 18:50:28 -08:00
parent 464c524375
commit 541a5e6776
10 changed files with 198 additions and 223 deletions

View file

@ -120,96 +120,4 @@ class DepreciationsController extends Controller
/**
* Show a count of assets by status label for pie chart
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @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] [<snipe@snipe.net>]
* @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] [<snipe@snipe.net>]
* @since [v4.0]
* @return Bool
*/
public function checkIfDeployable($id) {
$depreciation = Depreciation::findOrFail($id);
if ($depreciation->getDepreciationType()=='deployable') {
return '1';
}
return '0';
}
}

View file

@ -0,0 +1,121 @@
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Helpers\Helper;
use App\Models\Group;
use App\Http\Transformers\GroupsTransformer;
class GroupsController 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', 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] [<snipe@snipe.net>]
* @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] [<snipe@snipe.net>]
* @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] [<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', 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] [<snipe@snipe.net>]
* @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')));
}
}

View file

@ -156,45 +156,4 @@ class DepreciationsController extends Controller
}
/**
* Generates the JSON used to display the depreciation listing.
*
* @see DepreciationsController::getIndex()
* @author [A. Gianotto] [<snipe@snipe.net>]
* @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;
}
}

View file

@ -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] [<snipe@snipe.net>]
* @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 = '<nobr>';
$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 .= ' <span class="btn delete-asset btn-danger btn-sm disabled"><i class="fa fa-trash icon-white"></i></span>';
}
$actions .= '</nobr>';
$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;
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace App\Http\Transformers;
use App\Models\Group;
use Illuminate\Database\Eloquent\Collection;
class GroupsTransformer
{
public function transformGroups (Collection $groups)
{
$array = array();
foreach ($groups as $group) {
$array[] = self::transformGroup($group);
}
return (new DatatablesTransformer)->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;
}
}

View file

@ -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')
<style>
@ -41,11 +43,13 @@
<div class="form-group" style="padding-left: 15px;">
<label class="radio-padding">
{{ Form::radio('permission['.$permission_name.']', 1,
(array_key_exists($permission_name, $groupPermissions) && $groupPermissions[$permission_name]), ['class' => 'minimal']) }}
(is_array($groupPermissions))
&& (array_key_exists($permission_name, $groupPermissions)
&& $groupPermissions[$permission_name]), ['class' => 'minimal']) }}
Grant
</label>
<label class="radio-padding">
{{ Form::radio('permission['.$permission_name.']', 0, (!array_key_exists($permission_name, $groupPermissions) || !$groupPermissions[$permission_name]), ['class' => 'minimal']) }}
{{ Form::radio('permission['.$permission_name.']', 0, ((is_array($groupPermissions) && !array_key_exists($permission_name, $groupPermissions)) || !$groupPermissions[$permission_name]), ['class' => 'minimal']) }}
Deny
</label>
</div>

View file

@ -7,7 +7,7 @@
@stop
@section('header_right')
<a href="{{ route('create/group') }}" class="btn btn-primary pull-right"> {{ trans('general.create') }}</a>
<a href="{{ route('groups.create') }}" class="btn btn-primary pull-right"> {{ trans('general.create') }}</a>
@stop
@ -31,9 +31,9 @@
<tr>
<th data-switchable="true" data-sortable="false" data-field="id" data-visible="false">{{ trans('general.id') }}</th>
<th data-switchable="true" data-sortable="true" data-field="name" data-visible="true">{{ trans('admin/groups/table.name') }}</th>
<th data-switchable="true" data-sortable="false" data-field="users" data-visible="true">{{ trans('admin/groups/table.users') }}</th>
<th data-switchable="true" data-sortable="true" data-field="created_at" data-visible="true">{{ trans('general.created_at') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions" >{{ trans('table.actions') }}</th>
<th data-switchable="true" data-sortable="false" data-field="users_count" data-visible="true">{{ trans('admin/groups/table.users') }}</th>
<th data-switchable="true" data-sortable="true" data-field="created_at" data-visible="true" data-formatter="createdAtFormatter">{{ trans('general.created_at') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions" data-formatter="groupsActionsFormatter">{{ trans('table.actions') }}</th>
</tr>
</thead>
</table>

View file

@ -133,7 +133,8 @@ $('.snipe-table').bootstrapTable({
'suppliers',
'companies',
'depreciations',
'fieldsets'
'fieldsets',
'groups'
];
for (var i in formatters) {

View file

@ -156,6 +156,20 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
);
Route::resource('groups', 'GroupsController',
['names' =>
[
'index' => 'api.groups.index',
'create' => 'api.groups.create',
'store' => 'api.groups.store',
'destroy' => 'api.groups.destroy'
],
'parameters' =>
['group' => 'group_id']
]
);
Route::resource('depreciations', 'DepreciationsController',
['names' =>
[

View file

@ -148,24 +148,25 @@ Route::group([ 'prefix' => 'admin','middleware' => ['web','auth']], function ()
});
# Group Management
Route::group([ 'prefix' => 'groups', 'middleware' => ['web','auth','authorize:superadmin'] ], function () {
Route::get('/', [ 'as' => 'groups', 'uses' => 'GroupsController@getIndex' ]);
Route::get('create', [ 'as' => 'create/group', 'uses' => 'GroupsController@getCreate' ]);
Route::post('create', 'GroupsController@postCreate');
Route::get('{groupId}/edit', [ 'as' => 'update/group', 'uses' => 'GroupsController@getEdit' ]);
Route::post('{groupId}/edit', 'GroupsController@postEdit');
Route::get('{groupId}/delete', [ 'as' => 'delete/group', 'uses' => 'GroupsController@getDelete' ]);
Route::get('{groupId}/restore', [ 'as' => 'restore/group', 'uses' => 'GroupsController@getRestore' ]);
Route::get('{groupId}/view', [ 'as' => 'view/group', 'uses' => 'GroupsController@getView' ]);
});
# Dashboard
Route::get('/', [ 'as' => 'admin', 'uses' => 'DashboardController@getIndex' ]);
});
# Group Management
Route::group([ 'prefix' => 'groups', 'middleware' => ['web','auth','authorize:superadmin'] ], function () {
Route::get('/', [ 'as' => 'groups', 'uses' => 'GroupsController@getIndex' ]);
Route::get('create', [ 'as' => 'groups.create', 'uses' => 'GroupsController@getCreate']);
Route::post('create', [ 'as' => 'groups.store', 'uses' => 'GroupsController@postCreate' ]);
Route::get('{groupId}/edit', [ 'as' => 'groups.edit', 'uses' => 'GroupsController@getEdit' ]);
Route::post('{groupId}/edit', [ 'as' => 'groups.update', 'uses' => 'GroupsController@postEdit' ]);
Route::get('{groupId}/delete', [ 'as' => 'groups.delete', 'uses' => 'GroupsController@getDelete' ]);
});
/*
|--------------------------------------------------------------------------
| Account Routes