Skip to content

Commit

Permalink
fix: sort order after edit subscription in case the cookie is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
ellite authored Sep 4, 2024
1 parent f0190e6 commit 87809fe
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 106 deletions.
94 changes: 48 additions & 46 deletions endpoints/subscriptions/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion includes/version.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
$version = "v2.23.1";
$version = "v2.23.2";
?>
119 changes: 60 additions & 59 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 87809fe

Please sign in to comment.