From: Christian Weiske Date: Wed, 16 Nov 2016 10:14:23 +0000 (+0100) Subject: new pager X-Git-Tag: v0.2.0~8 X-Git-Url: https://git.cweiske.de/phinde.git/commitdiff_plain/202def908c2782312d82eeefd67afb0469dee440?hp=f2b3557f358efc5b6363b1f8e4b0c30e93945feb new pager --- diff --git a/data/templates/pager.htm b/data/templates/pager.htm index ed86a55..d2baa8a 100644 --- a/data/templates/pager.htm +++ b/data/templates/pager.htm @@ -2,48 +2,34 @@ {% if pager.numPages > 1 %} diff --git a/src/phinde/Html/Pager.php b/src/phinde/Html/Pager.php index 9726d95..cd944b9 100644 --- a/src/phinde/Html/Pager.php +++ b/src/phinde/Html/Pager.php @@ -1,6 +1,24 @@ >] + * [<< prev] [1] [2] [3] [4] [5] [next >>] + * [<< prev] [1] [2] [3] [4] [5] ... [8] [next >>] + * [<< prev] [1] ... [4] [5] [6] [7] [8] ... [10] [next >>] + * + * replace ".." with actual link when between previous and next is only one + */ class Html_Pager { protected $pager; @@ -19,50 +37,108 @@ class Html_Pager if (strpos($filename, '%d') !== false) { $append = false; } - //fix non-static factory method error - error_reporting(error_reporting() & ~E_STRICT); - $this->pager = \Pager::factory( - array( - 'mode' => 'Sliding', - 'perPage' => $perPage, - 'delta' => 2, - 'totalItems' => $itemCount, - 'currentPage' => $currentPage, - 'urlVar' => 'page', - 'append' => $append, - 'path' => '', - 'fileName' => $filename, - 'separator' => '###', - 'spacesBeforeSeparator' => 0, - 'spacesAfterSeparator' => 0, - 'curPageSpanPre' => '', - 'curPageSpanPost' => '', - 'firstPagePre' => '', - 'firstPageText' => 'first', - 'firstPagePost' => '', - 'lastPagePre' => '', - 'lastPageText' => 'last', - 'lastPagePost' => '', - 'prevImg' => '« prev', - 'nextImg' => 'next »', - ) - ); + + $numPages = ceil($itemCount / $perPage); + $this->numPages = $numPages; + + //1-based + $pages = [ + 1 => true, + 2 => true, + $numPages - 1 => true, + $numPages => true, + ]; + if ($currentPage <= 6) { + $pages[3] = 3; + $pages[4] = 4; + $pages[5] = 5; + } + if ($currentPage >= $numPages - 5) { + $pages[$numPages - 2] = true; + $pages[$numPages - 3] = true; + $pages[$numPages - 4] = true; + } + for ($n = $currentPage - 2; $n <= $currentPage + 2; $n++) { + $pages[$n] = true; + } + foreach (array_keys($pages) as $key) { + if ($key < 1 || $key > $numPages) { + unset($pages[$key]); + } + } + if ($currentPage >= 7 && !isset($pages[4])) { + $pages[3] = null; + } + if ($currentPage <= $numPages - 6 && !isset($pages[$numPages - 3])) { + $pages[$numPages - 2] = null; + } + + ksort($pages); + foreach ($pages as $pageNum => &$value) { + if ($pageNum == $currentPage) { + $value = ['active'=> false, 'title' => $pageNum]; + } else if ($value !== null) { + $value = $this->makeLink($pageNum, $filename); + } else { + $value = ['active'=> false, 'title' => '…']; + } + } + + $prev = ['active'=> false, 'title' => '« prev']; + if ($currentPage > 1) { + $prev = $this->makeLink($currentPage - 1, $filename, '« prev'); + } + $next = ['active'=> false, 'title' => 'next »']; + if ($currentPage < $numPages) { + $next = $this->makeLink($currentPage + 1, $filename, 'next »'); + } + //first and last are for opensearch + $first = ['active'=> false, 'title' => 'first']; + if ($currentPage > 1) { + $first = $this->makeLink(1, $filename, 'first'); + } + $last = ['active'=> false, 'title' => 'last']; + if ($numPages > 1 && $currentPage < $numPages) { + $last = $this->makeLink($numPages, $filename, 'last'); + } + + $this->links = [ + 'prev' => $prev, + 'next' => $next, + 'first' => $first, + 'last' => $last, + 'pages' => $pages, + ]; } + protected function makeLink($pageNum, $filename, $title = null) + { + $title = $title === null ? $pageNum : $title; + $url = $filename . '&page=' . $pageNum; + return [ + 'active' => true, + 'url' => $url, + 'title' => $title, + 'html' => '' + . htmlspecialchars($title) + . '', + ]; + } public function getLinks() { - $arLinks = $this->pager->getLinks(); - $arLinks['pages'] = explode('###', $arLinks['pages']); - return $arLinks; + return $this->links; } public function getFullUrls() { - $arLinks = $this->pager->getLinks(); - $arUrls = array(); - foreach ($arLinks['linkTagsRaw'] as $key => $link) { - if (isset($link['url'])) { + $arUrls = array(); + foreach ($this->links as $key => $link) { + if ($key == 'pages') { + continue; + } + if ($link['active']) { $arUrls[$key] = str_replace( '&', '&', Helper::fullUrl('/' . $link['url']) @@ -74,6 +150,7 @@ class Html_Pager public function numPages() { + return $this->numPages; return $this->pager->numPages(); } } diff --git a/www/css/phinde.css b/www/css/phinde.css index eae8dd9..b08a03a 100644 --- a/www/css/phinde.css +++ b/www/css/phinde.css @@ -6,6 +6,13 @@ margin-bottom: 1ex; } +/* bootstrap 2.1.1 does not have this style, 2.3.2 would have it */ +.pagination ul > .active > a, +.pagination ul > .active > a:hover { + background-color: #0088cc; + color: #FFF; +} + .hits { list-style-type: none; margin-left: 0px;