mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-02-20 22:10:24 +08:00
Added pagination, sorting & searching to users list
As requested on #113
This commit is contained in:
parent
23ab1f0c81
commit
be517de7dc
@ -31,14 +31,21 @@ class UserController extends Controller
|
||||
|
||||
/**
|
||||
* Display a listing of the users.
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->checkPermission('users-manage');
|
||||
$users = $this->userRepo->getAllUsers();
|
||||
$listDetails = [
|
||||
'order' => $request->has('order') ? $request->get('order') : 'asc',
|
||||
'search' => $request->has('search') ? $request->get('search') : '',
|
||||
'sort' => $request->has('sort') ? $request->get('sort') : 'name',
|
||||
];
|
||||
$users = $this->userRepo->getAllUsersPaginatedAndSorted(20, $listDetails);
|
||||
$this->setPageTitle('Users');
|
||||
return view('users/index', ['users' => $users]);
|
||||
$users->appends($listDetails);
|
||||
return view('users/index', ['users' => $users, 'listDetails' => $listDetails]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,6 +51,27 @@ class UserRepo
|
||||
return $this->user->with('roles', 'avatar')->orderBy('name', 'asc')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the users with their permissions in a paginated format.
|
||||
* @param int $count
|
||||
* @param $sortData
|
||||
* @return \Illuminate\Database\Eloquent\Builder|static
|
||||
*/
|
||||
public function getAllUsersPaginatedAndSorted($count = 20, $sortData)
|
||||
{
|
||||
$query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']);
|
||||
|
||||
if ($sortData['search']) {
|
||||
$term = '%' . $sortData['search'] . '%';
|
||||
$query->where(function($query) use ($term) {
|
||||
$query->where('name', 'like', $term)
|
||||
->orWhere('email', 'like', $term);
|
||||
});
|
||||
}
|
||||
|
||||
return $query->paginate($count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new user and attaches a role to them.
|
||||
* @param array $data
|
||||
|
@ -59,3 +59,35 @@ function setting($key, $default = false)
|
||||
$settingService = app('BookStack\Services\SettingService');
|
||||
return $settingService->get($key, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a url with multiple parameters for sorting purposes.
|
||||
* Works out the logic to set the correct sorting direction
|
||||
* Discards empty parameters and allows overriding.
|
||||
* @param $path
|
||||
* @param array $data
|
||||
* @param array $overrideData
|
||||
* @return string
|
||||
*/
|
||||
function sortUrl($path, $data, $overrideData = [])
|
||||
{
|
||||
$queryStringSections = [];
|
||||
$queryData = array_merge($data, $overrideData);
|
||||
|
||||
// Change sorting direction is already sorted on current attribute
|
||||
if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
|
||||
$queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
|
||||
} else {
|
||||
$queryData['order'] = 'asc';
|
||||
}
|
||||
|
||||
foreach ($queryData as $name => $value) {
|
||||
$trimmedVal = trim($value);
|
||||
if ($trimmedVal === '') continue;
|
||||
$queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
|
||||
}
|
||||
|
||||
if (count($queryStringSections) === 0) return $path;
|
||||
|
||||
return $path . '?' . implode('&', $queryStringSections);
|
||||
}
|
@ -266,6 +266,7 @@ ul.pagination {
|
||||
display: inline-block;
|
||||
list-style: none;
|
||||
margin: $-m 0;
|
||||
padding-left: 1px;
|
||||
li {
|
||||
float: left;
|
||||
}
|
||||
@ -300,6 +301,10 @@ ul.pagination {
|
||||
}
|
||||
}
|
||||
|
||||
.compact ul.pagination {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.entity-list {
|
||||
>div {
|
||||
padding: $-m 0;
|
||||
|
@ -297,6 +297,12 @@ span.sep {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.action-header {
|
||||
h1 {
|
||||
margin-top: $-m;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Icons
|
||||
*/
|
||||
|
@ -6,11 +6,15 @@
|
||||
|
||||
<div class="container small">
|
||||
|
||||
<h1>User Roles</h1>
|
||||
|
||||
<p>
|
||||
<a href="/settings/roles/new" class="text-pos"><i class="zmdi zmdi-lock-open"></i>Add new role</a>
|
||||
</p>
|
||||
<div class="row action-header">
|
||||
<div class="col-sm-8">
|
||||
<h1>User Roles</h1>
|
||||
</div>
|
||||
<div class="col-sm-4">
|
||||
<p></p>
|
||||
<a href="/settings/roles/new" class="button float right pos"><i class="zmdi zmdi-lock-open"></i>Add new role</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<table class="table">
|
||||
<tr>
|
||||
|
@ -7,17 +7,42 @@
|
||||
|
||||
|
||||
<div class="container small" ng-non-bindable>
|
||||
<h1>Users</h1>
|
||||
@if(userCan('users-manage'))
|
||||
<p>
|
||||
<a href="/settings/users/create" class="text-pos"><i class="zmdi zmdi-account-add"></i>Add new user</a>
|
||||
</p>
|
||||
@endif
|
||||
<div class="row action-header">
|
||||
<div class="col-sm-8">
|
||||
<h1>Users</h1>
|
||||
</div>
|
||||
<div class="col-sm-4">
|
||||
<p></p>
|
||||
@if(userCan('users-manage'))
|
||||
<a href="/settings/users/create" class="pos button float right"><i class="zmdi zmdi-account-add"></i>Add new user</a>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-sm-8">
|
||||
<div class="compact">
|
||||
{!! $users->links() !!}
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-4">
|
||||
<form method="get" class="float right" action="/settings/users">
|
||||
@foreach(collect($listDetails)->except('search') as $name => $val)
|
||||
<input type="hidden" name="{{$name}}" value="{{$val}}">
|
||||
@endforeach
|
||||
<input type="text" name="search" placeholder="Search Users" @if($listDetails['search']) value="{{$listDetails['search']}}" @endif>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-center">
|
||||
|
||||
</div>
|
||||
|
||||
<table class="table">
|
||||
<tr>
|
||||
<th></th>
|
||||
<th>Name</th>
|
||||
<th>Email</th>
|
||||
<th><a href="{{ sortUrl('/settings/users', $listDetails, ['sort' => 'name']) }}">Name</a></th>
|
||||
<th><a href="{{ sortUrl('/settings/users', $listDetails, ['sort' => 'email']) }}">Email</a></th>
|
||||
<th>User Roles</th>
|
||||
</tr>
|
||||
@foreach($users as $user)
|
||||
@ -42,11 +67,17 @@
|
||||
@endif
|
||||
</td>
|
||||
<td>
|
||||
<small> {{ $user->roles->implode('display_name', ', ') }}</small>
|
||||
@foreach($user->roles as $index => $role)
|
||||
<small><a href="/settings/roles/{{$role->id}}">{{$role->display_name}}</a>@if($index !== count($user->roles) -1),@endif</small>
|
||||
@endforeach
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</table>
|
||||
|
||||
<div>
|
||||
{!! $users->links() !!}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@stop
|
||||
|
Loading…
x
Reference in New Issue
Block a user