From: Banana Date: Tue, 16 Jul 2019 11:53:05 +0000 (+0200) Subject: rewriting code, sorting and fixing sql statements X-Git-Tag: 2.1-alpha-2019-0-29~19 X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=b93a8f92337b4aceb8f51861d8f8296dba8a63f0;p=insipid.git rewriting code, sorting and fixing sql statements --- diff --git a/webroot/lib/category.class.php b/webroot/lib/category.class.php index be4b5ad..197d222 100644 --- a/webroot/lib/category.class.php +++ b/webroot/lib/category.class.php @@ -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)) { diff --git a/webroot/lib/management.class.php b/webroot/lib/management.class.php index 0b4fc17..89d1e44 100644 --- a/webroot/lib/management.class.php +++ b/webroot/lib/management.class.php @@ -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 +?> diff --git a/webroot/view/home.php b/webroot/view/home.php index 3bf73b4..f448077 100644 --- a/webroot/view/home.php +++ b/webroot/view/home.php @@ -224,12 +224,12 @@
$date) { - $links = $Management->linksByCategoryString($cat); + foreach ($orderedCategories as $k=>$v) { + $links = $Management->linksByCategory($v['id'],false); ?>
-

+

diff --git a/webroot/view/overview.inc.php b/webroot/view/overview.inc.php index fe903b0..27b6aa3 100644 --- a/webroot/view/overview.inc.php +++ b/webroot/view/overview.inc.php @@ -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'].' '; } } else { # show all the tags we have - $tagCollection = $Management->tags(); + $tagCollection = $Management->tags(false, true); $subHeadline = 'All the tags '; } break; case 'category': if(!empty($_id)) { - $linkCollection = $Management->linksByCategoryString($_id,false); + $linkCollection = $Management->linksByCategory($_id,false,false); if(!empty($linkCollection)) { $subHeadline = $linkCollection[0]['category'].' '; } } else { # show all the categories we have - $categoryCollection = $Management->categories(); + $categoryCollection = $Management->categories(false, true); $subHeadline = 'All the categories '; } break; diff --git a/webroot/view/overview.php b/webroot/view/overview.php index 6eaa928..d9ee020 100644 --- a/webroot/view/overview.php +++ b/webroot/view/overview.php @@ -46,7 +46,6 @@
-

All of your links

@@ -88,21 +87,35 @@
-
    - -
  • + + + + + + $v) { ?> + + + + - +
    Name# of links
-
    - -
  • + + + + + + $v) { ?> + + + + - +
    Name# of links