Skip to content

Commit

Permalink
feat: add alphanumeric sort order for subscriptions (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellite authored Jul 14, 2024
1 parent 309d5a8 commit 775e6ee
Show file tree
Hide file tree
Showing 22 changed files with 53 additions and 14 deletions.
Empty file removed a.out
Empty file.
14 changes: 12 additions & 2 deletions endpoints/subscriptions/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
$sql = "SELECT * FROM subscriptions ORDER BY next_payment ASC, inactive ASC";
if (isset($_COOKIE['sortOrder']) && $_COOKIE['sortOrder'] != "") {
$sort = $_COOKIE['sortOrder'];
$allowedSortCriteria = ['name', 'id', 'next_payment', 'price', 'payer_user_id', 'category_id', 'payment_method_id', 'inactive'];
$sortOrder = $sort;
$allowedSortCriteria = ['name', 'id', 'next_payment', 'price', 'payer_user_id', 'category_id', 'payment_method_id', 'inactive', 'alphanumeric'];
if ($sort == "price" || $sort == "id") {
$order = "DESC";
}
if ($sort == "alphanumeric") {
$sort = "name";
}
if (!in_array($sort, $allowedSortCriteria)) {
$sort = "next_payment";
}
Expand Down Expand Up @@ -56,7 +60,7 @@
$params[':inactive'] = $_GET['state'];
}

$sql .= " ORDER BY $sort $order";
$sql .= " ORDER BY LOWER($sort) $order";
if ($sort != "next_payment") {
$sql .= ", next_payment ASC";
}
Expand Down Expand Up @@ -114,6 +118,12 @@
}
}

if ($sortOrder == "alphanumeric") {
usort($print, function ($a, $b) {
return strnatcmp(strtolower($a['name']), strtolower($b['name']));
});
}

