From b8530e5da900f0d8d72ce28147e94d4d690e3b91 Mon Sep 17 00:00:00 2001
From: Banana <mail@bananas-playground.net>
Date: Tue, 27 Dec 2022 22:58:35 +0100
Subject: [PATCH] Fixed pagination and sort options in all links view

---
 ChangeLog                        |  1 +
 webroot/lib/management.class.php | 39 ++++++++++++++++++++++++--------
 webroot/view/overview.inc.php    |  4 ++--
 3 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4cb8f6c..d4698c1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,6 +6,7 @@ version 2.x - Deathwind Chapel ()
 	+ Fixed search index update
 	+ Fixed missing lang property
 	+ Fixed xml ex- and import
+	+ Fixed pagination and sort options in all links view
 
 version 2.8 - Wastelands (2022-12-10)
 
diff --git a/webroot/lib/management.class.php b/webroot/lib/management.class.php
index 74c9ab5..772c631 100644
--- a/webroot/lib/management.class.php
+++ b/webroot/lib/management.class.php
@@ -439,24 +439,45 @@ class Management {
 	/**
 	 * return all links and Info we have from the combined view
 	 *
-	 * @param int $limit
-	 * @param bool $offset
+	 * @param array $options
 	 * @return array
 	 */
-	public function links(int $limit=10, bool $offset=false): array {
+	public function links(array $options=array()): 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 `hash`";
 		$queryFrom = " FROM `".DB_PREFIX."_link` AS t";
 		$queryWhere = " WHERE ".$this->_decideLinkTypeForQuery();
-		$queryOrder = " ORDER BY `created` DESC";
-		$queryLimit = "";
-		if(!empty($limit)) {
-			$queryLimit = " LIMIT $limit";
-			if($offset !== false) {
-				$queryLimit .= " OFFSET $offset";
+
+		$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 = '';
+		# 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);
 		if(!empty($query) && $query->num_rows > 0) {
 			while($result = $query->fetch_assoc()) {
diff --git a/webroot/view/overview.inc.php b/webroot/view/overview.inc.php
index 146a5bd..8145e78 100644
--- a/webroot/view/overview.inc.php
+++ b/webroot/view/overview.inc.php
@@ -133,12 +133,12 @@ switch($_requestMode) {
 		$subHeadline = 'Awaiting moderation';
 		$Management->setShowAwm(true);
 
-		$linkCollection = $Management->links(RESULTS_PER_PAGE, (RESULTS_PER_PAGE * ($_curPage-1)));
+		$linkCollection = $Management->links($_LinkColllectionQueryOptions);
 	break;
 	case 'all':
 	default:
 		# show all
-		$linkCollection = $Management->links(RESULTS_PER_PAGE, (RESULTS_PER_PAGE * ($_curPage-1)));
+		$linkCollection = $Management->links($_LinkColllectionQueryOptions);
 }
 
 if(!empty($linkCollection['amount'])) {
-- 
2.39.5