diff --git a/configs/AppConfig.inc b/configs/AppConfig.inc index 6c3c03b6..0e7678b0 100644 --- a/configs/AppConfig.inc +++ b/configs/AppConfig.inc @@ -209,6 +209,7 @@ * @var array */ public static $routes = array( + array('^users/all/?$', 'users/index/all', true), array('^users/([^/]+)/(edit|delete)/?$', 'users/$2/$1', true), array('^users/(?!(index|login|register|logout|manage|create|edit|delete))/?', 'users/user/$1', true), array('^seminaries/([^/]+)/(edit|delete)/?$', 'seminaries/$2/$1', true), @@ -253,6 +254,7 @@ * @var array */ public static $reverseRoutes = array( + array('^users/index/all$', 'users/all', true), array('^users/user/(.*)$', 'users/$1', true), array('^users/([^/]+)/(.*)$', 'users/$2/$1', true), array('^seminaries/seminary/(.*)$', 'seminaries/$1', false), diff --git a/controllers/UsersController.inc b/controllers/UsersController.inc index 0d80bbd9..8ae01465 100644 --- a/controllers/UsersController.inc +++ b/controllers/UsersController.inc @@ -50,10 +50,27 @@ /** * 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 - $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 @@ -61,6 +78,11 @@ // Pass data to view $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() { $selectedUsers = array(); - global $sortorder; + //global $sortorder; + $sortorder = 'username'; if($this->request->getRequestMethod() == 'POST') { @@ -324,21 +347,11 @@ } // Get registered users - $users = $this->Users->getUsers(); + $users = $this->Users->getUsers($sortorder); foreach($users as &$user) { $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 $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; - } - } ?> diff --git a/models/UsersModel.inc b/models/UsersModel.inc index 2181586a..101cc60a 100644 --- a/models/UsersModel.inc +++ b/models/UsersModel.inc @@ -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. * - * @return array 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 */ - public function getUsers() + public function getUsers($sort, $limit=null, $offset=0) { - return $this->db->query( - 'SELECT id, created, username, url, surname, prename, email '. - 'FROM users '. - 'ORDER BY username ASC' - ); + switch($sort) + { + case 'username': + case 'created': + $orders = array( + 'username' => 'ASC', + 'created' => 'DESC' + ); + + return $this->db->query( + sprintf( + 'SELECT id, created, username, url, surname, prename, email '. + 'FROM users '. + '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; + } } diff --git a/views/ajax/users/index.tpl b/views/ajax/users/index.tpl new file mode 100644 index 00000000..4615a8c1 --- /dev/null +++ b/views/ajax/users/index.tpl @@ -0,0 +1,27 @@ +format(new \DateTime($user['created']))); + } + +?> +=json_encode(array( + 'users' => $users, + 'more' => (($page*$limit) < $usersCount) +))?> diff --git a/views/html/users/index.tpl b/views/html/users/index.tpl index 8a5cb847..7b319d7c 100644 --- a/views/html/users/index.tpl +++ b/views/html/users/index.tpl @@ -6,8 +6,74 @@
=$user['username']?>=sprintf(_('registered on %s'), $dateFormatter->format(new \DateTime($user['created'])))?>
+ =$user['username']?>=sprintf(_('registered on %s'), $dateFormatter->format(new \DateTime($user['created'])))?> + (=_('Admin')?>) + (=_('Moderator')?>) + (=_('User')?>) +
+