]> 91.132.146.200 Git - insipid.git/commitdiff
pagination on overview
authorBanana <banana@optimus.de>
Thu, 3 Oct 2019 19:17:46 +0000 (21:17 +0200)
committerBanana <banana@optimus.de>
Thu, 3 Oct 2019 19:17:46 +0000 (21:17 +0200)
ChangeLog
webroot/config.default.php
webroot/lib/management.class.php
webroot/view/overview.inc.php
webroot/view/overview.php

index c983c91f787aebc9b27a81d0c889c4b8338f9bfa..cb2b7a44602fc05ccf0da0e0579ef6fa7d352c84 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,7 @@ version 2.2alpha - Guardian of Ice - (tba)
        * email import
        * code cleanups
        * using mysql transactions if needed
+       * pagination
 
 
 version 2.1alpha - Guardian of Fire - (2019-09-29)
index ef73a95052e3124ba6a34f475d7b93848ce19fa0..e645573db2b77458b15f272a68b47a94e9b25a62 100644 (file)
@@ -46,6 +46,9 @@ define('LOCAL_STORAGE', 'localdata');
 # username and password see above
 define("USE_PAGE_AUTH",false);
 
+# results per page
+define("RESULTS_PER_PAGE",10);
+
 # settings for importing from e-mail
 # SSL/TLS only
 # imap
index cee705f1a1cea18b7598d7ebfdedf4199072bccc..a26cab7c9f0941ddefea691356838012791edfda 100644 (file)
@@ -267,7 +267,7 @@ class Management {
         * @param bool | int $limit
         * @return array
         */
-       public function links($limit=false) {
+       public function links($limit=false,$offset=false) {
                $ret = array();
 
                $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES."
@@ -275,9 +275,21 @@ class Management {
                        WHERE `status` = 2
                        GROUP BY `hash`
                        ORDER BY `created` DESC";
+               if(!empty($limit)) {
+                       $queryStr .= " LIMIT $limit";
+                       if($offset !== false) {
+                               $queryStr .= " OFFSET $offset";
+                       }
+               }
                $query = $this->DB->query($queryStr);
                if(!empty($query) && $query->num_rows > 0) {
-                       $ret = $query->fetch_all(MYSQLI_ASSOC);
+                       $ret['results'] = $query->fetch_all(MYSQLI_ASSOC);
+
+                       $query = $this->DB->query("SELECT COUNT(DISTINCT(hash)) AS `amount` 
+                               FROM `".DB_PREFIX."_combined`
+                               WHERE `status` = 2");
+                       $result = $query->fetch_assoc();
+                       $ret['amount'] = $result['amount'];
                }
 
                return $ret;
index 05581ec96e465e9393abafec1d665fffe390b07c..09eb95a6b91ccf08023e886ef15d0db6dbace251 100644 (file)
@@ -36,11 +36,17 @@ if(isset($_GET['id']) && !empty($_GET['id'])) {
        $_id = trim($_GET['id']);
        $_id = Summoner::validate($_id,'digit') ? $_id : false;
 }
+$_curPage = 1;
+if(isset($_GET['page']) && !empty($_GET['page'])) {
+       $_curPage = trim($_GET['page']);
+       $_curPage = Summoner::validate($_curPage,'digit') ? $_curPage : 1;
+}
 
 $linkCollection = array();
 $subHeadline = false;
 $tagCollection = array();
 $categoryCollection = array();
+$pagination = array('pages' => 0);
 
 switch($_requestMode) {
        case 'tag':
@@ -72,5 +78,29 @@ switch($_requestMode) {
        case 'all':
        default:
                # show all
-               $linkCollection = $Management->links();
+               $linkCollection = $Management->links(RESULTS_PER_PAGE, (RESULTS_PER_PAGE * ($_curPage-1)));
+               if(!empty($linkCollection['amount'])) {
+                       $pagination['pages'] = ceil($linkCollection['amount'] / RESULTS_PER_PAGE);
+                       $pagination['curPage'] = $_curPage;
+                       $pagination['m'] = $_requestMode;
+               }
+}
+
+if($pagination['pages'] > 11) {
+       # first pages
+       $pagination['visibleRange'] = range(1,3);
+       # last pages
+       foreach(range($pagination['pages']-2, $pagination['pages']) as $e) {
+               array_push($pagination['visibleRange'], $e);
+       }
+       # pages before and after current page
+       $cRange = range($pagination['curPage']-1, $pagination['curPage']+1);
+       foreach($cRange as $e) {
+               array_push($pagination['visibleRange'], $e);
+       }
+       $pagination['currentRangeStart'] = array_shift($cRange);
+       $pagination['currentRangeEnd'] = array_pop($cRange);
+}
+else {
+       $pagination['visibleRange'] = range(1,$pagination['pages']);
 }
index 79e6536b6e28f03d7652d8936d0e77997d599cb4..fc5bc5c550ef57bbb584e273bd39e78f3df3b282 100644 (file)
                        <?php } ?>
                </div>
        </div>
+
+       <?php if($pagination['pages'] > 0) { ?>
+               <nav class="pagination is-centered" role="navigation" aria-label="pagination">
+                       <?php if($pagination['curPage'] > 1) {
+                               echo '<a href="index.php?p=overview&m='.$pagination['m'].'&page='.($pagination['curPage']-1).'" 
+                                       class="pagination-previous">Previous</a>';
+                       }
+                       if($pagination['curPage'] < $pagination['pages']) {
+                               echo '<a href="index.php?p=overview&m='.$pagination['m'].'&page='.($pagination['curPage']+1).'" 
+                                       class="pagination-next">Next</a>';
+                       }
+                       ?>
+                       <ul class="pagination-list">
+                               <?php
+                               $ellipsisShown = 0;
+                               for($i=1;$i<=$pagination['pages'];$i++) {
+                                       $active = '';
+                                       if($i == $pagination['curPage']) $active = 'is-current';
+
+                                       if(in_array($i,$pagination['visibleRange'])) {
+                                               echo '<li><a href="index.php?p=overview&m=' . $pagination['m'] . '&page=' . $i . '"
+                                               class="pagination-link ' . $active . '"
+                                               aria-label="Goto page ' . $i . '">' . $i . '</a></li>';
+                                       }
+                                       else {
+                                               if($i < $pagination['currentRangeStart'] && $ellipsisShown == 0) {
+                                                       echo '<li><span class="pagination-ellipsis">&hellip;</span></li>';
+                                                       $ellipsisShown = 1;
+                                               }
+                                               if($i > $pagination['currentRangeEnd'] && ($ellipsisShown == 0 || $ellipsisShown == 1)) {
+                                                       echo '<li><span class="pagination-ellipsis">&hellip;</span></li>';
+                                                       $ellipsisShown = 2;
+                                               }
+                                       }
+                               }
+                               ?>
+                       </ul>
+               </nav>
+       <?php } ?>
 </section>
 
 <section class="section">
-<?php if(!empty($linkCollection)) { ?>
+<?php if(!empty($linkCollection['results'])) { ?>
 <div class="columns is-multiline">
-<?php foreach ($linkCollection as $link) { ?>
+<?php foreach ($linkCollection['results'] as $link) { ?>
        <div class="column is-one-quarter">
                <div class="card">
                        <div class="card-image">