From 3ca9227b25cc19b058b693b5d22b22fd25a4c9ee Mon Sep 17 00:00:00 2001 From: Miguel Ribeiro Date: Wed, 4 Sep 2024 22:52:29 +0200 Subject: [PATCH 1/2] fix: sort order after edit subscription in case the cookie is missing --- endpoints/subscriptions/get.php | 93 +++++++++++++------------ includes/version.php | 2 +- index.php | 119 ++++++++++++++++---------------- 3 files changed, 108 insertions(+), 106 deletions(-) diff --git a/endpoints/subscriptions/get.php b/endpoints/subscriptions/get.php index 2956839f2..e9fe62ab2 100644 --- a/endpoints/subscriptions/get.php +++ b/endpoints/subscriptions/get.php @@ -30,48 +30,48 @@ if (isset($_GET['categories']) && $_GET['categories'] != "") { $allCategories = explode(',', $_GET['categories']); - $placeholders = array_map(function($idx) { - return ":categories{$idx}"; + $placeholders = array_map(function ($idx) { + return ":categories{$idx}"; }, array_keys($allCategories)); - $sql .= " AND (" . implode(' OR ', array_map(function($placeholder) { - return "category_id = {$placeholder}"; + $sql .= " AND (" . implode(' OR ', array_map(function ($placeholder) { + return "category_id = {$placeholder}"; }, $placeholders)) . ")"; foreach ($allCategories as $idx => $category) { - $params[":categories{$idx}"] = $category; + $params[":categories{$idx}"] = $category; } -} + } -if (isset($_GET['payments']) && $_GET['payments'] !== "") { - $allPayments = explode(',', $_GET['payments']); - $placeholders = array_map(function($idx) { + if (isset($_GET['payments']) && $_GET['payments'] !== "") { + $allPayments = explode(',', $_GET['payments']); + $placeholders = array_map(function ($idx) { return ":payments{$idx}"; - }, array_keys($allPayments)); + }, array_keys($allPayments)); - $sql .= " AND (" . implode(' OR ', array_map(function($placeholder) { + $sql .= " AND (" . implode(' OR ', array_map(function ($placeholder) { return "payment_method_id = {$placeholder}"; - }, $placeholders)) . ")"; + }, $placeholders)) . ")"; - foreach ($allPayments as $idx => $payment) { + foreach ($allPayments as $idx => $payment) { $params[":payments{$idx}"] = $payment; + } } -} -if (isset($_GET['members']) && $_GET['members'] != "") { - $allMembers = explode(',', $_GET['members']); - $placeholders = array_map(function($idx) { + if (isset($_GET['members']) && $_GET['members'] != "") { + $allMembers = explode(',', $_GET['members']); + $placeholders = array_map(function ($idx) { return ":members{$idx}"; - }, array_keys($allMembers)); + }, array_keys($allMembers)); - $sql .= " AND (" . implode(' OR ', array_map(function($placeholder) { + $sql .= " AND (" . implode(' OR ', array_map(function ($placeholder) { return "payer_user_id = {$placeholder}"; - }, $placeholders)) . ")"; + }, $placeholders)) . ")"; - foreach ($allMembers as $idx => $member) { + foreach ($allMembers as $idx => $member) { $params[":members{$idx}"] = $member; + } } -} if (isset($_GET['state']) && $_GET['state'] != "") { $sql .= " AND inactive = :inactive"; @@ -80,41 +80,42 @@ if (isset($_COOKIE['sortOrder']) && $_COOKIE['sortOrder'] != "") { $sort = $_COOKIE['sortOrder']; - $allowedSortCriteria = ['name', 'id', 'next_payment', 'price', 'payer_user_id', 'category_id', 'payment_method_id', 'inactive', 'alphanumeric']; - $order = ($sort == "price" || $sort == "id") ? "DESC" : "ASC"; + } - if ($sort == "alphanumeric") { - $sort = "name"; - } + $allowedSortCriteria = ['name', 'id', 'next_payment', 'price', 'payer_user_id', 'category_id', 'payment_method_id', 'inactive', 'alphanumeric']; + $order = ($sort == "price" || $sort == "id") ? "DESC" : "ASC"; - if (!in_array($sort, $allowedSortCriteria)) { - $sort = "next_payment"; - } + if ($sort == "alphanumeric") { + $sort = "name"; + } - $orderByClauses = []; + if (!in_array($sort, $allowedSortCriteria)) { + $sort = "next_payment"; + } - if ($settings['disabledToBottom'] === 'true') { - if (in_array($sort, ["payer_user_id", "category_id", "payment_method_id"])) { - $orderByClauses[] = "$sort $order"; - $orderByClauses[] = "inactive ASC"; - } else { - $orderByClauses[] = "inactive ASC"; - $orderByClauses[] = "$sort $order"; - } + $orderByClauses = []; + + if ($settings['disabledToBottom'] === 'true') { + if (in_array($sort, ["payer_user_id", "category_id", "payment_method_id"])) { + $orderByClauses[] = "$sort $order"; + $orderByClauses[] = "inactive ASC"; } else { + $orderByClauses[] = "inactive ASC"; $orderByClauses[] = "$sort $order"; - if ($sort != "inactive") { - $orderByClauses[] = "inactive ASC"; - } } - - if ($sort != "next_payment") { - $orderByClauses[] = "next_payment ASC"; + } else { + $orderByClauses[] = "$sort $order"; + if ($sort != "inactive") { + $orderByClauses[] = "inactive ASC"; } + } - $sql .= " ORDER BY " . implode(", ", $orderByClauses); + if ($sort != "next_payment") { + $orderByClauses[] = "next_payment ASC"; } + $sql .= " ORDER BY " . implode(", ", $orderByClauses); + $stmt = $db->prepare($sql); $stmt->bindValue(':userId', $userId, SQLITE3_INTEGER); diff --git a/includes/version.php b/includes/version.php index 4521a86bf..a45bed749 100644 --- a/includes/version.php +++ b/includes/version.php @@ -1,3 +1,3 @@ \ No newline at end of file diff --git a/index.php b/index.php index 3e12d7e64..42a2a6ae2 100644 --- a/index.php +++ b/index.php @@ -18,97 +18,98 @@ if (isset($_COOKIE['sortOrder']) && $_COOKIE['sortOrder'] != "") { $sort = $_COOKIE['sortOrder'] ?? 'next_payment'; - $sortOrder = $sort; - $allowedSortCriteria = ['name', 'id', 'next_payment', 'price', 'payer_user_id', 'category_id', 'payment_method_id', 'inactive', 'alphanumeric']; - $order = ($sort == "price" || $sort == "id") ? "DESC" : "ASC"; +} - if ($sort == "alphanumeric") { - $sort = "name"; - } +$sortOrder = $sort; +$allowedSortCriteria = ['name', 'id', 'next_payment', 'price', 'payer_user_id', 'category_id', 'payment_method_id', 'inactive', 'alphanumeric']; +$order = ($sort == "price" || $sort == "id") ? "DESC" : "ASC"; - if (!in_array($sort, $allowedSortCriteria)) { - $sort = "next_payment"; - } +if ($sort == "alphanumeric") { + $sort = "name"; +} + +if (!in_array($sort, $allowedSortCriteria)) { + $sort = "next_payment"; +} - $sql = "SELECT * FROM subscriptions WHERE user_id = :userId"; +$sql = "SELECT * FROM subscriptions WHERE user_id = :userId"; - if (isset($_GET['member'])) { - $memberIds = explode(',', $_GET['member']); - $placeholders = array_map(function ($key) { - return ":member{$key}"; - }, array_keys($memberIds)); +if (isset($_GET['member'])) { + $memberIds = explode(',', $_GET['member']); + $placeholders = array_map(function ($key) { + return ":member{$key}"; + }, array_keys($memberIds)); - $sql .= " AND payer_user_id IN (" . implode(',', $placeholders) . ")"; + $sql .= " AND payer_user_id IN (" . implode(',', $placeholders) . ")"; - foreach ($memberIds as $key => $memberId) { - $params[":member{$key}"] = $memberId; - } + foreach ($memberIds as $key => $memberId) { + $params[":member{$key}"] = $memberId; } +} - if (isset($_GET['category'])) { - $categoryIds = explode(',', $_GET['category']); - $placeholders = array_map(function ($key) { - return ":category{$key}"; - }, array_keys($categoryIds)); +if (isset($_GET['category'])) { + $categoryIds = explode(',', $_GET['category']); + $placeholders = array_map(function ($key) { + return ":category{$key}"; + }, array_keys($categoryIds)); - $sql .= " AND category_id IN (" . implode(',', $placeholders) . ")"; + $sql .= " AND category_id IN (" . implode(',', $placeholders) . ")"; - foreach ($categoryIds as $key => $categoryId) { - $params[":category{$key}"] = $categoryId; - } + foreach ($categoryIds as $key => $categoryId) { + $params[":category{$key}"] = $categoryId; } +} - if (isset($_GET['payment'])) { - $paymentIds = explode(',', $_GET['payment']); - $placeholders = array_map(function ($key) { - return ":payment{$key}"; - }, array_keys($paymentIds)); +if (isset($_GET['payment'])) { + $paymentIds = explode(',', $_GET['payment']); + $placeholders = array_map(function ($key) { + return ":payment{$key}"; + }, array_keys($paymentIds)); - $sql .= " AND payment_method_id IN (" . implode(',', $placeholders) . ")"; + $sql .= " AND payment_method_id IN (" . implode(',', $placeholders) . ")"; - foreach ($paymentIds as $key => $paymentId) { - $params[":payment{$key}"] = $paymentId; - } + foreach ($paymentIds as $key => $paymentId) { + $params[":payment{$key}"] = $paymentId; } +} - if (!isset($settings['hideDisabledSubscriptions']) || $settings['hideDisabledSubscriptions'] !== 'true') { - if (isset($_GET['state']) && $_GET['state'] != "") { - $sql .= " AND inactive = :inactive"; - $params[':inactive'] = $_GET['state']; - } +if (!isset($settings['hideDisabledSubscriptions']) || $settings['hideDisabledSubscriptions'] !== 'true') { + if (isset($_GET['state']) && $_GET['state'] != "") { + $sql .= " AND inactive = :inactive"; + $params[':inactive'] = $_GET['state']; } +} - $orderByClauses = []; +$orderByClauses = []; - if ($settings['disabledToBottom'] === 'true') { - if (in_array($sort, ["payer_user_id", "category_id", "payment_method_id"])) { - $orderByClauses[] = "$sort $order"; - $orderByClauses[] = "inactive ASC"; - } else { - $orderByClauses[] = "inactive ASC"; - $orderByClauses[] = "$sort $order"; - } +if ($settings['disabledToBottom'] === 'true') { + if (in_array($sort, ["payer_user_id", "category_id", "payment_method_id"])) { + $orderByClauses[] = "$sort $order"; + $orderByClauses[] = "inactive ASC"; } else { + $orderByClauses[] = "inactive ASC"; $orderByClauses[] = "$sort $order"; - if ($sort != "inactive") { - $orderByClauses[] = "inactive ASC"; - } } - - if ($sort != "next_payment") { - $orderByClauses[] = "next_payment ASC"; +} else { + $orderByClauses[] = "$sort $order"; + if ($sort != "inactive") { + $orderByClauses[] = "inactive ASC"; } +} - $sql .= " ORDER BY " . implode(", ", $orderByClauses); +if ($sort != "next_payment") { + $orderByClauses[] = "next_payment ASC"; } +$sql .= " ORDER BY " . implode(", ", $orderByClauses); + $stmt = $db->prepare($sql); $stmt->bindValue(':userId', $userId, SQLITE3_INTEGER); if (!empty($params)) { foreach ($params as $key => $value) { - $stmt->bindValue($key, $value, SQLITE3_INTEGER); + $stmt->bindValue($key, $value, SQLITE3_INTEGER); } } From 45320fbcea58b1ee065f652d0840e70592419002 Mon Sep 17 00:00:00 2001 From: Miguel Ribeiro Date: Wed, 4 Sep 2024 23:04:23 +0200 Subject: [PATCH 2/2] more sorting fixes --- endpoints/subscriptions/get.php | 1 + 1 file changed, 1 insertion(+) diff --git a/endpoints/subscriptions/get.php b/endpoints/subscriptions/get.php index e9fe62ab2..487490723 100644 --- a/endpoints/subscriptions/get.php +++ b/endpoints/subscriptions/get.php @@ -82,6 +82,7 @@ $sort = $_COOKIE['sortOrder']; } + $sortOrder = $sort; $allowedSortCriteria = ['name', 'id', 'next_payment', 'price', 'payer_user_id', 'category_id', 'payment_method_id', 'inactive', 'alphanumeric']; $order = ($sort == "price" || $sort == "id") ? "DESC" : "ASC";