From 73ddb2429653ba3b4669689338a58a67ac7276e5 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 9 Oct 2013 11:45:18 +0200 Subject: [PATCH] Basic pagination --- public_html/media/css/core.css | 20 ++++----- public_html/media/css/post-list.css | 33 ++++++++++++++ src/Controllers/PostController.php | 60 ++++++++++++++++--------- src/Views/layout-normal.phtml | 2 +- src/Views/post-list.phtml | 70 ++++++++++++++++++++++++++++- 5 files changed, 152 insertions(+), 33 deletions(-) create mode 100644 public_html/media/css/post-list.css diff --git a/public_html/media/css/core.css b/public_html/media/css/core.css index d3ed16d8..249f83b8 100644 --- a/public_html/media/css/core.css +++ b/public_html/media/css/core.css @@ -12,48 +12,48 @@ body { margin: 1.5em 0.75em; } -nav { +#top-nav { background: #eee; color: black; } -nav ul { +#top-nav ul { margin: 0; padding: 0; list-style-type: none; } -nav li { +#top-nav li { display: inline-block; } -nav li input, -nav li a { +#top-nav li input, +#top-nav li a { color: black; display: inline-block; margin-bottom: 3px; text-decoration: none; } -nav li a { +#top-nav li a { padding: 0.2em 0.75em; outline: 0; } -nav li.search { +#top-nav li.search { background: white; margin: 0 0.25em; padding: 0.2em 0.5em; } -nav li a:focus, -nav li a:hover { +#top-nav li a:focus, +#top-nav li a:hover { color: firebrick; border-bottom: 3px solid firebrick; margin-bottom: 0; } -nav li.search input { +#top-nav li.search input { border: 0; } diff --git a/public_html/media/css/post-list.css b/public_html/media/css/post-list.css new file mode 100644 index 00000000..d128d9f9 --- /dev/null +++ b/public_html/media/css/post-list.css @@ -0,0 +1,33 @@ +.paginator-wrapper { + text-align: center; +} + +.paginator { + margin: 2em auto 0 auto; + padding: 0; + list-style-type: none; +} + +.paginator li { + display: inline-block; +} + +.paginator li a { + display: inline-block; + padding: 0.2em 0.5em; + margin-right: 0.5em; + background: #eee; + border: 1px solid silver; + color: black; + text-decoration: none; +} + +.paginator li.active a { + color: red; + background: mistyrose; + border-color: red; +} + +.paginator li.inactive a { + color: gray; +} diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 58dd493e..e4d3a68c 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -12,11 +12,16 @@ class PostController /** * @route /posts + * @route /posts/{page} * @route /posts/{query} - * @validate query .* + * @route /posts/{query}/{page} + * @validate page \d* + * @validate query [^\/]* */ - public function listAction($query = null) + public function listAction($page = 1, $query = null) { + $this->context->stylesheets []= 'post-list.css'; + #redirect requests in form of /posts/?query=... to canonical address $formQuery = InputHelper::get('query'); if (!empty($formQuery)) @@ -27,33 +32,48 @@ class PostController } $this->context->subTitle = 'browsing posts'; - $this->context->searchQuery = $query; + $page = intval($page); + $postsPerPage = intval($this->config->browsing->postsPerPage); PrivilegesHelper::confirmWithException($this->context->user, Privilege::ListPosts); - $page = 1; - $params = []; - - $allowedSafety = array_filter(PostSafety::getAll(), function($safety) + $buildDbQuery = function($dbQuery) { - return PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, PostSafety::toString($safety)); - }); - //todo safety [user choice] + $dbQuery->from('post'); - $whereSql = 'WHERE safety IN (' . R::genSlots($allowedSafety) . ')'; - $params = array_merge($params, $allowedSafety); + $allowedSafety = array_filter(PostSafety::getAll(), function($safety) + { + return PrivilegesHelper::confirm($this->context->user, Privilege::ListPosts, PostSafety::toString($safety)); + }); + //todo safety [user choice] - //todo construct WHERE based on filters + $dbQuery->where('safety IN (' . R::genSlots($allowedSafety) . ')'); + foreach ($allowedSafety as $s) + $dbQuery->put($s); - //todo construct ORDER based on filers - $orderSql = 'ORDER BY upload_date DESC'; + //todo construct WHERE based on filters - $limitSql = 'LIMIT ? OFFSET ?'; - $postsPerPage = intval($this->config->browsing->postsPerPage); - $params[] = $postsPerPage; - $params[] = ($page - 1) * $postsPerPage; + //todo construct ORDER based on filers + }; - $posts = R::findAll('post', sprintf('%s %s %s', $whereSql, $orderSql, $limitSql), $params); + $countDbQuery = R::$f->begin(); + $countDbQuery->select('COUNT(1) AS count'); + $buildDbQuery($countDbQuery); + $postCount = intval($countDbQuery->get('row')['count']); + $pageCount = ceil($postCount / $postsPerPage); + + $searchDbQuery = R::$f->begin(); + $searchDbQuery->select('*'); + $buildDbQuery($searchDbQuery); + $searchDbQuery->orderBy('upload_date DESC'); + $searchDbQuery->limit('?')->put($postsPerPage); + $searchDbQuery->offset('?')->put(($page - 1) * $postsPerPage); + + $posts = $searchDbQuery->get(); + $this->context->transport->searchQuery = $query; + $this->context->transport->page = $page; + $this->context->transport->postCount = $postCount; + $this->context->transport->pageCount = $pageCount; $this->context->transport->posts = $posts; } diff --git a/src/Views/layout-normal.phtml b/src/Views/layout-normal.phtml index 495ce150..2623a801 100644 --- a/src/Views/layout-normal.phtml +++ b/src/Views/layout-normal.phtml @@ -20,7 +20,7 @@ -