From 45b5e8b879c1135398790cf988434188aa062cd4 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sat, 25 Oct 2014 15:44:38 -0400 Subject: Add pagination for users page --- app/Controller/User.php | 23 +++++++++++++++---- app/Model/User.php | 56 ++++++++++++++++++++++++++++++++++++++++++++- app/Template/user_index.php | 26 +++++++++++---------- 3 files changed, 88 insertions(+), 17 deletions(-) (limited to 'app') diff --git a/app/Controller/User.php b/app/Controller/User.php index 834b2379..e757fa84 100644 --- a/app/Controller/User.php +++ b/app/Controller/User.php @@ -121,16 +121,31 @@ class User extends Base */ public function index() { - $users = $this->user->getAll(); - $nb_users = count($users); + $direction = $this->request->getStringParam('direction', 'ASC'); + $order = $this->request->getStringParam('order', 'username'); + $offset = $this->request->getIntegerParam('offset', 0); + $limit = 25; + + $users = $this->user->paginate($offset, $limit, $order, $direction); + $nb_users = $this->user->count(); $this->response->html( $this->template->layout('user_index', array( 'projects' => $this->project->getList(), - 'users' => $users, 'nb_users' => $nb_users, + 'users' => $users, 'menu' => 'users', - 'title' => t('Users').' ('.$nb_users.')' + 'title' => t('Users').' ('.$nb_users.')', + 'pagination' => array( + 'controller' => 'user', + 'action' => 'index', + 'direction' => $direction, + 'order' => $order, + 'total' => $nb_users, + 'offset' => $offset, + 'limit' => $limit, + 'params' => array(), + ), ))); } diff --git a/app/Model/User.php b/app/Model/User.php index 9544f3c9..41bad0bb 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -138,10 +138,64 @@ class User extends Base return $this->db ->table(self::TABLE) ->asc('username') - ->columns('id', 'username', 'name', 'email', 'is_admin', 'default_project_id', 'is_ldap_user', 'notifications_enabled', 'google_id', 'github_id') + ->columns( + 'id', + 'username', + 'name', + 'email', + 'is_admin', + 'default_project_id', + 'is_ldap_user', + 'notifications_enabled', + 'google_id', + 'github_id' + ) ->findAll(); } + /** + * Get all users with pagination + * + * @access public + * @param integer $offset Offset + * @param integer $limit Limit + * @param string $column Sorting column + * @param string $direction Sorting direction + * @return array + */ + public function paginate($offset = 0, $limit = 25, $column = 'username', $direction = 'ASC') + { + return $this->db + ->table(self::TABLE) + ->columns( + 'id', + 'username', + 'name', + 'email', + 'is_admin', + 'default_project_id', + 'is_ldap_user', + 'notifications_enabled', + 'google_id', + 'github_id' + ) + ->offset($offset) + ->limit($limit) + ->orderBy($column, $direction) + ->findAll(); + } + + /** + * Get the number of users + * + * @access public + * @return integer + */ + public function count() + { + return $this->db->table(self::TABLE)->count(); + } + /** * List all users (key-value pairs with id/username) * diff --git a/app/Template/user_index.php b/app/Template/user_index.php index d4e1bbf9..fc2b6307 100644 --- a/app/Template/user_index.php +++ b/app/Template/user_index.php @@ -3,7 +3,7 @@

()

@@ -13,29 +13,29 @@ - - - - - - - + + + + + + + - +
- # + $user['id'])) ?> - + $user['id'])) ?> - + @@ -66,6 +66,8 @@
+ + -- cgit v1.2.3