From 8fa86cfb2b4df6f928299fd00938f7f7b6a20ac0 Mon Sep 17 00:00:00 2001 From: GitHabbo Date: Sun, 13 Nov 2022 17:29:20 +0100 Subject: [PATCH 1/4] Add pagination to housekeeping ban overview page --- .../org/alexdev/havana/dao/mysql/BanDao.java | 42 +++++++++++-------- .../HousekeepingBansController.java | 22 +++++++++- .../default-en/housekeeping/users_bans.tpl | 8 ++++ 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/Havana-Server/src/main/java/org/alexdev/havana/dao/mysql/BanDao.java b/Havana-Server/src/main/java/org/alexdev/havana/dao/mysql/BanDao.java index 47c52f6..daec830 100644 --- a/Havana-Server/src/main/java/org/alexdev/havana/dao/mysql/BanDao.java +++ b/Havana-Server/src/main/java/org/alexdev/havana/dao/mysql/BanDao.java @@ -115,28 +115,36 @@ public class BanDao { } } - public static List getActiveBans() { + public static List getActiveBans(int page, String sortBy) { List banList = new ArrayList<>(); - Connection sqlConnection = null; - PreparedStatement preparedStatement = null; - ResultSet resultSet = null; + int rows = 1; + int nextOffset = page * rows; - try { - sqlConnection = Storage.getStorage().getConnection(); - preparedStatement = Storage.getStorage().prepare("SELECT * FROM users_bans WHERE is_active = 1 ORDER BY banned_at DESC", sqlConnection); - resultSet = preparedStatement.executeQuery(); + if (nextOffset >= 0) { + Connection sqlConnection = null; + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; - while (resultSet.next()) { - banList.add(new Ban(BanType.valueOf(resultSet.getString("ban_type")), resultSet.getString("banned_value"), resultSet.getString("message"), resultSet.getTime("banned_until").getTime() / 1000L, - resultSet.getTime("banned_at").getTime() / 1000L, resultSet.getInt("banned_by"))); + try { + sqlConnection = Storage.getStorage().getConnection(); + preparedStatement = Storage.getStorage().prepare("SELECT * FROM users_bans WHERE is_active = 1 ORDER BY " + sortBy + " DESC LIMIT ? OFFSET ?", sqlConnection); + preparedStatement.setInt(1, rows); + preparedStatement.setInt(2, nextOffset); + + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + banList.add(new Ban(BanType.valueOf(resultSet.getString("ban_type")), resultSet.getString("banned_value"), resultSet.getString("message"), resultSet.getTime("banned_until").getTime() / 1000L, + resultSet.getTime("banned_at").getTime() / 1000L, resultSet.getInt("banned_by"))); + } + } catch (Exception e) { + Storage.logError(e); + } finally { + Storage.closeSilently(sqlConnection); + Storage.closeSilently(preparedStatement); + Storage.closeSilently(resultSet); } - } catch (Exception e) { - Storage.logError(e); - } finally { - Storage.closeSilently(sqlConnection); - Storage.closeSilently(preparedStatement); - Storage.closeSilently(resultSet); } return banList; diff --git a/Havana-Web/src/main/java/org/alexdev/http/controllers/housekeeping/HousekeepingBansController.java b/Havana-Web/src/main/java/org/alexdev/http/controllers/housekeeping/HousekeepingBansController.java index a72ab0d..7bd3e6c 100644 --- a/Havana-Web/src/main/java/org/alexdev/http/controllers/housekeeping/HousekeepingBansController.java +++ b/Havana-Web/src/main/java/org/alexdev/http/controllers/housekeeping/HousekeepingBansController.java @@ -15,6 +15,21 @@ public class HousekeepingBansController { return; } + int currentPage = 0; + + if (client.get().contains("page")) { + currentPage = Integer.parseInt(client.get().getString("page")); + } + + String sortBy = "banned_at"; + + if (client.get().contains("sort")) { + if (client.get().getString("sort").equals("banned_at") || + client.get().getString("sort").equals("banned_until")) { + sortBy = client.get().getString("sort"); + } + } + Template tpl = client.template("housekeeping/users_bans"); tpl.set("housekeepingManager", HousekeepingManager.getInstance()); @@ -25,7 +40,12 @@ public class HousekeepingBansController { return; } - tpl.set("bans", BanDao.getActiveBans()); + tpl.set("pageName", "Bans"); + tpl.set("bans", BanDao.getActiveBans(currentPage, sortBy)); + tpl.set("nextBans", BanDao.getActiveBans(currentPage + 1, sortBy)); + tpl.set("previousBans", BanDao.getActiveBans(currentPage - 1, sortBy)); + tpl.set("page", currentPage); + tpl.set("sortBy", sortBy); tpl.render(); // Delete alert after it's been rendered diff --git a/tools/www-tpl/default-en/housekeeping/users_bans.tpl b/tools/www-tpl/default-en/housekeeping/users_bans.tpl index 9e00c89..96269ae 100644 --- a/tools/www-tpl/default-en/housekeeping/users_bans.tpl +++ b/tools/www-tpl/default-en/housekeeping/users_bans.tpl @@ -5,6 +5,14 @@

View and manage bans

{% include "housekeeping/base/alert.tpl" %}

Manage all currently active bans on the hotel

+ {% if nextBans|length > 0 %} + {% set ourNextPage = page + 1 %} + + {% endif %} + {% if previousBans|length > 0 %} + {% set ourNextPage = page - 1 %} + + {% endif %}
From 81481d4994eaf204f653a9a16e9befaf339b0d6c Mon Sep 17 00:00:00 2001 From: GitHabbo Date: Sun, 13 Nov 2022 17:31:15 +0100 Subject: [PATCH 2/4] Add sort option to table titles --- tools/www-tpl/default-en/housekeeping/users_bans.tpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/www-tpl/default-en/housekeeping/users_bans.tpl b/tools/www-tpl/default-en/housekeeping/users_bans.tpl index 96269ae..6a1f268 100644 --- a/tools/www-tpl/default-en/housekeeping/users_bans.tpl +++ b/tools/www-tpl/default-en/housekeeping/users_bans.tpl @@ -21,8 +21,8 @@ - - + + From 1f62baaa647646efb2ef2a9b25c954678dae0226 Mon Sep 17 00:00:00 2001 From: GitHabbo Date: Sun, 13 Nov 2022 17:40:36 +0100 Subject: [PATCH 3/4] Fix wrong amount of rows --- .../src/main/java/org/alexdev/havana/dao/mysql/BanDao.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Havana-Server/src/main/java/org/alexdev/havana/dao/mysql/BanDao.java b/Havana-Server/src/main/java/org/alexdev/havana/dao/mysql/BanDao.java index daec830..49dbf26 100644 --- a/Havana-Server/src/main/java/org/alexdev/havana/dao/mysql/BanDao.java +++ b/Havana-Server/src/main/java/org/alexdev/havana/dao/mysql/BanDao.java @@ -118,7 +118,7 @@ public class BanDao { public static List getActiveBans(int page, String sortBy) { List banList = new ArrayList<>(); - int rows = 1; + int rows = 25; int nextOffset = page * rows; if (nextOffset >= 0) { From 24509c106e18e1ee0ae72e5f05c28acc4f62de5b Mon Sep 17 00:00:00 2001 From: GitHabbo Date: Sun, 13 Nov 2022 18:28:06 +0100 Subject: [PATCH 4/4] Apply same styling as on dashboard --- tools/www-tpl/default-en/housekeeping/users_bans.tpl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/www-tpl/default-en/housekeeping/users_bans.tpl b/tools/www-tpl/default-en/housekeeping/users_bans.tpl index 6a1f268..88326d3 100644 --- a/tools/www-tpl/default-en/housekeeping/users_bans.tpl +++ b/tools/www-tpl/default-en/housekeeping/users_bans.tpl @@ -5,6 +5,7 @@

View and manage bans

{% include "housekeeping/base/alert.tpl" %}

Manage all currently active bans on the hotel

+
{% if nextBans|length > 0 %} {% set ourNextPage = page + 1 %} @@ -13,6 +14,7 @@ {% set ourNextPage = page - 1 %} {% endif %} +
Type Value MessageBanned UtilBanned AtBanned UtilBanned At Banned By