]> 91.132.146.200 Git - insipid.git/commitdiff
rewriting code, sorting and fixing sql statements
authorBanana <banana@optimus.de>
Tue, 16 Jul 2019 11:53:05 +0000 (13:53 +0200)
committerBanana <banana@optimus.de>
Tue, 16 Jul 2019 11:53:05 +0000 (13:53 +0200)
webroot/lib/category.class.php
webroot/lib/management.class.php
webroot/view/home.php
webroot/view/overview.inc.php
webroot/view/overview.php

index be4b5ad92db0e232d24521e90b28660856912643..197d222572d049767cc71645a3c30ffa54b43ce3 100644 (file)
@@ -81,7 +81,7 @@ class Category {
     /**
      * set the relation to the given link to the loaded category
      * @param int $linkid
-     * @return boolean
+     * @return void
      */
     public function setRelation($linkid) {
         if(!empty($linkid) && !empty($this->id)) {
index 0b4fc17f6117c61bc0e678cf6b009a6805e72c7b..89d1e44960d65fb8fabbb54a8bc716f6420f71f3 100644 (file)
@@ -33,6 +33,19 @@ class Management {
      */
     private $DB;
 
+    protected $COMBINED_SELECT_VALUES = "any_value(`id`) as id,
+                               any_value(`link`) as link,
+                               any_value(`created`) as created,
+                               any_value(`status`) as `status`,
+                               any_value(`description`) as description,
+                               any_value(`title`) as title,
+                               any_value(`image`) as image,
+                               any_value(`hash`) as hash,
+                               any_value(`tag`) as tag,
+                               any_value(`category`) as category,
+                               any_value(`categoryId`) as categoryId,
+                               any_value(`tagId`) as tagId";
+
     public function __construct($databaseConnectionObject) {
         $this->DB = $databaseConnectionObject;
     }
@@ -40,19 +53,47 @@ class Management {
        /**
         * get all the available categories from the DB.
         * optional limit
+        * optional stats
         * @param bool | int $limit
+        * @param bool $stats
         * @return array
         */
-    public function categories($limit=false) {
+    public function categories($limit=false, $stats=false) {
         $ret = array();
+               $statsInfo = array();
+
+               if($stats === true) {
+                       $queryStr = "SELECT 
+                               COUNT(*) as amount,
+                               any_value(categoryid) as categoryId
+                               FROM `".DB_PREFIX."_categoryrelation` 
+                               GROUP BY categoryid";
+                       $query = $this->DB->query($queryStr);
+                       if(!empty($query)) {
+                               while($result = $query->fetch_assoc()) {
+                                       $statsInfo[$result['categoryId']] = $result['amount'];
+                               }
+                       }
+               }
 
-        $queryStr = "SELECT * FROM `".DB_PREFIX."_category` ORDER BY `name`";
+        $queryStr = "SELECT
+                       any_value(`id`) as id,
+                       any_value(`name`) as name
+                       FROM `".DB_PREFIX."_category` 
+                       ORDER BY `name` ASC";
         if(!empty($limit)) {
             $queryStr .= " LIMIT $limit";
         }
         $query = $this->DB->query($queryStr);
         if(!empty($query)) {
-            $ret = $query->fetch_all(MYSQLI_ASSOC);
+               while($result = $query->fetch_assoc()) {
+                       if($stats === true) {
+                                       $ret[$result['id']] = array('name' => $result['name'], 'amount' => $statsInfo[$result['id']]);
+                               }
+                               else {
+                                       $ret[$result['id']] = array('name' => $result['name']);
+                               }
+                       }
         }
 
         return $ret;
@@ -61,26 +102,54 @@ class Management {
        /**
         * get all the available tags from the DB.
         * optional limit
+        * optional stats
         * @param bool | int $limit
+        * @param bool $stats
         * @return array
         */
-    public function tags($limit=false) {
+    public function tags($limit=false, $stats=false) {
         $ret = array();
+               $statsInfo = array();
+
+               if($stats === true) {
+                       $queryStr = "SELECT 
+                               COUNT(*) as amount,
+                               any_value(`tagid`) as tagId
+                               FROM `".DB_PREFIX."_tagrelation` 
+                               GROUP BY tagId";
+                       $query = $this->DB->query($queryStr);
+                       if(!empty($query)) {
+                               while($result = $query->fetch_assoc()) {
+                                       $statsInfo[$result['tagId']] = $result['amount'];
+                               }
+                       }
+               }
 
-        $queryStr = "SELECT * FROM `".DB_PREFIX."_tag` ORDER BY `name`";
+        $queryStr = "SELECT
+               any_value(`id`) as id,
+                       any_value(`name`) as name
+                       FROM `".DB_PREFIX."_tag` 
+                       ORDER BY `name` ASC";
         if(!empty($limit)) {
             $queryStr .= " LIMIT $limit";
         }
         $query = $this->DB->query($queryStr);
         if(!empty($query)) {
-            $ret = $query->fetch_all(MYSQLI_ASSOC);
+                       while($result = $query->fetch_assoc()) {
+                               if($stats === true) {
+                                       $ret[$result['id']] = array('name' => $result['name'], 'amount' => $statsInfo[$result['id']]);
+                               }
+                               else {
+                                       $ret[$result['id']] = array('name' => $result['name']);
+                               }
+                       }
         }
 
         return $ret;
     }
 
        /**
-        * return the latest addded links
+        * return the latest added links
         * @param int $limit
         * @return array
         */
@@ -106,51 +175,44 @@ class Management {
         $ret = array();
 
         $categories = $this->categories();
-        foreach($categories as $cat) {
-            $queryStr = "SELECT insipid_category.name, insipid_link.created
-                            FROM `insipid_category`
-                            LEFT JOIN insipid_categoryrelation ON insipid_categoryrelation.categoryid = insipid_category.id
-                            LEFT JOIN insipid_link ON insipid_link.id = insipid_categoryrelation.linkid
-                            WHERE insipid_category.id = '".$this->DB->real_escape_string($cat['id'])."'
-                            ORDER BY insipid_link.created DESC
-                            LIMIT 1";
-            $query = $this->DB->query($queryStr);
-            if(!empty($query) && $query->num_rows > 0) {
-                $result = $query->fetch_assoc();
-                $ret[$result['name']] = $result['created'];
-            }
+        foreach($categories as $k=>$v) {
+                       $latestLink = $this->latestLinkForCategory($k);
+                       if(!empty($latestLink)) {
+                               array_push($ret, array('created' => $latestLink[0]['created'], 'id' => $k, 'name' => $v['name']));
+                       }
         }
 
-        arsort($ret);
+               $_created  = array_column($ret, 'created');
+               array_multisort($_created, SORT_DESC, $ret);
 
         return $ret;
     }
 
        /**
-        * find all links by given category string.
+        * find all links by given category string or id.
         * Return array sorted by creation date DESC
+        * @param int $id
         * @param string $string
         * @param int $limit
         * @return array
         */
-    public function linksByCategoryString($string,$limit=5) {
+    public function linksByCategory($id,$string,$limit=5) {
         $ret = array();
 
-        $queryStr = "SELECT 
-                               any_value(`id`) as id,
-                               any_value(`link`) as link,
-                               any_value(`created`) as created,
-                               any_value(`status`) as status,
-                               any_value(`description`) as description,
-                               any_value(`title`) as title,
-                               any_value(`image`) as image,
-                               any_value(`hash`) as hash,
-                               any_value(`tag`) as tag,
-                               any_value(`category`) as category
+        $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES."
                        FROM `".DB_PREFIX."_combined`
-            WHERE `status` = 2
-                AND `category` = '".$this->DB->real_escape_string($string)."'
-            GROUP BY `hash`
+            WHERE `status` = 2";
+               if(!empty($id) && is_numeric($id)) {
+                       $queryStr .= " AND `categoryId` = '" . $this->DB->real_escape_string($id) . "'";
+               }
+               elseif(!empty($string) && is_string($string)) {
+                       $queryStr .= " AND `category` = '" . $this->DB->real_escape_string($string) . "'";
+               }
+               else {
+                       return $ret;
+               }
+
+               $queryStr .= "GROUP BY `hash`
             ORDER BY `created` DESC";
         if(!empty($limit)) {
             $queryStr .= " LIMIT $limit";
@@ -164,30 +226,30 @@ class Management {
     }
 
        /**
-        * find all links by given tag string.
+        * find all links by given tag string or id.
         * Return array sorted by creation date DESC
+        * @param int $id
         * @param string $string
         * @param int $limit
         * @return array
         */
-    public function linksByTagString($string,$limit=5) {
+    public function linksByTag($id,$string,$limit=5) {
         $ret = array();
 
-        $queryStr = "SELECT
-                               any_value(`id`) as id,
-                               any_value(`link`) as link,
-                               any_value(`created`) as created,
-                               any_value(`status`) as status,
-                               any_value(`description`) as description,
-                               any_value(`title`) as title,
-                               any_value(`image`) as image,
-                               any_value(`hash`) as hash,
-                               any_value(`tag`) as tag,
-                               any_value(`category`) as category 
+               $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES."
                        FROM `".DB_PREFIX."_combined`
-            WHERE `status` = 2
-                AND `tag` = '".$this->DB->real_escape_string($string)."'
-            GROUP BY `hash`
+            WHERE `status` = 2";
+               if(!empty($id) && is_numeric($id)) {
+                       $queryStr .= " AND `tagId` = '" . $this->DB->real_escape_string($id) . "'";
+               }
+               elseif(!empty($string) && is_string($string)) {
+                       $queryStr .= " AND `tag` = '" . $this->DB->real_escape_string($string) . "'";
+               }
+               else {
+                       return $ret;
+               }
+
+               $queryStr .= "GROUP BY `hash`
             ORDER BY `created` DESC";
         if(!empty($limit)) {
             $queryStr .= " LIMIT $limit";
@@ -208,17 +270,7 @@ class Management {
     public function links($limit=false) {
         $ret = array();
 
-        $queryStr = "SELECT
-                               any_value(`id`) as id,
-                               any_value(`link`) as link,
-                               any_value(`created`) as created,
-                               any_value(`status`) as status,
-                               any_value(`description`) as description,
-                               any_value(`title`) as title,
-                               any_value(`image`) as image,
-                               any_value(`hash`) as hash,
-                               any_value(`tag`) as tag,
-                               any_value(`category`) as category 
+        $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES."
                        FROM `".DB_PREFIX."_combined`
             WHERE `status` = 2
             GROUP BY `hash`
@@ -231,6 +283,29 @@ class Management {
         return $ret;
     }
 
+       /**
+        * return the latest added link for given category id
+        * @param int $categoryid
+        * @return array
+        */
+    public function latestLinkForCategory($categoryid) {
+       $ret = array();
+
+       if(!empty($categoryid) && is_numeric($categoryid)) {
+                       $queryStr = "SELECT ".$this->COMBINED_SELECT_VALUES."
+                       FROM `".DB_PREFIX."_combined`
+            WHERE `status` = 2
+            AND `categoryId` = '" . $this->DB->real_escape_string($categoryid) . "' 
+            ORDER BY `created` DESC
+            LIMIT 1";
+                       $query = $this->DB->query($queryStr);
+                       if(!empty($query) && $query->num_rows > 0) {
+                               $ret = $query->fetch_all(MYSQLI_ASSOC);
+                       }
+               }
+       return $ret;
+       }
+
     /**
      * for simpler management we have the search data in a separate column
      * it is not fancy or even technical nice but it damn works
@@ -272,4 +347,4 @@ class Management {
     }
 }
 
-?>
\ No newline at end of file
+?>
index 3bf73b4ae1e7f14f4084fd637364ecc8ad620c11..f448077d9cc861fba2246fe86b3918260d21d1b4 100644 (file)
        <div class="columns is-multiline">
 <?php
     if(!empty($orderedCategories)) {
-        foreach ($orderedCategories as $cat=>$date) {
-            $links = $Management->linksByCategoryString($cat);
+        foreach ($orderedCategories as $k=>$v) {
+            $links = $Management->linksByCategory($v['id'],false);
 ?>
        <div class="column is-one-quarter">
                <div class="content">
-                       <h4><a href="?p=overview&m=category&id=<?php echo urlencode($cat); ?>"><?php echo $cat; ?></a></h4>
+                       <h4><a href="?p=overview&m=category&id=<?php echo urlencode($v['id']); ?>"><?php echo $v['name']; ?></a></h4>
                                <div class="tags">
 <?php foreach ($links as $link) { ?>
                                        <a class="tag" href="<?php echo $link['link']; ?>" target="_blank"><?php echo $link['title']; ?></a>
index fe903b038b09e2b45f3fc3cbb5b4021a862da6c6..27b6aa3da27fb387f94e092d4d729b4ef5399019 100644 (file)
@@ -34,7 +34,7 @@ if(isset($_GET['m']) && !empty($_GET['m'])) {
 $_id = false;
 if(isset($_GET['id']) && !empty($_GET['id'])) {
     $_id = trim($_GET['id']);
-    $_id = Summoner::validate($_id,'nospace') ? $_id : false;
+    $_id = Summoner::validate($_id,'digit') ? $_id : false;
 }
 
 $linkCollection = array();
@@ -45,27 +45,27 @@ $categoryCollection = array();
 switch($_requestMode) {
     case 'tag':
         if(!empty($_id)) {
-            $linkCollection = $Management->linksByTagString($_id,false);
+            $linkCollection = $Management->linksByTag($_id,false,false);
             if(!empty($linkCollection)) {
                 $subHeadline = $linkCollection[0]['tag'].' <i class="ion-md-pricetag"></i>';
             }
         }
         else {
             # show all the tags we have
-            $tagCollection = $Management->tags();
+            $tagCollection = $Management->tags(false, true);
             $subHeadline = 'All the tags <i class="ion-md-pricetag"></i>';
         }
     break;
     case 'category':
         if(!empty($_id)) {
-            $linkCollection = $Management->linksByCategoryString($_id,false);
+            $linkCollection = $Management->linksByCategory($_id,false,false);
             if(!empty($linkCollection)) {
                 $subHeadline = $linkCollection[0]['category'].' <i class="ion-md-list"></i>';
             }
         }
         else {
             # show all the categories we have
-            $categoryCollection = $Management->categories();
+            $categoryCollection = $Management->categories(false, true);
             $subHeadline = 'All the categories <i class="ion-md-list"></i>';
         }
     break;
index 6eaa928c9459989ae74f7da2900e8bb158ee0ff3..d9ee020ed4886423fba8f296cc398c233c0efc8c 100644 (file)
@@ -46,7 +46,6 @@
 
        <div class="columns">
                <div class="column">
-               <h1 class="is-size-1">All of your links</h1>
                <?php if(!empty($subHeadline)) { ?>
                <h2 class="is-size-2"><?php echo $subHeadline; ?></h2>
                <?php } ?>
 <?php } if(!empty($tagCollection)) { ?>
 <div class="columns">
        <div class="column">
-               <ul>
-               <?php foreach ($tagCollection as $t) { ?>
-                       <li><a href="index.php?p=overview&m=tag&id=<?php echo urlencode($t['name']); ?>"><?php echo $t['name']; ?></a></li>
+               <table class="table">
+                       <tr>
+                               <th>Name</th>
+                               <th># of links</th>
+                       </tr>
+               <?php foreach ($tagCollection as $k=>$v) { ?>
+                       <tr>
+                               <td><a href="index.php?p=overview&m=tag&id=<?php echo urlencode($k); ?>"><?php echo $v['name']; ?></a></td>
+                               <td><?php echo $v['amount']; ?></td>
+                       </tr>
                <?php } ?>
-               </ul>
+               </table>
        </div>
 </div>
 <?php } if(!empty($categoryCollection)) { ?>
 <div class="columns">
        <div class="column">
-               <ul>
-               <?php foreach ($categoryCollection as $c) { ?>
-                       <li><a href="index.php?p=overview&m=category&id=<?php echo urlencode($c['name']); ?>"><?php echo $c['name']; ?></a></li>
+        <table class="table">
+            <tr>
+                <th>Name</th>
+                <th># of links</th>
+            </tr>
+               <?php foreach ($categoryCollection as $k=>$v) { ?>
+                       <tr>
+                <td><a href="index.php?p=overview&m=category&id=<?php echo urlencode($k); ?>"><?php echo $v['name']; ?></a></td>
+                <td><?php echo $v['amount']; ?></td>
+            </tr>
                <?php } ?>
-               </ul>
+        </table>
        </div>
 </div>
 <?php } ?>