]> 91.132.146.200 Git - insipid.git/commitdiff
pagination
authorBanana <banana@optimus.de>
Thu, 3 Oct 2019 20:28:13 +0000 (22:28 +0200)
committerBanana <banana@optimus.de>
Thu, 3 Oct 2019 20:28:13 +0000 (22:28 +0200)
webroot/config.default.php
webroot/lib/category.class.php
webroot/lib/management.class.php
webroot/lib/tag.class.php
webroot/view/home.php
webroot/view/overview.inc.php
webroot/view/overview.php

index e645573db2b77458b15f272a68b47a94e9b25a62..9b015f7365064dd4fa2f6747710f4bd7a1d66a12 100644 (file)
@@ -3,7 +3,7 @@
  * Insipid
  * Personal web-bookmark-system
  *
- * Copyright 2016-2018 Johannes Keßler
+ * Copyright 2016-2019 Johannes Keßler
  *
  * Development starting from 2011: Johannes Keßler
  * https://www.bananas-playground.net/projekt/insipid/
@@ -47,7 +47,7 @@ define('LOCAL_STORAGE', 'localdata');
 define("USE_PAGE_AUTH",false);
 
 # results per page
-define("RESULTS_PER_PAGE",10);
+define("RESULTS_PER_PAGE",12);
 
 # settings for importing from e-mail
 # SSL/TLS only
