From 29b96b123ec014039f07f1fc7de3ed82f1aa6f89 Mon Sep 17 00:00:00 2001 From: Banana Date: Sun, 29 Dec 2019 20:33:30 +0100 Subject: [PATCH] sorting in category view --- ChangeLog | 1 + webroot/lib/management.class.php | 54 ++++++++++++++++++++++---------- webroot/view/home.php | 2 +- webroot/view/overview.inc.php | 21 ++++++++++--- webroot/view/overview.php | 2 +- 5 files changed, 56 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index ab1cd4f..3a49000 100755 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,7 @@ version x.x - Guardian of Steel (tba) + Improved documentation + Delete single local storage of a link ++ Done by uncheck the store image locally option + + Sorting in category view + Fixed a bug in tag selection SQL query version 2.2 - Guardian of Ice - (2019-02-27) diff --git a/webroot/lib/management.class.php b/webroot/lib/management.class.php index 696c399..a91b3cc 100644 --- a/webroot/lib/management.class.php +++ b/webroot/lib/management.class.php @@ -229,18 +229,22 @@ class Management { return $ret; } - /** - * 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 - * @param bool $offset - * @return array - */ - public function linksByCategory($id, $string, $limit=5, $offset=false) { + /** + * find all links by given category string or id. + * Return array sorted by creation date DESC + * @param int $id Category ID + * @param string $string Category as string + * @param array $options Array with limit|offset|sort|sortDirection + * @return array + */ + public function linksByCategory($id, $string, $options=array()) { $ret = array(); + if(!isset($options['limit'])) $options['limit'] = 5; + if(!isset($options['offset'])) $options['offset'] = false; + if(!isset($options['sort'])) $options['sort'] = false; + if(!isset($options['sortDirection'])) $options['sortDirection'] = false; + $querySelect = "SELECT ".self::COMBINED_SELECT_VALUES; $queryFrom = " FROM `".DB_PREFIX."_combined` AS t"; $queryWhere = " WHERE ".$this->_decideLinkTypeForQuery(); @@ -254,16 +258,32 @@ class Management { return $ret; } - $queryOrder = "GROUP BY t.hash - ORDER BY t.created DESC"; + $queryGroup = " GROUP BY t.hash"; + $queryOrder = " ORDER BY"; + if(!empty($options['sort'])) { + $queryOrder .= ' t.'.$options['sort']; + } + else { + $queryOrder .= " t.created"; + } + if(!empty($options['sortDirection'])) { + $queryOrder .= ' '.$options['sortDirection']; + } + else { + $queryOrder .= " DESC"; + } + $queryLimit = ''; - if(!empty($limit)) { - $queryLimit .= " LIMIT $limit"; - if($offset !== false) { - $queryLimit .= " OFFSET $offset"; + # this allows the set the limit to false + if(!empty($options['limit'])) { + $queryLimit .= " LIMIT ".$options['limit']; + # offset can be 0 + if($options['offset'] !== false) { + $queryLimit .= " OFFSET ".$options['offset']; } } - $query = $this->DB->query($querySelect.$queryFrom.$queryWhere.$queryOrder.$queryLimit); + $query = $this->DB->query($querySelect.$queryFrom.$queryWhere.$queryGroup.$queryOrder.$queryLimit); + if(!empty($query) && $query->num_rows > 0) { while($result = $query->fetch_assoc()) { $linkObj = new Link($this->DB); diff --git a/webroot/view/home.php b/webroot/view/home.php index cdf4a64..59c07d6 100644 --- a/webroot/view/home.php +++ b/webroot/view/home.php @@ -221,7 +221,7 @@ $v) { - $links = $Management->linksByCategory($v['id'],false); + $links = $Management->linksByCategory($v['id'],''); ?>
diff --git a/webroot/view/overview.inc.php b/webroot/view/overview.inc.php index 5770d40..eea682b 100644 --- a/webroot/view/overview.inc.php +++ b/webroot/view/overview.inc.php @@ -73,13 +73,17 @@ if(Summoner::simpleAuthCheck() === true) { $sortLink['active'] = 'default'; $sortLink['activeDirection'] = false; -if(!empty($_sort) && $_sort === 'name') { - $currentGetParameters['s'] = 'name'; - $sortLink['active'] = 'name'; +$_saveSort = false; +if(!empty($_sort) && $_sort === 'title') { + $currentGetParameters['s'] = 'title'; + $sortLink['active'] = 'title'; + $_saveSort = 'title'; } +$_saveSortDirection = false; if(!empty($_sortDirection) && $_sortDirection === 'asc') { $currentGetParameters['sd'] = 'asc'; $sortLink['activeDirection'] = true; + $_saveSortDirection = 'asc'; } switch($_requestMode) { @@ -109,7 +113,14 @@ switch($_requestMode) { $catname = $catObj->getData('name'); $subHeadline = $catname.' '; - $linkCollection = $Management->linksByCategory($_id,false,RESULTS_PER_PAGE, (RESULTS_PER_PAGE * ($_curPage-1))); + $_options = array( + 'limit' => RESULTS_PER_PAGE, + 'offset' =>(RESULTS_PER_PAGE * ($_curPage-1)) + ); + if(!empty($_saveSort)) $_options['sort'] = $_saveSort; + if(!empty($_saveSortDirection)) $_options['sortDirection'] = $_saveSortDirection; + + $linkCollection = $Management->linksByCategory($_id,'',$_options); $currentGetParameters['id'] = $_id; } @@ -161,5 +172,5 @@ else { } $sortLink['default'] = Summoner::createFromParameterLinkQuery($currentGetParameters,array('s'=>false,'sd'=>false)); -$sortLink['name'] = Summoner::createFromParameterLinkQuery($currentGetParameters,array('s'=>'name','sd'=>false)); +$sortLink['name'] = Summoner::createFromParameterLinkQuery($currentGetParameters,array('s'=>'title','sd'=>false)); $sortLink['direction'] = Summoner::createFromParameterLinkQuery($currentGetParameters,array('sd'=>'asc')); \ No newline at end of file diff --git a/webroot/view/overview.php b/webroot/view/overview.php index b0335c3..081bd32 100644 --- a/webroot/view/overview.php +++ b/webroot/view/overview.php @@ -97,7 +97,7 @@ default name + class="button is-small is-link">title
-- 2.39.5