add username filter for user listing (issue #316)

This commit is contained in:
coderkun 2014-06-24 21:29:59 +02:00
commit dacb967f32
5 changed files with 38 additions and 32 deletions

View file

@ -54,11 +54,13 @@
{ {
// Set filters // Set filters
$sortorder = 'username'; $sortorder = 'username';
$username = null;
$page = 1; $page = 1;
if($this->request->getRequestMethod() == 'GET') if($this->request->getRequestMethod() == 'GET')
{ {
$sortorder = $this->request->getGetParam('sortorder'); $sortorder = $this->request->getGetParam('sortorder');
$sortorder = !empty($sortorder) ? $sortorder : 'username'; $sortorder = !empty($sortorder) ? $sortorder : 'username';
$username = (!is_null($this->request->getGetParam('username'))) ? $this->request->getGetParam('username') : $username;
$page = $this->request->getGetParam('page'); $page = $this->request->getGetParam('page');
$page = !empty($page) ? intval($page) : 1; $page = !empty($page) ? intval($page) : 1;
} }
@ -67,7 +69,7 @@
$limit = ($all != 'all') ? \nre\configs\AppConfig::$misc['lists_limit'] : null; $limit = ($all != 'all') ? \nre\configs\AppConfig::$misc['lists_limit'] : null;
$offset = ($all != 'all') ? max((intval($page) - 1), 0) * $limit : 0; $offset = ($all != 'all') ? max((intval($page) - 1), 0) * $limit : 0;
$usersCount = $this->Users->getUsersCount(); $usersCount = $this->Users->getUsersCount();
$users = $this->Users->getUsers($sortorder, $limit, $offset); $users = $this->Users->getUsers($sortorder, $username, $limit, $offset);
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']));
} }

View file

@ -1,8 +1,8 @@
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: The Legend of Z\n" "Project-Id-Version: The Legend of Z\n"
"POT-Creation-Date: 2014-06-24 20:53+0100\n" "POT-Creation-Date: 2014-06-24 21:29+0100\n"
"PO-Revision-Date: 2014-06-24 20:53+0100\n" "PO-Revision-Date: 2014-06-24 21:29+0100\n"
"Last-Translator: \n" "Last-Translator: \n"
"Language-Team: \n" "Language-Team: \n"
"Language: de_DE\n" "Language: de_DE\n"
@ -18,31 +18,31 @@ msgstr ""
"X-Poedit-SearchPath-2: controllers\n" "X-Poedit-SearchPath-2: controllers\n"
#: controllers/CharactersController.inc:382 #: controllers/CharactersController.inc:382
#: controllers/UsersController.inc:312 views/ajax/characters/index.tpl:10 #: controllers/UsersController.inc:314 views/ajax/characters/index.tpl:10
#: views/ajax/users/index.tpl:10 views/html/characters/index.tpl:39 #: views/ajax/users/index.tpl:10 views/html/characters/index.tpl:39
#: views/html/characters/manage.tpl:35 views/html/characters/manage.tpl:50 #: views/html/characters/manage.tpl:35 views/html/characters/manage.tpl:50
#: views/html/characters/manage.tpl:58 views/html/userroles/user.tpl:5 #: views/html/characters/manage.tpl:58 views/html/userroles/user.tpl:5
#: views/html/users/index.tpl:26 views/html/users/manage.tpl:28 #: views/html/users/index.tpl:30 views/html/users/manage.tpl:28
#: views/html/users/manage.tpl:41 views/html/users/manage.tpl:49 #: views/html/users/manage.tpl:41 views/html/users/manage.tpl:49
msgid "Admin" msgid "Admin"
msgstr "Administrator" msgstr "Administrator"
#: controllers/CharactersController.inc:388 #: controllers/CharactersController.inc:388
#: controllers/UsersController.inc:318 views/ajax/characters/index.tpl:12 #: controllers/UsersController.inc:320 views/ajax/characters/index.tpl:12
#: views/ajax/users/index.tpl:12 views/html/characters/index.tpl:40 #: views/ajax/users/index.tpl:12 views/html/characters/index.tpl:40
#: views/html/characters/manage.tpl:36 views/html/characters/manage.tpl:51 #: views/html/characters/manage.tpl:36 views/html/characters/manage.tpl:51
#: views/html/characters/manage.tpl:59 views/html/userroles/user.tpl:7 #: views/html/characters/manage.tpl:59 views/html/userroles/user.tpl:7
#: views/html/users/index.tpl:27 views/html/users/manage.tpl:29 #: views/html/users/index.tpl:31 views/html/users/manage.tpl:29
#: views/html/users/manage.tpl:42 views/html/users/manage.tpl:50 #: views/html/users/manage.tpl:42 views/html/users/manage.tpl:50
msgid "Moderator" msgid "Moderator"
msgstr "Moderator" msgstr "Moderator"
#: controllers/CharactersController.inc:394 #: controllers/CharactersController.inc:394
#: controllers/UsersController.inc:324 views/ajax/characters/index.tpl:14 #: controllers/UsersController.inc:326 views/ajax/characters/index.tpl:14
#: views/ajax/users/index.tpl:14 views/html/characters/index.tpl:41 #: views/ajax/users/index.tpl:14 views/html/characters/index.tpl:41
#: views/html/characters/manage.tpl:37 views/html/characters/manage.tpl:53 #: views/html/characters/manage.tpl:37 views/html/characters/manage.tpl:53
#: views/html/characters/manage.tpl:61 views/html/userroles/user.tpl:9 #: views/html/characters/manage.tpl:61 views/html/userroles/user.tpl:9
#: views/html/users/index.tpl:28 views/html/users/manage.tpl:30 #: views/html/users/index.tpl:32 views/html/users/manage.tpl:30
#: views/html/users/manage.tpl:44 views/html/users/manage.tpl:52 #: views/html/users/manage.tpl:44 views/html/users/manage.tpl:52
msgid "User" msgid "User"
msgstr "Benutzer" msgstr "Benutzer"
@ -249,7 +249,7 @@ msgstr "Texteingabe"
msgid "%dXPs" msgid "%dXPs"
msgstr "%d XP" msgstr "%d XP"
#: views/ajax/users/index.tpl:20 views/html/users/index.tpl:25 #: views/ajax/users/index.tpl:20 views/html/users/index.tpl:29
#: views/html/users/manage.tpl:31 views/html/users/user.tpl:22 #: views/html/users/manage.tpl:31 views/html/users/user.tpl:22
#, php-format #, php-format
msgid "registered on %s" msgid "registered on %s"
@ -889,27 +889,27 @@ msgid "Manage"
msgstr "Verwalten" msgstr "Verwalten"
#: views/html/characters/index.tpl:23 views/html/characters/manage.tpl:18 #: views/html/characters/index.tpl:23 views/html/characters/manage.tpl:18
#: views/html/users/index.tpl:15 views/html/users/manage.tpl:15 #: views/html/users/index.tpl:16 views/html/users/manage.tpl:15
msgid "Role" msgid "Role"
msgstr "Rolle" msgstr "Rolle"
#: views/html/characters/index.tpl:24 views/html/characters/manage.tpl:19 #: views/html/characters/index.tpl:24 views/html/characters/manage.tpl:19
#: views/html/users/index.tpl:16 views/html/users/manage.tpl:16 #: views/html/users/index.tpl:17 views/html/users/manage.tpl:16
msgid "Date of registration" msgid "Date of registration"
msgstr "Registrierungsdatum" msgstr "Registrierungsdatum"
#: views/html/characters/index.tpl:29 views/html/characters/manage.tpl:24 #: views/html/characters/index.tpl:29 views/html/characters/manage.tpl:24
#: views/html/users/index.tpl:18 views/html/users/manage.tpl:18 #: views/html/users/index.tpl:19 views/html/users/manage.tpl:18
msgid "Sort list" msgid "Sort list"
msgstr "Liste sortieren" msgstr "Liste sortieren"
#: views/html/characters/index.tpl:52 views/html/quests/index.tpl:82 #: views/html/characters/index.tpl:52 views/html/quests/index.tpl:82
#: views/html/users/index.tpl:36 #: views/html/users/index.tpl:40
msgid "Show more" msgid "Show more"
msgstr "Weitere anzeigen" msgstr "Weitere anzeigen"
#: views/html/characters/index.tpl:53 views/html/quests/index.tpl:83 #: views/html/characters/index.tpl:53 views/html/quests/index.tpl:83
#: views/html/users/index.tpl:37 #: views/html/users/index.tpl:41
msgid "Show all" msgid "Show all"
msgstr "Alle anzeigen" msgstr "Alle anzeigen"
@ -956,7 +956,8 @@ msgstr "Login"
#: views/html/introduction/index.tpl:7 views/html/users/create.tpl:84 #: views/html/introduction/index.tpl:7 views/html/users/create.tpl:84
#: views/html/users/create.tpl:85 views/html/users/edit.tpl:87 #: views/html/users/create.tpl:85 views/html/users/edit.tpl:87
#: views/html/users/edit.tpl:89 views/html/users/edit.tpl:91 #: views/html/users/edit.tpl:89 views/html/users/edit.tpl:91
#: views/html/users/index.tpl:14 views/html/users/login.tpl:12 #: views/html/users/index.tpl:15 views/html/users/index.tpl:20
#: views/html/users/index.tpl:21 views/html/users/login.tpl:12
#: views/html/users/login.tpl:13 views/html/users/manage.tpl:14 #: views/html/users/login.tpl:13 views/html/users/manage.tpl:14
#: views/html/users/register.tpl:83 views/html/users/register.tpl:84 #: views/html/users/register.tpl:83 views/html/users/register.tpl:84
msgid "Username" msgid "Username"
@ -1251,8 +1252,8 @@ msgstr "Text für falsche Antwort"
msgid "Task" msgid "Task"
msgstr "Aufgabe" msgstr "Aufgabe"
#: views/html/quests/index.tpl:13 #: views/html/quests/index.tpl:13 views/html/users/index.tpl:12
msgid "Filter" msgid "Filters"
msgstr "Filter" msgstr "Filter"
#: views/html/quests/index.tpl:16 views/html/quests/index.tpl:23 #: views/html/quests/index.tpl:16 views/html/quests/index.tpl:23
@ -1263,7 +1264,7 @@ msgstr "alle"
msgid "Questtype" msgid "Questtype"
msgstr "Questtyp" msgstr "Questtyp"
#: views/html/quests/index.tpl:50 #: views/html/quests/index.tpl:50 views/html/users/index.tpl:23
msgid "Apply filters" msgid "Apply filters"
msgstr "Filter anwenden" msgstr "Filter anwenden"

View file

@ -58,12 +58,13 @@
* Get registered users. * Get registered users.
* *
* @throws ParamsNotValidException * @throws ParamsNotValidException
* @param string $sort Field to sort by * @param string $sort Field to sort by
* @param int $limit Limit amount of Characters (optional) * @param string $username Only get users with the given username (optional)
* @param int $offset Offset (optional) * @param int $limit Limit amount of Characters (optional)
* @return array Users * @param int $offset Offset (optional)
* @return array Users
*/ */
public function getUsers($sort, $limit=null, $offset=0) public function getUsers($sort, $username=null, $limit=null, $offset=0)
{ {
switch($sort) switch($sort)
{ {
@ -75,14 +76,11 @@
); );
return $this->db->query( return $this->db->query(
sprintf( 'SELECT id, created, username, url, surname, prename, email, mailing '.
'SELECT id, created, username, url, surname, prename, email, mailing '. 'FROM users '.
'FROM users '. (!is_null($username) ? sprintf('WHERE username LIKE \'%%%s%%\' ', $username) : null).
'ORDER BY %s %s '. sprintf('ORDER BY %s %s ', $sort, $orders[$sort]).
(!empty($limit) ? sprintf('LIMIT %d, %d', $offset, $limit) : null), (!empty($limit) ? sprintf('LIMIT %d, %d', $offset, $limit) : null)
$sort,
$orders[$sort]
)
); );
break; break;
case 'role': case 'role':
@ -91,6 +89,7 @@
'FROM users '. 'FROM users '.
'LEFT JOIN users_userroles ON users_userroles.user_id = users.id '. 'LEFT JOIN users_userroles ON users_userroles.user_id = users.id '.
'LEFT JOIN userroles ON userroles.id = users_userroles.user_id '. 'LEFT JOIN userroles ON userroles.id = users_userroles.user_id '.
(!is_null($username) ? sprintf('WHERE username LIKE \'%%%s%%\' ', $username) : null).
'ORDER BY userroles.id IS NULL, userroles.id ASC '. 'ORDER BY userroles.id IS NULL, userroles.id ASC '.
(!empty($limit) ? sprintf('LIMIT %d, %d', $offset, $limit) : null) (!empty($limit) ? sprintf('LIMIT %d, %d', $offset, $limit) : null)
); );

View file

@ -9,6 +9,7 @@
<form method="get"> <form method="get">
<fieldset class="filter"> <fieldset class="filter">
<legend><?=_('Filters')?></legend>
<p><small>Sortierung:</small></p> <p><small>Sortierung:</small></p>
<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>
@ -16,7 +17,10 @@
<option value="created" <?php if($sortorder == 'created') : ?>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>
<label for="username"><?=_('Username')?></label>:
<input id="username" type="text" name="username" placeholder="<?=_('Username')?>" value="" />
</fieldset> </fieldset>
<input type="submit" value="<?=_('Apply filters')?>" />
</form> </form>
<ol id="userlist" class="cglist"> <ol id="userlist" class="cglist">
<?php foreach($users as &$user) : ?> <?php foreach($users as &$user) : ?>