add AJAX to user listing (Issue #287)
This commit is contained in:
parent
6ba52812c9
commit
e9d9147b28
6 changed files with 190 additions and 117 deletions
|
|
@ -209,6 +209,7 @@
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public static $routes = array(
|
public static $routes = array(
|
||||||
|
array('^users/all/?$', 'users/index/all', true),
|
||||||
array('^users/([^/]+)/(edit|delete)/?$', 'users/$2/$1', true),
|
array('^users/([^/]+)/(edit|delete)/?$', 'users/$2/$1', true),
|
||||||
array('^users/(?!(index|login|register|logout|manage|create|edit|delete))/?', 'users/user/$1', true),
|
array('^users/(?!(index|login|register|logout|manage|create|edit|delete))/?', 'users/user/$1', true),
|
||||||
array('^seminaries/([^/]+)/(edit|delete)/?$', 'seminaries/$2/$1', true),
|
array('^seminaries/([^/]+)/(edit|delete)/?$', 'seminaries/$2/$1', true),
|
||||||
|
|
@ -253,6 +254,7 @@
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public static $reverseRoutes = array(
|
public static $reverseRoutes = array(
|
||||||
|
array('^users/index/all$', 'users/all', true),
|
||||||
array('^users/user/(.*)$', 'users/$1', true),
|
array('^users/user/(.*)$', 'users/$1', true),
|
||||||
array('^users/([^/]+)/(.*)$', 'users/$2/$1', true),
|
array('^users/([^/]+)/(.*)$', 'users/$2/$1', true),
|
||||||
array('^seminaries/seminary/(.*)$', 'seminaries/$1', false),
|
array('^seminaries/seminary/(.*)$', 'seminaries/$1', false),
|
||||||
|
|
|
||||||
|
|
@ -50,10 +50,27 @@
|
||||||
/**
|
/**
|
||||||
* Action: index.
|
* Action: index.
|
||||||
*/
|
*/
|
||||||
public function index()
|
public function index($all=null)
|
||||||
{
|
{
|
||||||
|
// Set filters
|
||||||
|
$sortorder = 'username';
|
||||||
|
$page = 1;
|
||||||
|
if($this->request->getRequestMethod() == 'GET')
|
||||||
|
{
|
||||||
|
$sortorder = $this->request->getGetParam('sortorder');
|
||||||
|
$sortorder = !empty($sortorder) ? $sortorder : 'username';
|
||||||
|
$page = $this->request->getGetParam('page');
|
||||||
|
$page = !empty($page) ? intval($page) : 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Get registered users
|
// Get registered users
|
||||||
$users = $this->Users->getUsers();
|
$limit = ($all != 'all') ? \nre\configs\AppConfig::$misc['lists_limit'] : null;
|
||||||
|
$offset = ($all != 'all') ? max((intval($page) - 1), 0) * $limit : 0;
|
||||||
|
$usersCount = $this->Users->getUsersCount();
|
||||||
|
$users = $this->Users->getUsers($sortorder, $limit, $offset);
|
||||||
|
foreach($users as &$user) {
|
||||||
|
$user['roles'] = array_map(function($r) { return $r['name']; }, $this->Userroles->getUserrolesForUserById($user['id']));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Set titile
|
// Set titile
|
||||||
|
|
@ -61,6 +78,11 @@
|
||||||
|
|
||||||
// Pass data to view
|
// Pass data to view
|
||||||
$this->set('users', $users);
|
$this->set('users', $users);
|
||||||
|
$this->set('usersCount', $usersCount);
|
||||||
|
$this->set('sortorder', $sortorder);
|
||||||
|
$this->set('all', $all);
|
||||||
|
$this->set('page', $page);
|
||||||
|
$this->set('limit', $limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -260,7 +282,8 @@
|
||||||
public function manage()
|
public function manage()
|
||||||
{
|
{
|
||||||
$selectedUsers = array();
|
$selectedUsers = array();
|
||||||
global $sortorder;
|
//global $sortorder;
|
||||||
|
$sortorder = 'username';
|
||||||
|
|
||||||
if($this->request->getRequestMethod() == 'POST')
|
if($this->request->getRequestMethod() == 'POST')
|
||||||
{
|
{
|
||||||
|
|
@ -324,21 +347,11 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get registered users
|
// Get registered users
|
||||||
$users = $this->Users->getUsers();
|
$users = $this->Users->getUsers($sortorder);
|
||||||
foreach($users as &$user) {
|
foreach($users as &$user) {
|
||||||
$user['roles'] = array_map(function($r) { return $r['name']; }, $this->Userroles->getUserrolesForUserById($user['id']));
|
$user['roles'] = array_map(function($r) { return $r['name']; }, $this->Userroles->getUserrolesForUserById($user['id']));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort users
|
|
||||||
$sortorder = (!is_null($sortorder)) ? $sortorder : 'username';
|
|
||||||
$sortMethod = 'sortUsersBy'.ucfirst(strtolower($sortorder));
|
|
||||||
if(method_exists($this, $sortMethod)) {
|
|
||||||
usort($users, array($this, $sortMethod));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new \nre\exceptions\ParamsNotValidException($sortorder);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Set titile
|
// Set titile
|
||||||
$this->addTitleLocalized('Manage users');
|
$this->addTitleLocalized('Manage users');
|
||||||
|
|
@ -582,94 +595,6 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compare two users by their username.
|
|
||||||
*
|
|
||||||
* @param array $a User a
|
|
||||||
* @param array $b User b
|
|
||||||
* @return int Result of comparison
|
|
||||||
*/
|
|
||||||
private function sortUsersByUsername($a, $b)
|
|
||||||
{
|
|
||||||
if($a['username'] == $b['username']) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return ($a['username'] < $b['username']) ? -1 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compare two users by their userroles.
|
|
||||||
*
|
|
||||||
* @param array $a User a
|
|
||||||
* @param array $b User b
|
|
||||||
* @return int Result of comparison
|
|
||||||
*/
|
|
||||||
private function sortUsersByRole($a, $b)
|
|
||||||
{
|
|
||||||
if(in_array('admin', $a['roles']))
|
|
||||||
{
|
|
||||||
if(in_array('admin', $b['roles'])) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if(in_array('moderator', $a['roles']))
|
|
||||||
{
|
|
||||||
if(in_array('admin', $b['roles'])) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if(in_array('moderator', $b['roles'])) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if(in_array('user', $a['roles']))
|
|
||||||
{
|
|
||||||
if(in_array('admin', $b['roles']) || in_array('moderator', $b['roles'])) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if(in_array('user', $b['roles'])) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if(in_array('guest', $a['roles']))
|
|
||||||
{
|
|
||||||
if(in_array('admin', $b['roles']) || in_array('moderator', $b['roles']) || in_array('user', $b['roles'])) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if(in_array('guest', $b['roles'])) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compare two users by their registration date.
|
|
||||||
*
|
|
||||||
* @param array $a User a
|
|
||||||
* @param array $b User b
|
|
||||||
* @return int Result of comparison
|
|
||||||
*/
|
|
||||||
private function sortUsersByDate($a, $b)
|
|
||||||
{
|
|
||||||
if($a['created'] == $b['created']) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return ($a['created'] > $b['created']) ? -1 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
|
|
@ -34,18 +34,71 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get count of registered users.
|
||||||
|
*
|
||||||
|
* @return int Count of users
|
||||||
|
*/
|
||||||
|
public function getUsersCount()
|
||||||
|
{
|
||||||
|
$data = $this->db->query(
|
||||||
|
'SELECT count(DISTINCT id) AS c '.
|
||||||
|
'FROM users '
|
||||||
|
);
|
||||||
|
if(!empty($data)) {
|
||||||
|
return $data[0]['c'];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get registered users.
|
* Get registered users.
|
||||||
*
|
*
|
||||||
|
* @throws ParamsNotValidException
|
||||||
|
* @param string $sort Field to sort by
|
||||||
|
* @param int $limit Limit amount of Characters (optional)
|
||||||
|
* @param int $offset Offset (optional)
|
||||||
* @return array Users
|
* @return array Users
|
||||||
*/
|
*/
|
||||||
public function getUsers()
|
public function getUsers($sort, $limit=null, $offset=0)
|
||||||
{
|
{
|
||||||
|
switch($sort)
|
||||||
|
{
|
||||||
|
case 'username':
|
||||||
|
case 'created':
|
||||||
|
$orders = array(
|
||||||
|
'username' => 'ASC',
|
||||||
|
'created' => 'DESC'
|
||||||
|
);
|
||||||
|
|
||||||
return $this->db->query(
|
return $this->db->query(
|
||||||
|
sprintf(
|
||||||
'SELECT id, created, username, url, surname, prename, email '.
|
'SELECT id, created, username, url, surname, prename, email '.
|
||||||
'FROM users '.
|
'FROM users '.
|
||||||
'ORDER BY username ASC'
|
'ORDER BY %s %s '.
|
||||||
|
(!empty($limit) ? sprintf('LIMIT %d, %d', $offset, $limit) : null),
|
||||||
|
$sort,
|
||||||
|
$orders[$sort]
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
break;
|
||||||
|
case 'role':
|
||||||
|
return $this->db->query(
|
||||||
|
'SELECT DISTINCT users.id, users.created, users.username, users.url, users.surname, users.prename, users.email '.
|
||||||
|
'FROM users '.
|
||||||
|
'LEFT JOIN users_userroles ON users_userroles.user_id = users.id '.
|
||||||
|
'LEFT JOIN userroles ON userroles.id = users_userroles.user_id '.
|
||||||
|
'ORDER BY userroles.id IS NULL, userroles.id ASC '.
|
||||||
|
(!empty($limit) ? sprintf('LIMIT %d, %d', $offset, $limit) : null)
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new \nre\exceptions\ParamsNotValidException($sort);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
27
views/ajax/users/index.tpl
Normal file
27
views/ajax/users/index.tpl
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
foreach($users as &$user)
|
||||||
|
{
|
||||||
|
// Role translation
|
||||||
|
foreach($user['roles'] as &$role)
|
||||||
|
{
|
||||||
|
switch($role)
|
||||||
|
{
|
||||||
|
case 'admin': $role = _('Admin');
|
||||||
|
break;
|
||||||
|
case 'moderator': $role = _('Moderator');
|
||||||
|
break;
|
||||||
|
case 'user': $role = _('User');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Date formattieng
|
||||||
|
$user['created'] = sprintf(_('registered on %s'), $dateFormatter->format(new \DateTime($user['created'])));
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
<?=json_encode(array(
|
||||||
|
'users' => $users,
|
||||||
|
'more' => (($page*$limit) < $usersCount)
|
||||||
|
))?>
|
||||||
|
|
@ -6,8 +6,74 @@
|
||||||
<li><a href="<?=$linker->link('create', 1)?>"><?=_('Create new user')?></a></li>
|
<li><a href="<?=$linker->link('create', 1)?>"><?=_('Create new user')?></a></li>
|
||||||
<li><a href="<?=$linker->link('manage',1)?>"><?=_('Manage')?></a></li>
|
<li><a href="<?=$linker->link('manage',1)?>"><?=_('Manage')?></a></li>
|
||||||
</nav>
|
</nav>
|
||||||
<ol class="cglist">
|
|
||||||
|
<form method="get">
|
||||||
|
<fieldset class="filter">
|
||||||
|
<p><small>Sortierung:</small></p>
|
||||||
|
<select name="sortorder" onchange="this.form.submit();">
|
||||||
|
<option value="username" <?php if($sortorder == 'username') : ?>selected="selected"<?php endif ?>><?=_('Username')?></option>
|
||||||
|
<option value="role" <?php if($sortorder == 'role') : ?>selected="selected"<?php endif ?>><?=_('Role')?></option>
|
||||||
|
<option value="created" <?php if($sortorder == 'created') : ?>selected="selected"<?php endif ?>><?=_('Date of registration')?></option>
|
||||||
|
</select>
|
||||||
|
<noscript><input type="submit" value="<?=_('Sort list')?>" /></noscript>
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
<ol id="userlist" class="cglist">
|
||||||
<?php foreach($users as &$user) : ?>
|
<?php foreach($users as &$user) : ?>
|
||||||
<li><p><a href="<?=$linker->link(array('user', $user['username']), 1)?>"><?=$user['username']?></a><span class="xp"><small><?=sprintf(_('registered on %s'), $dateFormatter->format(new \DateTime($user['created'])))?></small></span></p></li>
|
<li>
|
||||||
|
<p>
|
||||||
|
<a href="<?=$linker->link(array('user', $user['username']), 1)?>"><?=$user['username']?></a><span class="xp"><small><?=sprintf(_('registered on %s'), $dateFormatter->format(new \DateTime($user['created'])))?></small></span>
|
||||||
|
<?php if(in_array('admin', $user['roles'])) : ?><small>(<?=_('Admin')?>)</small><?php endif ?>
|
||||||
|
<?php if(in_array('moderator', $user['roles'])) : ?><small>(<?=_('Moderator')?>)</small><?php endif ?>
|
||||||
|
<?php if(in_array('user', $user['roles'])) : ?><small>(<?=_('User')?>)</small><?php endif ?>
|
||||||
|
</p>
|
||||||
|
</li>
|
||||||
<?php endforeach ?>
|
<?php endforeach ?>
|
||||||
</ol>
|
</ol>
|
||||||
|
|
||||||
|
<?php if(is_null($all) && ($page*$limit) < $usersCount) : ?>
|
||||||
|
<nav class="admin">
|
||||||
|
<li><a id="show-more" href="<?=$linker->link(null,1,true,array('page'=>$page+1,'sortorder'=>$sortorder))?>"><?=_('Show more')?></a></li>
|
||||||
|
<li><a id="show-all" href="<?=$linker->link('all',1,true,array('sortorder'=>$sortorder))?>"><?=_('Show all')?></a></li>
|
||||||
|
</nav>
|
||||||
|
<?php endif ?>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
var page = 1;
|
||||||
|
var request;
|
||||||
|
var linkUser = "<?=$linker->link(array('users','user','USER'))?>";
|
||||||
|
var linkPage = "<?=$linker->link(null,1,true,array('page'=>'PAGE','sortorder'=>$sortorder))?>";
|
||||||
|
|
||||||
|
$("#show-more").click(function(event) {
|
||||||
|
if(request) {
|
||||||
|
request.abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
page++;
|
||||||
|
request = $.getJSON(linkPage.replace('PAGE', page), function(data) {
|
||||||
|
$.each(data['users'], function(key, user) {
|
||||||
|
var urlUser = linkUser.replace('USER', user['url']);
|
||||||
|
$("#userlist").append(
|
||||||
|
"<li><p>" +
|
||||||
|
"<a href=\"" + urlUser + "\">" + user['username'] + "</a><span class=\"xp\"><small>" + user['created'] + "</small></span>\n" +
|
||||||
|
(user['roles'].length > 0 ? "<small>(" + user['roles'].join(', ') + ")</small>" : '') +
|
||||||
|
"</p></li>"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
if(data['more']) {
|
||||||
|
$("#show-more").attr('href', linkPage.replace('PAGE', page+1));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$("#show-more").remove();
|
||||||
|
$("#show-all").remove();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
request.fail(function (jqXHR, textStatus, errorThrown) {
|
||||||
|
window.location.href = linkPage.replace('PAGE', page);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
<select name="sortorder" onchange="this.form.submit();">
|
<select name="sortorder" onchange="this.form.submit();">
|
||||||
<option value="username" <?php if($sortorder == 'username') : ?>selected="selected"<?php endif ?>><?=_('Username')?></option>
|
<option value="username" <?php if($sortorder == 'username') : ?>selected="selected"<?php endif ?>><?=_('Username')?></option>
|
||||||
<option value="role" <?php if($sortorder == 'role') : ?>selected="selected"<?php endif ?>><?=_('Role')?></option>
|
<option value="role" <?php if($sortorder == 'role') : ?>selected="selected"<?php endif ?>><?=_('Role')?></option>
|
||||||
<option value="date" <?php if($sortorder == 'date') : ?>selected="selected"<?php endif ?>><?=_('Date of registration')?></option>
|
<option value="created" <?php if($sortorder == 'created') : ?>selected="selected"<?php endif ?>><?=_('Date of registration')?></option>
|
||||||
</select>
|
</select>
|
||||||
<noscript><input type="submit" value="<?=_('Sort list')?>" /></noscript>
|
<noscript><input type="submit" value="<?=_('Sort list')?>" /></noscript>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue