diff --git a/src/controller/ResourceUpdateController.php b/src/controller/ResourceUpdateController.php index 92f4f67..61d045e 100644 --- a/src/controller/ResourceUpdateController.php +++ b/src/controller/ResourceUpdateController.php @@ -26,7 +26,7 @@ public function getResourceUpdates() { $out = array(); if (Req::checkIdParam()) { - $updates = $this->database->getResourceUpdates($_GET['id'], Req::page()); + $updates = $this->database->getResourceUpdates($_GET['id'], Req::page(), Req::sorting()); if (is_null($updates)) return NULL; foreach ($updates as $update) { diff --git a/src/support/Database.php b/src/support/Database.php index 0f71a84..e6990da 100644 --- a/src/support/Database.php +++ b/src/support/Database.php @@ -133,11 +133,17 @@ public function getResourceUpdate($update_id) { return NULL; } - public function getResourceUpdates($resource_id, $page) { + public function getResourceUpdates($resource_id, $page, $sorting = null) { $page = $page == 1 ? 0 : 10 * ($page - 1); + // Default sorting option for this method. + if (is_null($sorting)) $sorting = 'asc'; + if (!is_null($this->conn)) { - $updatesStmt = $this->conn->prepare($this->_resource_update('AND r.resource_id = :resource_id LIMIT 10 OFFSET :offset')); + // PDO tries to quote the sorting method. Can't bind it normally. Should be OK, sorting is enforced to be 'asc' or 'desc'. + $querySuffix = sprintf("AND r.resource_id = :resource_id ORDER BY r.resource_update_id %s LIMIT 10 OFFSET :offset", $sorting); + + $updatesStmt = $this->conn->prepare($this->_resource_update($querySuffix)); $updatesStmt->bindParam(':resource_id', $resource_id); $updatesStmt->bindParam(':offset', $page, \PDO::PARAM_INT); diff --git a/src/util/RequestUtil.php b/src/util/RequestUtil.php index 8f24488..3fa406b 100644 --- a/src/util/RequestUtil.php +++ b/src/util/RequestUtil.php @@ -74,6 +74,23 @@ public static function page() { return 1; } + public static function sorting() { + $value = $_GET['sort'] ?? NULL; + + // Preconditions + if (is_null($value) || !is_string($value)) return NULL; + + // Sorting methods + if(strcasecmp($value, 'asc') == 0) { + return 'asc'; + } else if (strcasecmp($value, 'desc') == 0) { + return 'desc'; + } + + // Return default null. This allows different defaults per method. + return NULL; + } + public static function category() { $value = $_GET['category'] ?? null;