if (isset($print)) {
printSubscriptions($print, $sort, $categories, $members, $i18n, $colorTheme, "../../");
}
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Neues Abonnement",
'search' => "Suche",
'state' => "Status",
'alphanumeric' => "Alphanumerisch",
'sort' => "Sortieren",
'name' => "Bezeichnung",
'last_added' => "Zuletzt hinzugefügt",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/el.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Νέα συνδρομή",
'search' => "Αναζήτηση",
'state' => "Κατάσταση",
'alphanumeric' => "Αλφαριθμητική",
'sort' => "Ταξινόμηση",
'name' => "Όνομα",
'last_added' => "Τελευταία προσθήκη",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "New Subscription",
'search' => "Search",
'state' => "State",
'alphanumeric' => "Alphanumeric",
'sort' => "Sort",
'name' => "Name",
'last_added' => "Last Added",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Nueva Suscripción",
'search' => "Buscar",
'state' => "Estado",
'alphanumeric' => "Alfanumérico",
'sort' => "Ordenar",
'name' => "Nombre",
'last_added' => "Última Añadida",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Nouvel abonnement",
'search' => "Rechercher",
'state' => "État",
'alphanumeric' => "Alphanumérique",
'sort' => "Trier",
'name' => "Nom",
'last_added' => "Dernier ajouté",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/it.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'new_subscription' => 'Nuovo abbonamento',
'search' => 'Cerca',
'state' => "Stato",
'alphanumeric' => 'Alfanumerico',
'sort' => 'Ordina',
'name' => 'Nome',
'last_added' => 'Ultimo aggiunto',
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/jp.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "新しい定期購入",
'search' => "検索",
'state' => "状態",
'alphanumeric' => "アルファベット順",
'sort' => "並べ替え",
'name' => "名前",
'last_added' => "最終追加日",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/ko.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "새 구독",
'search' => "검색",
'state' => "상태",
'alphanumeric' => "알파벳순",
'sort' => "정렬",
'name' => "이름",
'last_added' => "최근 등록",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/pl.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Nowa subskrypcja",
'search' => "Szukaj",
'state' => "Stan",
'alphanumeric' => "Alfanumeryczny",
'sort' => "Sortuj",
'name' => "Nazwa",
'last_added' => "Ostatnio dodane",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/pt.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Nova Subscrição",
'search' => "Pesquisar",
'state' => "Estado",
'alphanumeric' => "Alfanumérico",
'sort' => "Ordenar",
'name' => "Nome",
'last_added' => "Última Adicionada",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/pt_br.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Nova assinatura",
'search' => "Pesquisar",
'state' => "Estado",
'alphanumeric' => "Alfanumérico",
'sort' => "Ordenar",
'name' => "Nome",
'last_added' => "Última adicionada",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Новая подписка",
'search' => "Поиск",
'state' => "Состояние",
'alphanumeric' => "Алфавитный порядок",
'sort' => "Сортировка",
'name' => "Имя",
'last_added' => "Дата создания",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/sl.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Nova naročnina",
'search' => "Iskanje",
'state' => "Stanje",
'alphanumeric' => "Abecedno",
'sort' => "Razvrsti",
'name' => "Ime",
'last_added' => "Zadnje dodano",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/sr.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Нова претплата",
'search' => "Претрага",
'state' => "Статус",
'alphanumeric' => "Алфанумерички",
'sort' => "Сортирај",
'name' => "Назив",
'last_added' => "Последње додато",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/sr_lat.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Nova pretplata",
'search' => "Pretraga",
'state' => "Stanje",
'alphanumeric' => "Alfanumerički",
'sort' => "Sortiraj",
'name' => "Naziv",
'last_added' => "Poslednje dodato",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/tr.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "Yeni Abonelik",
'search' => "Ara",
'state' => "Durum",
'alphanumeric' => "Alfanümerik",
'sort' => "Sırala",
'name' => "İsim",
'last_added' => "Son Eklenen",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/zh_cn.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'new_subscription' => "新订阅",
'search' => "搜索",
'state' => "状态",
'alphanumeric' => "名称",
'sort' => "排序",
'name' => "名称",
'last_added' => "创建时间",
Expand Down
1 change: 1 addition & 0 deletions includes/i18n/zh_tw.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'new_subscription' => "新訂閱",
'search' => "搜尋",
'state' => "狀態",
'alphanumeric' => "字母數字",
'sort' => "排序",
'name' => "名稱",
'last_added' => "建立時間",
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.18.0";
$version = "v2.19.0";
?>
33 changes: 22 additions & 11 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
$sql = "SELECT * FROM subscriptions WHERE user_id = :userId ORDER BY next_payment ASC, inactive ASC";
if (isset($_COOKIE['sortOrder']) && $_COOKIE['sortOrder'] != "") {
$sort = $_COOKIE['sortOrder'];
$allowedSortCriteria = ['name', 'id', 'next_payment', 'price', 'payer_user_id', 'category_id', 'payment_method_id', 'inactive'];
$sortOrder = $sort;
$allowedSortCriteria = ['name', 'id', 'next_payment', 'price', 'payer_user_id', 'category_id', 'payment_method_id', 'inactive', 'alphanumeric'];
$order = "ASC";
if ($sort == "price" || $sort == "id") {
$order = "DESC";
}
if ($sort == "alphanumeric") {
$sort = "name";
}
if (!in_array($sort, $allowedSortCriteria)) {
$sort = "next_payment";
}

$sql = "SELECT * FROM subscriptions WHERE user_id = :userId";
$sql .= " ORDER BY $sort $order";
$sql .= " ORDER BY LOWER($sort) $order";
if ($sort != "next_payment") {
$sql .= ", next_payment ASC";
}
Expand Down Expand Up @@ -165,32 +169,33 @@
</button>
<div class="sort-options" id="sort-options">
<ul>
<li <?= $sort == "name" ? 'class="selected"' : "" ?> onClick="setSortOption('name')" id="sort-name">
<li <?= $sortOrder == "name" ? 'class="selected"' : "" ?> onClick="setSortOption('name')" id="sort-name">
<?= translate('name', $i18n) ?>
</li>
<li <?= $sort == "id" ? 'class="selected"' : "" ?> onClick="setSortOption('id')" id="sort-id">
<li <?= $sortOrder == "id" ? 'class="selected"' : "" ?> onClick="setSortOption('id')" id="sort-id">
<?= translate('last_added', $i18n) ?>
</li>
<li <?= $sort == "price" ? 'class="selected"' : "" ?> onClick="setSortOption('price')" id="sort-price">
<li <?= $sortOrder == "price" ? 'class="selected"' : "" ?> onClick="setSortOption('price')" id="sort-price">
<?= translate('price', $i18n) ?>
</li>
<li <?= $sort == "next_payment" ? 'class="selected"' : "" ?> onClick="setSortOption('next_payment')"
<li <?= $sortOrder == "next_payment" ? 'class="selected"' : "" ?> onClick="setSortOption('next_payment')"
id="sort-next_payment"><?= translate('next_payment', $i18n) ?></li>
<li <?= $sort == "payer_user_id" ? 'class="selected"' : "" ?> onClick="setSortOption('payer_user_id')"
<li <?= $sortOrder == "payer_user_id" ? 'class="selected"' : "" ?> onClick="setSortOption('payer_user_id')"
id="sort-payer_user_id"><?= translate('member', $i18n) ?></li>
<li <?= $sort == "category_id" ? 'class="selected"' : "" ?> onClick="setSortOption('category_id')"
<li <?= $sortOrder == "category_id" ? 'class="selected"' : "" ?> onClick="setSortOption('category_id')"
id="sort-category_id"><?= translate('category', $i18n) ?></li>
<li <?= $sort == "payment_method_id" ? 'class="selected"' : "" ?> onClick="setSortOption('payment_method_id')"
<li <?= $sortOrder == "payment_method_id" ? 'class="selected"' : "" ?> onClick="setSortOption('payment_method_id')"
id="sort-payment_method_id"><?= translate('payment_method', $i18n) ?></li>
<?php
if (!isset($settings['hideDisabledSubscriptions']) || $settings['hideDisabledSubscriptions'] !== 'true') {
?>
<li <?= $sort == "inactive" ? 'class="selected"' : "" ?> onClick="setSortOption('inactive')"
<li <?= $sortOrder == "inactive" ? 'class="selected"' : "" ?> onClick="setSortOption('inactive')"
id="sort-inactive"><?= translate('state', $i18n) ?></li>
<?php
}
?>

<li <?= $sortOrder == "alphanumeric" ? 'class="selected"' : "" ?> onClick="setSortOption('alphanumeric')"
id="sort-alphanumeric"><?= translate('alphanumeric', $i18n) ?></li>
</ul>
</div>
</div>
Expand Down Expand Up @@ -233,6 +238,12 @@
}
}

if ($sortOrder == "alphanumeric") {
usort($print, function ($a, $b) {
return strnatcmp(strtolower($a['name']), strtolower($b['name']));
});
}

if (isset($print)) {
printSubscriptions($print, $sort, $categories, $members, $i18n, $colorTheme, "");
}
Expand Down

0 comments on commit 775e6ee

Please sign in to comment.