From 87809fea71b92c7518173fedd189d7e76ce11bfb Mon Sep 17 00:00:00 2001 From: Miguel Ribeiro Date: Wed, 4 Sep 2024 23:09:14 +0200 Subject: [PATCH] fix: sort order after edit subscription in case the cookie is missing --- endpoints/subscriptions/get.php | 94 +++++++++++++------------ includes/version.php | 2 +- index.php | 119 ++++++++++++++++---------------- 3 files changed, 109 insertions(+), 106 deletions(-) diff --git a/endpoints/subscriptions/get.php b/endpoints/subscriptions/get.php index 2956839f2..487490723 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,43 @@ 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"; - } + $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"; + } - $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); } }