Skip to content

Commit

Permalink
Server side datatables for user view - needs fix for deleted users
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed Mar 17, 2015
1 parent 77fd73b commit 46c1f2a
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 152 deletions.
148 changes: 85 additions & 63 deletions app/controllers/admin/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Sentry;
use Validator;
use View;
use Chumper\Datatable\Facades\Datatable;
use Datatable;
use League\Csv\Reader;
use Mail;

Expand All @@ -47,41 +47,9 @@ class UsersController extends AdminController
*/
public function getIndex()
{
// Grab all the users - depending on the scope to include
$users = Sentry::getUserProvider()->createModel();

// Do we want to include the deleted users?
// the with and onlyTrashed calls currently do not work - returns an
// inconsistent array which cannot be displayed by the blade output

if (Input::get('withTrashed')) {

$users = $users->withTrashed();
//$users = Sentry::getUserProvider()->createModel()->paginate();

} elseif (Input::get('onlyTrashed')) {

// this is a tempoary 'fix' to display NO deleted users.
//$users = Sentry::getUserProvider()->createModel()->whereNotNull('deleted_at')->paginate();
//$users = Sentry::findAllUsers();
//$users = users::deletedUsers()->paginate();
$users = Sentry::getUserProvider()->createModel()->onlyTrashed();
//$users = users::whereNotNull('deleted_at')->paginate();
//$users = $users->onlyTrashed();
//$users = Users::onlyTrashed()->get();
//$users = Sentry::getUserProvider()->createModel();
}


// Paginate the users
$users = $users->paginate(100000)
->appends(array(
'withTrashed' => Input::get('withTrashed'),
'onlyTrashed' => Input::get('onlyTrashed'),
));


// Show the page
return View::make('backend/users/index', compact('users'));
return View::make('backend/users/index');
}