index b901f404a2fe5be99f88eb749c3b84b994f39d76..de47cd004ed553fe0d1099f23a65b5334d52be16 100644 (file)
@@ -37,7 +37,13 @@ class Category {
         * the current loaded category by DB id
         * @var int
         */
-       private $id;
+       private $_id;
+
+       /**
+        * current loaded tag data
+        * @var array
+        */
+       private $_data;
 
        public function __construct($databaseConnectionObject) {
                $this->DB = $databaseConnectionObject;
@@ -46,27 +52,31 @@ class Category {
        /**
         * by given string load the info from the DB and even create if not existing
         * @param string $string
+        * @return bool|int
         */
        public function initbystring($string) {
-               $this->id = false;
+               $this->_id = false;
                if(!empty($string)) {
-                       $queryStr = "SELECT id FROM `".DB_PREFIX."_category`
+                       $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_category`
                                                        WHERE `name` = '".$this->DB->real_escape_string($string)."'";
                        $query = $this->DB->query($queryStr);
                        if(!empty($query) && $query->num_rows > 0) {
                                $result = $query->fetch_assoc();
-                               $this->id = $result['id'];
+                               $this->_id = $result['id'];
+                               $this->_data = $result;
                        }
                        else {
                                $queryStr = "INSERT INTO `".DB_PREFIX."_category`
                                                                SET `name` = '".$this->DB->real_escape_string($string)."'";
                                $this->DB->query($queryStr);
                                if(!empty($this->DB->insert_id)) {
-                                       $this->id = $this->DB->insert_id;
+                                       $this->_id = $this->DB->insert_id;
+                                       $this->_data['id'] = $this->_id;
+                                       $this->_data['name'] = $string;
                                }
                        }
                }
-               return $this->id;
+               return $this->_id;
        }
 
        /**
@@ -75,7 +85,7 @@ class Category {
         * @return mixed
         */
        public function initbyid($id) {
-               $this->id = false;
+               $this->_id = false;
 
                if(!empty($id)) {
                        $queryStr = "SELECT id,name
@@ -84,11 +94,27 @@ class Category {
                        $query = $this->DB->query($queryStr);
                        if(!empty($query) && $query->num_rows > 0) {
                                $result = $query->fetch_assoc();
-                               $this->id = $id;
+                               $this->_id = $id;
+                               $this->_data = $result;
                        }
                }
 
-               return $this->id;
+               return $this->_id;
+       }
+
+       /**
+        * return all or data fpr given key on the current loaded tag
+        * @param bool $key
+        * @return array|mixed
+        */
+       public function getData($key=false) {
+               $ret = $this->_data;
+
+               if(!empty($key) && isset($this->_data[$key])) {
+                       $ret = $this->_data[$key];
+               }
+
+               return $ret;
        }
 
        /**
@@ -97,10 +123,10 @@ class Category {
         * @return void
         */
        public function setRelation($linkid) {
-               if(!empty($linkid) && !empty($this->id)) {
+               if(!empty($linkid) && !empty($this->_id)) {
                        $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_categoryrelation`
                                                        SET `linkid` = '".$this->DB->real_escape_string($linkid)."',
-                                                               `categoryid` = '".$this->DB->real_escape_string($this->id)."'";
+                                                               `categoryid` = '".$this->DB->real_escape_string($this->_id)."'";
                        $this->DB->query($queryStr);
                }
        }
@@ -112,18 +138,18 @@ class Category {
        public function delete() {
                $ret = false;
 
-               if(!empty($this->id)) {
+               if(!empty($this->_id)) {
                        $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
 
                        try {
                                $queryStr = "DELETE
                                        FROM `".DB_PREFIX."_categoryrelation`
-                                       WHERE `categoryid` = '".$this->DB->real_escape_string($this->id)."'";
+                                       WHERE `categoryid` = '".$this->DB->real_escape_string($this->_id)."'";
                                $this->DB->query($queryStr);
 
                                $queryStr = "DELETE
                                        FROM `".DB_PREFIX."_category`
-                                       WHERE `id` = '".$this->DB->real_escape_string($this->id)."'";
+                                       WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
                                $this->DB->query($queryStr);
 
                                $this->DB->commit();
index e7099744e5ef5a20e92f82f7c01f5ee1ce7a0c8f..8180243bc7c703da931eabad5c1ea7ac00b2ef81 100644 (file)
@@ -156,7 +156,7 @@ class Management {
        public function latestLinks($limit=5) {
                $ret = array();
 
-               $queryStr = "SELECT * FROM `".DB_PREFIX."_link` WHERE `status` = 2 ORDER BY `created` DESC";
+               $queryStr = "SELECT `title`      FROM `".DB_PREFIX."_link` WHERE `status` = 2 ORDER BY `created` DESC";
                if(!empty($limit)) {
                        $queryStr .= " LIMIT $limit";
                }
@@ -194,32 +194,46 @@ class Management {
         * @param int $id
         * @param string $string
         * @param int $limit
+        * @param bool $offset
         * @return array
         */
-       public function linksByCategory($id,$string,$limit=5) {
+       public function linksByCategory($id, $string, $limit=5, $offset=false) {
                $ret = array();
 
-               $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES."
-                       FROM `".DB_PREFIX."_combined`
-                       WHERE `status` = 2";
+               $querySelect = "SELECT ".$this->COMBINED_SELECT_VALUES;
+               $queryFrom = " FROM `".DB_PREFIX."_combined`";
+               $queryWhere = " WHERE `status` = 2";
+
                if(!empty($id) && is_numeric($id)) {
-                       $queryStr .= " AND `categoryId` = '" . $this->DB->real_escape_string($id) . "'";
+                       $queryWhere .= " AND `categoryId` = '" . $this->DB->real_escape_string($id) . "'";
                }
                elseif(!empty($string) && is_string($string)) {
-                       $queryStr .= " AND `category` = '" . $this->DB->real_escape_string($string) . "'";
+                       $queryWhere .= " AND `category` = '" . $this->DB->real_escape_string($string) . "'";
                }
                else {
                        return $ret;
                }
 
-               $queryStr .= "GROUP BY `hash`
+               $queryOrder = "GROUP BY `hash`
                        ORDER BY `created` DESC";
+               $queryLimit = '';
                if(!empty($limit)) {
-                       $queryStr .= " LIMIT $limit";
+                       $queryLimit .= " LIMIT $limit";
+                       if($offset !== false) {
+                               $queryLimit .= " OFFSET $offset";
+                       }
                }
-               $query = $this->DB->query($queryStr);
+               $query = $this->DB->query($querySelect.$queryFrom.$queryWhere.$queryOrder.$queryLimit);
                if(!empty($query) && $query->num_rows > 0) {
-                       $ret = $query->fetch_all(MYSQLI_ASSOC);
+                       while($result = $query->fetch_assoc()) {
+                               $linkObj = new Link($this->DB);
+                               $ret['results'][] = $linkObj->loadShortInfo($result['hash']);
+                               unset($linkObj);
+                       }
+
+                       $query = $this->DB->query("SELECT COUNT(DISTINCT(hash)) AS amount ".$queryFrom.$queryWhere);
+                       $result = $query->fetch_assoc();
+                       $ret['amount'] = $result['amount'];
                }
 
                return $ret;
@@ -231,32 +245,46 @@ class Management {
         * @param int $id
         * @param string $string
         * @param int $limit
+        * @param bool $offset
         * @return array
         */
-       public function linksByTag($id,$string,$limit=5) {
+       public function linksByTag($id, $string, $limit=5, $offset=false) {
                $ret = array();
 
-               $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES."
-                       FROM `".DB_PREFIX."_combined`
-                       WHERE `status` = 2";
+               $querySelect = "SELECT ".$this->COMBINED_SELECT_VALUES;
+               $queryFrom = " FROM `".DB_PREFIX."_combined`";
+               $queryWhere = " WHERE `status` = 2";
+
                if(!empty($id) && is_numeric($id)) {
-                       $queryStr .= " AND `tagId` = '" . $this->DB->real_escape_string($id) . "'";
+                       $queryWhere .= " AND `tagId` = '" . $this->DB->real_escape_string($id) . "'";
                }
                elseif(!empty($string) && is_string($string)) {
-                       $queryStr .= " AND `tag` = '" . $this->DB->real_escape_string($string) . "'";
+                       $queryWhere .= " AND `tag` = '" . $this->DB->real_escape_string($string) . "'";
                }
                else {
                        return $ret;
                }
 
-               $queryStr .= "GROUP BY `hash`
+               $queryOrder = "GROUP BY `hash`
                        ORDER BY `created` DESC";
+               $queryLimit = '';
                if(!empty($limit)) {
-                       $queryStr .= " LIMIT $limit";
+                       $queryLimit .= " LIMIT $limit";
+                       if($offset !== false) {
+                               $queryLimit .= " OFFSET $offset";
+                       }
                }
-               $query = $this->DB->query($queryStr);
+               $query = $this->DB->query($querySelect.$queryFrom.$queryWhere.$queryOrder.$queryLimit);
                if(!empty($query) && $query->num_rows > 0) {
-                       $ret = $query->fetch_all(MYSQLI_ASSOC);
+                       while($result = $query->fetch_assoc()) {
+                               $linkObj = new Link($this->DB);
+                               $ret['results'][] = $linkObj->loadShortInfo($result['hash']);
+                               unset($linkObj);
+                       }
+
+                       $query = $this->DB->query("SELECT COUNT(DISTINCT(hash)) AS amount ".$queryFrom.$queryWhere);
+                       $result = $query->fetch_assoc();
+                       $ret['amount'] = $result['amount'];
                }
 
                return $ret;
@@ -268,20 +296,21 @@ class Management {
         * @param bool $offset
         * @return array
         */
-       public function links($limit=false,$offset=false) {
+       public function links($limit=10,$offset=false) {
                $ret = array();
 
-               $queryStr = "SELECT `hash`
-                       FROM `".DB_PREFIX."_link`
-                       WHERE `status` = 2
-                       ORDER BY `created` DESC";
+               $querySelect = "SELECT `hash`";
+               $queryFrom = " FROM `".DB_PREFIX."_link`";
+               $queryWhere = " WHERE `status` = 2";
+               $queryOrder = " ORDER BY `created` DESC";
+               $queryLimit = "";
                if(!empty($limit)) {
-                       $queryStr .= " LIMIT $limit";
+                       $queryLimit = " LIMIT $limit";
                        if($offset !== false) {
-                               $queryStr .= " OFFSET $offset";
+                               $queryLimit .= " OFFSET $offset";
                        }
                }
-               $query = $this->DB->query($queryStr);
+               $query = $this->DB->query($querySelect.$queryFrom.$queryWhere.$queryOrder.$queryLimit);
                if(!empty($query) && $query->num_rows > 0) {
                        while($result = $query->fetch_assoc()) {
                                $linkObj = new Link($this->DB);
@@ -289,9 +318,7 @@ class Management {
                                unset($linkObj);
                        }
 
-                       $query = $this->DB->query("SELECT COUNT(DISTINCT(hash)) AS `amount` 
-                               FROM `".DB_PREFIX."_combined`
-                               WHERE `status` = 2");
+                       $query = $this->DB->query("SELECT COUNT(hash) AS amount ".$queryFrom.$queryWhere);
                        $result = $query->fetch_assoc();
                        $ret['amount'] = $result['amount'];
                }
index 45caae7303f30b668a38450a3228d714fb811d0c..d7f9052ec806275feb43eb43c022ec45f5c0abc0 100644 (file)
@@ -37,7 +37,13 @@ class Tag {
         * the current loaded tag by DB id
         * @var int
         */
-       private $id;
+       private $_id;
+
+       /**
+        * current loaded tag data
+        * @var array
+        */
+       private $_data;
 
        public function __construct($databaseConnectionObject) {
                $this->DB = $databaseConnectionObject;
@@ -48,21 +54,24 @@ class Tag {
         * @param string $string
         */
        public function initbystring($string) {
-               $this->id = false;
+               $this->_id = false;
                if(!empty($string)) {
-                       $queryStr = "SELECT id FROM `".DB_PREFIX."_tag`
+                       $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_tag`
                                                        WHERE `name` = '".$this->DB->real_escape_string($string)."'";
                        $query = $this->DB->query($queryStr);
                        if(!empty($query) && $query->num_rows > 0) {
                                $result = $query->fetch_assoc();
-                               $this->id = $result['id'];
+                               $this->_id = $result['id'];
+                               $this->_data = $result;
                        }
                        else {
                                $queryStr = "INSERT INTO `".DB_PREFIX."_tag`
                                                                SET `name` = '".$this->DB->real_escape_string($string)."'";
                                $this->DB->query($queryStr);
                                if(!empty($this->DB->insert_id)) {
-                                       $this->id = $this->DB->insert_id;
+                                       $this->_id = $this->DB->insert_id;
+                                       $this->_data['id'] = $this->_id;
+                                       $this->_data['name'] = $string;
                                }
                        }
                }
@@ -71,11 +80,38 @@ class Tag {
        /**
         * by given DB table id load all the info we need
         * @param int $id
+        * @return bool|int
         */
        public function initbyid($id) {
+               $this->_id = false;
+
                if(!empty($id)) {
-                       $this->id = $id;
+                       $queryStr = "SELECT `id`,`name` FROM `".DB_PREFIX."_tag`
+                                                       WHERE `id` = '".$this->DB->real_escape_string($id)."'";
+                       $query = $this->DB->query($queryStr);
+                       if(!empty($query) && $query->num_rows > 0) {
+                               $result = $query->fetch_assoc();
+                               $this->_id = $result['id'];
+                               $this->_data = $result;
+                       }
                }
+
+               return $this->_id;
+       }
+
+       /**
+        * return all or data fpr given key on the current loaded tag
+        * @param bool $key
+        * @return array|mixed
+        */
+       public function getData($key=false) {
+               $ret = $this->_data;
+
+               if(!empty($key) && isset($this->_data[$key])) {
+                       $ret = $this->_data[$key];
+               }
+
+               return $ret;
        }
 
        /**
@@ -84,10 +120,10 @@ class Tag {
         * @return boolean
         */
        public function setRelation($linkid) {
-               if(!empty($linkid) && !empty($this->id)) {
+               if(!empty($linkid) && !empty($this->_id)) {
                        $queryStr = "INSERT IGNORE INTO `".DB_PREFIX."_tagrelation`
                                                        SET `linkid` = '".$this->DB->real_escape_string($linkid)."',
-                                                               `tagid` = '".$this->DB->real_escape_string($this->id)."'";
+                                                               `tagid` = '".$this->DB->real_escape_string($this->_id)."'";
                        $this->DB->query($queryStr);
                }
        }
@@ -99,18 +135,18 @@ class Tag {
        public function delete() {
                $ret = false;
 
-               if(!empty($this->id)) {
+               if(!empty($this->_id)) {
                        $this->DB->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
 
                        try {
                                $queryStr = "DELETE
                                        FROM `".DB_PREFIX."_tagrelation`
-                                       WHERE `tagid` = '".$this->DB->real_escape_string($this->id)."'";
+                                       WHERE `tagid` = '".$this->DB->real_escape_string($this->_id)."'";
                                $this->DB->query($queryStr);
 
                                $queryStr = "DELETE
                                        FROM `".DB_PREFIX."_tag`
-                                       WHERE `id` = '".$this->DB->real_escape_string($this->id)."'";
+                                       WHERE `id` = '".$this->DB->real_escape_string($this->_id)."'";
                                $this->DB->query($queryStr);
 
                                $this->DB->commit();
index 0e8a5563734bb5ca71ce1cd115be4e33d1fa0292..58128e91fbf1594205a50dcd2c523c46f04dacb1 100644 (file)
                        <div class="content">
                                <h4><a href="?p=overview&m=category&id=<?php echo urlencode($v['id']); ?>"><?php echo $v['name']; ?></a></h4>
                                <ul>
-<?php foreach ($links as $link) { ?>
+<?php foreach ($links['results'] as $link) { ?>
                                        <li><a class="" href="<?php echo $link['link']; ?>" target="_blank"><?php echo $link['title']; ?></a></li>
 <?php } ?>
                                </ul>
index 09eb95a6b91ccf08023e886ef15d0db6dbace251..2aa80c6b06ca4a2b93a767e67ed43a2becb8ba24 100644 (file)
@@ -51,9 +51,12 @@ $pagination = array('pages' => 0);
 switch($_requestMode) {
        case 'tag':
                if(!empty($_id)) {
-                       $linkCollection = $Management->linksByTag($_id,false,false);
-                       if(!empty($linkCollection)) {
-                               $subHeadline = $linkCollection[0]['tag'].' <i class="ion-md-pricetag"></i>';
+                       $linkCollection = $Management->linksByTag($_id,false,RESULTS_PER_PAGE, (RESULTS_PER_PAGE * ($_curPage-1)));
+                       if(!empty($linkCollection['results'])) {
+                               $tagObj = new Tag($DB);
+                               $tagObj->initbyid($_id);
+                               $tagname = $tagObj->getData('name');
+                               $subHeadline = $tagname.' <i class="ion-md-pricetag"></i>';
                        }
                }
                else {
@@ -64,9 +67,12 @@ switch($_requestMode) {
        break;
        case 'category':
                if(!empty($_id)) {
-                       $linkCollection = $Management->linksByCategory($_id,false,false);
-                       if(!empty($linkCollection)) {
-                               $subHeadline = $linkCollection[0]['category'].' <i class="ion-md-filing"></i>';
+                       $linkCollection = $Management->linksByCategory($_id,false,RESULTS_PER_PAGE, (RESULTS_PER_PAGE * ($_curPage-1)));
+                       if(!empty($linkCollection['results'])) {
+                               $catObj = new Category($DB);
+                               $catObj->initbyid($_id);
+                               $catname = $catObj->getData('name');
+                               $subHeadline = $catname.' <i class="ion-md-filing"></i>';
                        }
                }
                else {
@@ -79,11 +85,14 @@ switch($_requestMode) {
        default:
                # show all
                $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(!empty($linkCollection['amount'])) {
+       $pagination['pages'] = ceil($linkCollection['amount'] / RESULTS_PER_PAGE);
+       $pagination['curPage'] = $_curPage;
+       $pagination['linkadd'] = '&m='.$_requestMode;
+       if(!empty($_id)) {
+               $pagination['linkadd'] .= '&id='.$_id;
+       }
 }
 
 if($pagination['pages'] > 11) {
index c9dbc383ecc04957d61658d7c7d3d1793e78a531..405cb24f7cf90a17949e5883d56827be30959562 100644 (file)
                </div>
        </div>
 
-       <?php if($pagination['pages'] > 0) { ?>
+       <?php if($pagination['pages'] > 1) { ?>
                <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).'" 
+                               echo '<a href="index.php?p=overview'.$pagination['linkadd'].'&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).'" 
+                               echo '<a href="index.php?p=overview'.$pagination['linkadd'].'&page='.($pagination['curPage']+1).'" 
                                        class="pagination-next">Next</a>';
                        }
                        ?>
@@ -70,7 +70,7 @@
                                        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 . '"
+                                               echo '<li><a href="index.php?p=overview' . $pagination['linkadd'] . '&page=' . $i . '"
                                                class="pagination-link ' . $active . '"
                                                aria-label="Goto page ' . $i . '">' . $i . '</a></li>';
                                        }