From 243f22542d943eb3ef6f50c702f9eef15e8c3915 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 4 May 2014 10:32:32 +0200 Subject: [PATCH] Moved user listing to API --- public_html/dispatch.php | 7 +++---- src/Api/Jobs/ListUsersJob.php | 25 ++++++++++++++++++++++++ src/Controllers/UserController.php | 31 +++++++++++------------------- src/Views/top-navigation.phtml | 2 +- src/Views/user-list.phtml | 2 +- 5 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 src/Api/Jobs/ListUsersJob.php diff --git a/public_html/dispatch.php b/public_html/dispatch.php index a729ac10..2458fc16 100644 --- a/public_html/dispatch.php +++ b/public_html/dispatch.php @@ -140,6 +140,9 @@ $userValidations = 'filter' => '[^\/]+', ]; +\Chibi\Router::register(['UserController', 'listView'], 'GET', '/users', $userValidations); +\Chibi\Router::register(['UserController', 'listView'], 'GET', '/users/{page}', $userValidations); +\Chibi\Router::register(['UserController', 'listView'], 'GET', '/users/{filter}/{page}', $userValidations); \Chibi\Router::register(['UserController', 'flagAction'], 'POST', '/user/{name}/flag', $userValidations); \Chibi\Router::register(['UserController', 'banAction'], 'POST', '/user/{name}/ban', $userValidations); \Chibi\Router::register(['UserController', 'unbanAction'], 'POST', '/user/{name}/unban', $userValidations); @@ -151,10 +154,6 @@ foreach (['GET', 'POST'] as $method) \Chibi\Router::register(['UserController', 'registrationAction'], $method, '/register', $userValidations); \Chibi\Router::register(['UserController', 'viewAction'], $method, '/user/{name}/{tab}', $userValidations); \Chibi\Router::register(['UserController', 'viewAction'], $method, '/user/{name}/{tab}/{page}', $userValidations); - \Chibi\Router::register(['UserController', 'listAction'], $method, '/users', $userValidations); - \Chibi\Router::register(['UserController', 'listAction'], $method, '/users/{page}', $userValidations); - \Chibi\Router::register(['UserController', 'listAction'], $method, '/users/{filter}', $userValidations); - \Chibi\Router::register(['UserController', 'listAction'], $method, '/users/{filter}/{page}', $userValidations); \Chibi\Router::register(['UserController', 'acceptRegistrationAction'], $method, '/user/{name}/accept-registration', $userValidations); \Chibi\Router::register(['UserController', 'deleteAction'], $method, '/user/{name}/delete', $userValidations); \Chibi\Router::register(['UserController', 'settingsAction'], $method, '/user/{name}/settings', $userValidations); diff --git a/src/Api/Jobs/ListUsersJob.php b/src/Api/Jobs/ListUsersJob.php new file mode 100644 index 00000000..4df5c1a5 --- /dev/null +++ b/src/Api/Jobs/ListUsersJob.php @@ -0,0 +1,25 @@ +getPageSize(); + $page = $this->getArgument(self::PAGE_NUMBER); + $filter = $this->getArgument(self::QUERY); + + $users = UserSearchService::getEntities($filter, $pageSize, $page); + $userCount = UserSearchService::getEntityCount($filter); + + return $this->getPager($users, $userCount, $page, $pageSize); + } + + public function getDefaultPageSize() + { + return intval(getConfig()->browsing->usersPerPage); + } + + public function requiresPrivilege() + { + return Privilege::ListUsers; + } +} diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index cf8b796e..bf43c9eb 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -1,29 +1,20 @@ $page, + ListUsersJob::QUERY => $filter, + ]); + $context = getContext(); - Access::assert( - Privilege::ListUsers); - $suppliedFilter = $filter ?: InputHelper::get('filter') ?: 'order:alpha,asc'; - $page = max(1, intval($page)); - $usersPerPage = intval(getConfig()->browsing->usersPerPage); - - $users = UserSearchService::getEntities($suppliedFilter, $usersPerPage, $page); - $userCount = UserSearchService::getEntityCount($suppliedFilter); - $pageCount = ceil($userCount / $usersPerPage); - $page = min($pageCount, $page); - - $context->filter = $suppliedFilter; - $context->transport->users = $users; - $context->transport->paginator = new StdClass; - $context->transport->paginator->page = $page; - $context->transport->paginator->pageCount = $pageCount; - $context->transport->paginator->entityCount = $userCount; - $context->transport->paginator->entities = $users; - $context->transport->paginator->params = func_get_args(); + $context->filter = $filter; + $context->transport->users = $ret->entities; + $context->transport->paginator = $ret; } public function flagAction($name) diff --git a/src/Views/top-navigation.phtml b/src/Views/top-navigation.phtml index c1d16774..7ac7b55d 100644 --- a/src/Views/top-navigation.phtml +++ b/src/Views/top-navigation.phtml @@ -54,7 +54,7 @@ { $registerNavItem( 'Users', - \Chibi\Router::linkTo(['UserController', 'listAction']), + \Chibi\Router::linkTo(['UserController', 'listView']), $activeController == 'user' and $activeAction != 'registration' and (!isset($this->context->route->arguments['name']) or $this->context->route->arguments['name'] != Auth::getCurrentUser()->name)); diff --git a/src/Views/user-list.phtml b/src/Views/user-list.phtml index 5e0d58af..38759bd2 100644 --- a/src/Views/user-list.phtml +++ b/src/Views/user-list.phtml @@ -27,7 +27,7 @@ if (Auth::getCurrentUser()->hasEnabledEndlessScrolling())
  • - +