/**
Expand Down Expand Up @@ -490,34 +458,7 @@ public function getView($userId = null)

}

public function getDatatable()
{
return Datatable::collection(User::all())
->addColumn('name',function ($model) {
$name = HTML::image($model->gravatar(), $model->first_name, array('class'=>'img-circle avatar hidden-phone', 'style'=>'max-width: 45px'));
$name .= HTML::link(URL::action('Controllers\Admin\UsersController@getView', $model->id), $model->first_name . ' ' . $model->last_name, array('class' => 'name'));
return $name;
}
)
->showColumns('email')
->addColumn('assets', function ($model) {
$assets = $model->assets->count();
return $assets;
}
)
->addColumn('licenses', function ($model) {
$licenses = $model->licenses->count();
return $licenses;
}
)
->addColumn('activated', function ($model) {
$activated = $model->isActivated() ? '<i class="fa fa-check"></i>' : '';
return $activated;
}
)
->make();
}


/**
* Unsuspend the given user.
*
Expand Down Expand Up @@ -733,6 +674,87 @@ public function postImport()

}


public function getDatatable($status = null)
{

$users = User::with('assets');

switch ($status) {
case 'deleted':
$users->GetDeleted();
break;
case '':
$users->GetNotDeleted();
break;
}

$users = $users->orderBy('created_at', 'DESC')->get();

$actions = new \Chumper\Datatable\Columns\FunctionColumn('actions', function ($users)
{
$action_buttons = '';


if ( ! is_null($users->deleted_at)) {
$action_buttons .= '<a href="'.route('restore/user', $users->id).'" class="btn btn-warning btn-sm"><i class="fa fa-share icon-white"></i></a> ';
} else {
if ($users->accountStatus()=='suspended') {
$action_buttons .= '<a href="'.route('unsuspend/user', $users->id).'" class="btn btn-warning btn-sm"><span class="fa fa-time icon-white"></span></a> ';
}

$action_buttons .= '<a href="'.route('update/user', $users->id).'" class="btn btn-warning btn-sm"><i class="fa fa-pencil icon-white"></i></a> ';

if ((Sentry::getId() !== $users->id) && (!Config::get('app.lock_passwords'))) {
$action_buttons .= '<a data-html="false" class="btn delete-asset btn-danger btn-sm" data-toggle="modal" href="'.route('delete/user', $users->id).'" data-content="Are you sure you wish to delete this user?" data-title="Delete '.htmlspecialchars($users->first_name).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a> ';
} else {
$action_buttons .= ' <span class="btn delete-asset btn-danger btn-sm disabled"><i class="fa fa-trash icon-white"></i></span>';
}
}
return $action_buttons;

});


return Datatable::collection($users)
->addColumn('name',function($users)
{
return '<a title="'.$users->fullName().'" href="users/'.$users->id.'/view">'.$users->fullName().'</a>';
})

->addColumn('email',function($users)
{
return '<a title="'.$users->email.'" href="mailto:'.$users->email.'">'.$users->email.'</a>';
})

->addColumn('manager',function($users)
{
if ($users->manager) {
return '<a title="'.$users->manager->fullName().'" href="users/'.$users->manager->id.'/view">'.$users->manager->fullName().'</a>';
}
})

->addColumn('assets',function($users)
{
return $users->assets->count();
})

->addColumn('licenses',function($users)
{
return $users->licenses->count();
})
->addColumn('activated',function($users)
{
return $users->isActivated() ? '<i class="fa fa-check"></i>' : '';
})

->addColumn($actions)
->searchColumns('name','email','manager','activated', 'licenses','assets')
->orderColumns('name','email','manager','activated', 'licenses','assets')
->make();

}




Expand Down
13 changes: 13 additions & 0 deletions app/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function fullName()
{
return "{$this->first_name} {$this->last_name}";
}


/**
* Returns the user Gravatar image url.
Expand Down Expand Up @@ -89,4 +90,16 @@ public function accountStatus()
return '';
}
}

public function scopeGetDeleted($query)
{
return $query->whereNotNull('deleted_at');
}

public function scopeGetNotDeleted($query)
{
return $query->whereNull('deleted_at');
}


}
8 changes: 4 additions & 4 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@

# User Management
Route::group(array('prefix' => 'users'), function () {
Route::get('/', array('as' => 'users', 'uses' => 'UsersController@getIndex'));

Route::get('create', array('as' => 'create/user', 'uses' => 'UsersController@getCreate'));
Route::post('create', 'UsersController@postCreate');
Route::get('import', array('as' => 'import/user', 'uses' => 'UsersController@getImport'));
Expand All @@ -225,9 +225,9 @@
Route::get('{userId}/restore', array('as' => 'restore/user', 'uses' => 'UsersController@getRestore'));
Route::get('{userId}/view', array('as' => 'view/user', 'uses' => 'UsersController@getView'));
Route::get('{userId}/unsuspend', array('as' => 'unsuspend/user', 'uses' => 'UsersController@getUnsuspend'));


Route::get('datatable', array('as' => 'api.users', 'uses' => 'UsersController@getDatatable'));
Route::get('api/users/{status?}', array('as'=>'api.users', 'uses'=>'UsersController@getDatatable'));
Route::get('/', array('as' => 'users', 'uses' => 'UsersController@getIndex'));
});

# Group Management
Expand Down
126 changes: 41 additions & 85 deletions app/views/backend/users/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<div class="col-md-12">
<a href="{{ route('import/user') }}" class="btn btn-default pull-right"><span class="fa fa-upload"></span> Import</a>
<a href="{{ route('create/user') }}" class="btn btn-success pull-right"><i class="fa fa-plus icon-white"></i> @lang('general.create')</a>
@if (Input::get('onlyTrashed'))
@if (Input::get('status')=='deleted')
<a class="btn btn-default pull-right" href="{{ URL::to('admin/users') }}" style="margin-right: 5px;">Show Current Users</a>
@else
<a class="btn btn-default pull-right" href="{{ URL::to('admin/users?onlyTrashed=true') }}" style="margin-right: 5px;">Show Deleted Users</a>
<a class="btn btn-default pull-right" href="{{ URL::to('admin/users?status=deleted') }}" style="margin-right: 5px;">Deleted Users</a>
@endif

<h3>
Expand All @@ -32,93 +32,49 @@

<div class="row form-wrapper">

@if ($users->getTotal() > 0)
<div class="row-fluid table users-list">
<div class="table-responsive">
<table id="example">
<thead>
<tr role="row">
<th class="col-md-3">@lang('admin/users/table.name')</th>
<th class="col-md-2">@lang('admin/users/table.email')</th>
<th class="col-md-2">@lang('admin/users/table.manager')</th>
<th class="col-md-1">@lang('general.assets')</th>
<th class="col-md-1">@lang('general.licenses')</th>
<th class="col-md-1">@lang('admin/users/table.activated')</th>
<th class="col-md-2 actions">@lang('table.actions')</th>
</tr>
</thead>
<tbody>

@foreach ($users as $user)
<tr>
<td nowrap="nowrap">
@if ($user->avatar)
<img src="/uploads/avatars/{{{ $user->avatar }}}" class="img-circle avatar hidden-phone" style="max-width: 45px;" />
@else
<img src="{{ $user->gravatar() }}" class="img-circle avatar hidden-phone" style="max-width: 45px;" />
@endif
<a href="{{ route('view/user', $user->id) }}" class="name">{{{ $user->fullName() }}}</a>

</td>
<td>{{{ $user->email }}}</td>
<td>
@if ($user->manager) {{{ $user->manager->fullName() }}}
@endif
</td>
<td>{{{ $user->assets->count() }}}</td>
<td>{{{ $user->licenses->count() }}}</td>
<td>{{ $user->isActivated() ? '<i class="fa fa-check"></i>' : ''}}</td>

<td nowrap="nowrap">

<!-- If the user account is suspended - show the UNSUSPEND button. Do NOT evaluate if soft deleted! -->
@if (is_null($user->deleted_at))
@if ($user->accountStatus()=='suspended')
<a href="{{ route('unsuspend/user', $user->id) }}" class="btn btn-warning btn-sm"><span class="fa fa-time icon-white"></span></a>
@endif
@endif

{{ Datatable::table()
->addColumn(
Lang::get('admin/users/table.name'),
Lang::get('admin/users/table.email'),
Lang::get('admin/users/table.manager'),
Lang::get('general.assets'),
Lang::get('general.licenses'),
Lang::get('admin/users/table.activated'),
Lang::get('table.actions')
)
->setUrl(route('api.users', Input::get('status'))) // this is the route where data will be retrieved
->setOptions(
array(
'deferRender'=> true,
'stateSave'=> true,
'stateDuration'=> -1,
'dom' =>'CT<"clear">lfrtip',
'tableTools' => array(
'sSwfPath'=> Config::get('app.url').'/assets/swf/copy_csv_xls_pdf.swf',
'aButtons'=>array(
'copy',
'print',
array(
'sExtends'=>'collection',
'sButtonText'=>'Export',
'aButtons'=>array(
'csv',
'xls',
'pdf'
)
)
)
),
'colVis'=> array('showAll'=>'Show All','restore'=>'Restore','activate'=>'mouseover'),
'columnDefs'=> array(array('visible'=>false,'targets'=>array()),array('bSortable'=>false,'targets'=>array(6))),
'order'=>array(array(1,'asc')),
)
)
->render() }}

@if ( ! is_null($user->deleted_at))
<a href="{{ route('restore/user', $user->id) }}" class="btn btn-warning btn-sm"><i class="fa fa-share icon-white"></i></a>
@else
<a href="{{ route('update/user', $user->id) }}" class="btn btn-warning btn-sm"><i class="fa fa-pencil icon-white"></i></a>

@if ((Sentry::getId() !== $user->id) && (!Config::get('app.lock_passwords')))
<a data-html="false" class="btn delete-asset btn-danger btn-sm" data-toggle="modal" href="{{ route('delete/user', $user->id) }}" data-content="Are you sure you wish to delete this user?" data-title="Delete {{ htmlspecialchars($user->first_name) }}?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a>

@else
<span class="btn delete-asset btn-danger btn-sm disabled"><i class="fa fa-trash icon-white"></i></span>
@endif
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
<!-- {{
Datatable::table()
->addColumn(Lang::get('name'))
->addColumn(Lang::get('email'))
->addColumn('Assets')
->addColumn('Licenses')
->addColumn(Lang::get('activated'))
->setUrl(route('api.users'))
->render()
}} -->

@else


<div class="col-md-6">
<div class="alert alert-warning alert-block">
<i class="fa fa-warning"></i>
@lang('general.no_results')

</div>
</div>
@endif

@stop

0 comments on commit 46c1f2a

Please sign in to comment.