From bfcbe5aa2e6569c69c46196c86e7b5516b81b4c6 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Thu, 1 Sep 2016 23:47:34 +0200 Subject: [PATCH] Implement pagination for people page. --- controllers/ViewPeople.php | 23 +++++++++++++++++------ models/Tag.php | 33 +++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/controllers/ViewPeople.php b/controllers/ViewPeople.php index 4b80365..5d554a7 100644 --- a/controllers/ViewPeople.php +++ b/controllers/ViewPeople.php @@ -12,9 +12,12 @@ class ViewPeople extends HTMLController public function __construct() { + $page = isset($_GET['page']) ? (int) $_GET['page'] : 1; + $start = ($page - 1) * self::PER_PAGE; + $total_count = Tag::getCount(1, 'Person'); + // Fetch subalbums. - // !!! TODO: pagination. - $subalbums = Tag::getPeople(); + $subalbums = Tag::getPeople(0, $start, self::PER_PAGE); // What assets are we using? $id_assets = array_map(function($album) { @@ -36,10 +39,18 @@ class ViewPeople extends HTMLController ]; } - $index = new AlbumIndex($albums); - parent::__construct('People - ' . SITE_TITLE); - $this->page->adopt($index); - $this->page->setCanonicalUrl(BASEURL . '/people/'); + $this->page->adopt(new AlbumIndex($albums)); + + $pagination = new PageIndex([ + 'recordCount' => $total_count, + 'items_per_page' => self::PER_PAGE, + 'start' => $start, + 'base_url' => BASEURL . '/people/', + 'page_slug' => 'page/%PAGE%/', + ]); + $this->page->adopt(new Pagination($pagination)); + + $this->page->setCanonicalUrl(BASEURL . '/people/' . ($page > 1 ? 'page/' . $page . '/' : '')); } } diff --git a/models/Tag.php b/models/Tag.php index 572e077..ea9bd7c 100644 --- a/models/Tag.php +++ b/models/Tag.php @@ -95,16 +95,19 @@ class Tag return $rows; } - public static function getAlbums($id_parent = 0, $return_format = 'array') + public static function getAlbums($id_parent = 0, $offset = 0, $limit = 24, $return_format = 'array') { $rows = Registry::get('db')->queryAssocs(' SELECT * FROM tags WHERE id_parent = {int:id_parent} AND kind = {string:kind} - ORDER BY tag ASC', + ORDER BY tag ASC + LIMIT {int:offset}, {int:limit}', [ 'id_parent' => $id_parent, 'kind' => 'Album', + 'offset' => $offset, + 'limit' => $limit, ]); if ($return_format == 'object') @@ -118,16 +121,19 @@ class Tag return $rows; } - public static function getPeople($id_parent = 0, $return_format = 'array') + public static function getPeople($id_parent = 0, $offset = 0, $limit = 24, $return_format = 'array') { $rows = Registry::get('db')->queryAssocs(' SELECT * FROM tags WHERE id_parent = {int:id_parent} AND kind = {string:kind} - ORDER BY tag ASC', + ORDER BY tag ASC + LIMIT {int:offset}, {int:limit}', [ 'id_parent' => $id_parent, 'kind' => 'Person', + 'offset' => $offset, + 'limit' => $limit, ]); if ($return_format == 'object') @@ -322,12 +328,23 @@ class Tag ['tags' => $tags]); } - public static function getCount($only_active = 1) + public static function getCount($only_active = 1, $kind = '') { - return $db->queryValue(' + $where = []; + if ($only_active) + $where[] = 'count > 0'; + if (!empty($kind)) + $where[] = 'kind = {string:kind}'; + + if (!empty($where)) + $where = 'WHERE ' . implode(' AND ', $where); + else + $where = ''; + + return Registry::get('db')->queryValue(' SELECT COUNT(*) - FROM tags' . ($only_active ? ' - WHERE count > 0' : '')); + FROM tags ' . $where, + ['kind' => $kind]); } public function __toString()