Skip to content

Commit

Permalink
feat: allow renaming / translation of payment methods
Browse files Browse the repository at this point in the history
feat: allow deletion of the default payment methods
  • Loading branch information
ellite committed Mar 4, 2024
1 parent 1a7c4e1 commit fb3f689
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 10 deletions.
2 changes: 1 addition & 1 deletion endpoints/payments/get.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<?= $payment['name'] ?>
</span>
<?php
if ($payment['id'] > 31 && !$inUse) {
if (!$inUse) {
?>
<div class="delete-payment-method" title="<?= translate('delete', $i18n) ?>" data-paymentid="<?= $payment['id'] ?>" onclick="deletePaymentMethod(<?= $payment['id'] ?>)">
x
Expand Down
41 changes: 41 additions & 0 deletions endpoints/payments/rename.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

require_once '../../includes/connect_endpoint.php';
session_start();

if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
die(json_encode([
"success" => false,
"message" => translate('session_expired', $i18n)
]));
}

if (!isset($_POST['paymentId']) || !isset($_POST['name']) || $_POST['paymentId'] === '' || $_POST['name'] === '') {
die(json_encode([
"success" => false,
"message" => translate('fields_missing', $i18n)
]));
}

$paymentId = $_POST['paymentId'];
$name = $_POST['name'];

$sql = "UPDATE payment_methods SET name = :name WHERE id = :paymentId";
$stmt = $db->prepare($sql);
$stmt->bindParam(':name', $name, SQLITE3_TEXT);
$stmt->bindParam(':paymentId', $paymentId, SQLITE3_INTEGER);
$result = $stmt->execute();

if ($result) {
echo json_encode([
"success" => true,
"message" => translate('payment_renamed', $i18n)
]);
} else {
echo json_encode([
"success" => false,
"message" => translate('payment_not_renamed', $i18n)
]);
}

?>
4 changes: 4 additions & 0 deletions includes/i18n/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@
"experimental_info" => "Experimentelle Einstellungen funktionieren möglicherweise nicht perfekt.",
"payment_methods" => "Zahlungsmethoden",
"payment_methods_info" => "Zahlungsmethode zum (de-)aktivieren anklicken.",
"rename_payment_methods_info" => "Klicken Sie auf den Namen einer Zahlungsmethode, um sie umzubenennen",
"cant_delete_payment_method_in_use" => "Genutzte Zahlungsmethoden können nicht deaktiviert werden",
"add_custom_payment" => "Eigene Zahlungsmethode hinzufügen",
"payment_method_name" => "Name der Zahlungsmethode",
"payment_method_added_successfuly" => "Zahlungsmethode erfolgreich hinzugefügt",
"disable" => "Deaktivieren",
"enable" => "Aktivieren",
"rename_payment_method" => "Zahlungsmethode umbenennen",
"payment_renamed" => "Zahlungsmethode umbenannt",
"payment_not_renamed" => "Zahlungsmethode konnte nicht umbenannt werden",
"test" => "Test",
"add" => "Hinzufügen",
"save" => "Speichern",
Expand Down
4 changes: 4 additions & 0 deletions includes/i18n/el.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@
"experimental_info" => "Οι πειραματικές ρυθμίσεις πιθανότατα δεν θα λειτουργούν τέλεια.",
"payment_methods" => "Τρόποι πληρωμής",
"payment_methods_info" => "Κάνε κλικ σε μια μέθοδο πληρωμής για να την απενεργοποιήσεις/ενεργοποιήσεις.",
"rename_payment_methods_info" => "Κάντε κλικ στο όνομα μιας μεθόδου πληρωμής για να τη μετονομάσετε.",
"cant_delete_payment_method_in_use" => "Δεν είναι εφικτό να απενεργοποιηθεί η χρησιμοποιούμενη μέθοδο πληρωμής",
"add_custom_payment" => "Προσθήκη προσαρμοσμένης μεθόδου πληρωμής",
"payment_method_name" => "Όνομα μεθόδου πληρωμής",
"payment_method_added_successfuly" => "Η μέθοδος πληρωμής προστέθηκε με επιτυχία",
"disable" => "Ανενεργό",
"enable" => "Ενεργό",
"rename_payment_method" => "Μετονομασία μεθόδου πληρωμής",
"payment_renamed" => "Η μέθοδος πληρωμής μετονομάστηκε",
"payment_not_renamed" => "Η μέθοδος πληρωμής δεν μετονομάστηκε",
"test" => "Δοκιμή",
"add" => "Προσθήκη",
"save" => "Αποθήκευση",
Expand Down
4 changes: 4 additions & 0 deletions includes/i18n/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@
"experimental_info" => "Experimental settings will probably not work perfectly.",
"payment_methods" => "Payment Methods",
"payment_methods_info" => "Click a payment method to disable / enable it.",
"rename_payment_methods_info" => "Click the name on a payment method to rename it.",
"cant_delete_payment_method_in_use" => "Can't disable used payment method",
"add_custom_payment" => "Add Custom Payment Method",
"payment_method_name" => "Payment Method Name",
"payment_method_added_successfuly" => "Payment method added successfully",
"disable" => "Disable",
"enable" => "Enable",
"rename_payment_method" => "Rename Payment Method",
"payment_renamed" => "Payment method renamed",
"payment_not_renamed" => "Payment method not renamed",
"test" => "Test",
"add" => "Add",
"save" => "Save",
Expand Down
4 changes: 4 additions & 0 deletions includes/i18n/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@
"experimental_info" => "Las configuraciones experimentales probablemente no funcionarán perfectamente.",
"payment_methods" => "Métodos de Pago",
"payment_methods_info" => "Haz clic en un método de pago para deshabilitarlo/habilitarlo.",
"rename_payment_methods_info" => "Haz clic en el nombre de un método de pago para cambiarle el nombre.",
"cant_delete_payment_method_in_use" => "No se puede desactivar el método de pago utilizado",
"add_custom_payment" => "Añadir método de pago personalizado",
"payment_method_name" => "Nombre del método de pago",
"payment_method_added_successfuly" => "Método de pago añadido con éxito",
"disable" => "Desactivar",
"enable" => "Activar",
"rename_payment_method" => "Renombrar método de pago",
"payment_renamed" => "Método de pago renombrado",
"payment_not_renamed" => "Error al renombrar el método de pago",
"test" => "Probar",
"add" => "Agregar",
"save" => "Guardar",
Expand Down
4 changes: 4 additions & 0 deletions includes/i18n/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@
"experimental_info" => "Les paramètres expérimentaux ne fonctionneront probablement pas parfaitement.",
"payment_methods" => "Méthodes de paiement",
"payment_methods_info" => "Cliquez sur une méthode de paiement pour la désactiver / l'activer.",
"rename_payment_methods_info" => "Cliquez sur le nom d'un mode de paiement pour le renommer.",
"cant_delete_payment_method_in_use" => "Impossible de désactiver la méthode de paiement utilisée",
"add_custom_payment" => "Ajouter un paiement personnalisé",
"payment_method_name" => "Nom de la méthode de paiement",
"payment_method_added_successfuly" => "Méthode de paiement ajoutée avec succès",
"disable" => "Désactiver",
"enable" => "Activer",
"rename_payment_method" => "Renommer la méthode de paiement",
"payment_renamed" => "Méthode de paiement renommée",
"payment_not_renamed" => "La méthode de paiement n'a pas été renommée",
"test" => "Test",
"add" => "Ajouter",
"save" => "Enregistrer",
Expand Down
4 changes: 4 additions & 0 deletions includes/i18n/jp.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@
"experimental_info" => "実験的な設定は、おそらく完全には機能しません。",
"payment_methods" => "支払い方法",
"payment_methods_info" => "支払い方法をクリックして無効/有効を切り替えます。",
"rename_payment_methods_info" => "支払い方法の名前をクリックして、名前を変更します。",
"cant_delete_payment_method_in_use" => "支払い方法が使用中のため無効にできません。",
"add_custom_payment" => "カスタム支払い方法を追加",
"payment_method_name" => "支払い方法名",
"payment_method_added_successfuly" => "支払い方法が追加されました",
"disable" => "無効",
"enable" => "有効",
"rename_payment_method" => "支払い方法の名前を変更",
"payment_renamed" => "支払い方法が変更されました",
"payment_not_renamed" => "支払い方法が変更されませんでした",
"test" => "テスト",
"add" => "追加",
"save" => "保存",
Expand Down
4 changes: 4 additions & 0 deletions includes/i18n/pt.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@
"experimental_info" => "Definições experimentais provavelmente não funcionarão correctamente.",
"payment_methods" => "Métodos de Pagamento",
"payment_methods_info" => "Clique num método de pagamento para o activar / desactivar.",
"rename_payment_methods_info" => "Clique no nome do método de pagamento para o renomear.",
"cant_delete_payment_method_in_use" => "Não pode desactivar metodo de pagamento em uso",
"add_custom_payment" => "Adicionar método de pagamento personalizado",
"payment_method_name" => "Nome do método de pagamento",
"payment_method_added_successfuly" => "Método de pagamento adicionado com sucesso",
"disable" => "Desactivar",
"enable" => "Activar",
"rename_payment_method" => "Renomear método de pagamento",
"payment_renamed" => "Método de pagamento renomeado",
"payment_not_renamed" => "Método de pagamento não renomeado",
"test" => "Testar",
"add" => "Adicionar",
"save" => "Guardar",
Expand Down
4 changes: 4 additions & 0 deletions includes/i18n/tr.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@
"experimental_info" => "Deneysel ayarlar muhtemelen mükemmel çalışmayacak.",
"payment_methods" => "Ödeme Yöntemleri",
"payment_methods_info" => "Bir ödeme yöntemini devre dışı bırakmak / etkinleştirmek için tıklayın.",
"rename_payment_methods_info" => "Yeniden adlandırmak için bir ödeme yönteminin adına tıklayın.",
"cant_delete_payment_method_in_use" => "Kullanımda olan ödeme yöntemini devre dışı bırakamazsınız",
"add_custom_payment" => "Özel ödeme yöntemi ekle",
"payment_method_name" => "Ödeme Yöntemi Adı",
"payment_method_added_successfuly" => "Ödeme yöntemi başarıyla eklendi",
"disable" => "Devre Dışı Bırak",
"enable" => "Etkinleştir",
"rename_payment_method" => "Ödeme yöntemi adını değiştir",
"payment_renamed" => "Ödeme yöntemi adı değiştirildi",
"payment_not_renamed" => "Ödeme yöntemi adı değiştirilemedi",
"test" => "Test Et",
"add" => "Ekle",
"save" => "Kaydet",
Expand Down
4 changes: 4 additions & 0 deletions includes/i18n/zh_cn.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,16 @@
"experimental_info" => "实验性设置,可能存在问题。",
"payment_methods" => "支付方式",
"payment_methods_info" => "点击支付方式以禁用/启用。",
"rename_payment_methods_info" => "点击付款方式名称,重新命名该付款方式。"
"cant_delete_payment_method_in_use" => "不能禁用正在使用的支付方式",
"add_custom_payment" => "添加自定义支付方式",
"payment_method_name" => "支付方式名称",
"payment_method_added_successfuly" => "支付方式已成功添加",
"disable" => "禁用",
"enable" => "启用",
"rename_payment_method" => "重命名支付方式",
"payment_renamed" => "支付方式已重命名",
"payment_not_renamed" => "支付方式未重命名",
"test" => "测试",
"add" => "添加",
"save" => "保存",
Expand Down
4 changes: 4 additions & 0 deletions includes/i18n/zh_tw.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@
"experimental_info" => "實驗性設定,可能存在問題。",
"payment_methods" => "付款方式",
"payment_methods_info" => "點選付款方式以停用/啟用。",
"rename_payment_methods_info" => "點選付款方式的名稱可對其進行重新命名。",
"cant_delete_payment_method_in_use" => "無法停用正在使用的付款方式",
"add_custom_payment" => "新增自訂付款方式",
"payment_method_name" => "付款方式名稱",
"payment_method_added_successfuly" => "付款方式已成功新增",
"disable" => "停用",
"enable" => "啟用",
"rename_payment_method" => "更改付款方式名稱",
"payment_renamed" => "付款方式名稱已更改",
"payment_not_renamed" => "付款方式名稱未更改",
"test" => "測試",
"add" => "新增",
"save" => "儲存",
Expand Down
65 changes: 64 additions & 1 deletion scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ function togglePayment(paymentId) {
const element = document.querySelector(`div[data-paymentid="${paymentId}"]`);

if (element.dataset.inUse === 'yes') {
return showErrorMessage(translate(cant_disable_payment_in_use));
return showErrorMessage(translate('cant_disable_payment_in_use'));
}

const newEnabledState = element.dataset.enabled === '1' ? '0' : '1';
Expand All @@ -454,6 +454,69 @@ function togglePayment(paymentId) {
});
}

document.body.addEventListener('click', function(e) {
let targetElement = e.target;
do {
if (targetElement.classList && targetElement.classList.contains('payments-payment')) {
let targetChild = e.target;
do {
if (targetChild.classList && targetChild.classList.contains('payment-name')) {
return;
}
targetChild = targetChild.parentNode;
} while (targetChild && targetChild !== targetElement);

const paymentId = targetElement.dataset.paymentid;
togglePayment(paymentId);
return;
}
targetElement = targetElement.parentNode;
} while (targetElement);
});

document.body.addEventListener('blur', function(e) {
let targetElement = e.target;
if (targetElement.classList && targetElement.classList.contains('payment-name')) {
const paymentId = targetElement.closest('.payments-payment').dataset.paymentid;
const newName = targetElement.textContent;
renamePayment(paymentId, newName);
}
}, true);

function renamePayment(paymentId, newName) {
const name = newName.trim();
const formData = new FormData();
formData.append('paymentId', paymentId);
formData.append('name', name);
fetch('endpoints/payments/rename.php', {
method: 'POST',
body: formData
}).then(response => {
if (!response.ok) {
throw new Error(translate('network_response_error'));
}
return response.json();
}).then(data => {
if (data.success) {
showSuccessMessage(`${newName} ${data.message}`);
} else {
showErrorMessage(data.message);
}
}).catch(error => {
showErrorMessage(translate('unknown_error'));
});
}

document.body.addEventListener('keypress', function(e) {
let targetElement = e.target;
if (targetElement.classList && targetElement.classList.contains('payment-name')) {
if (e.key === 'Enter') {
e.preventDefault();
targetElement.blur();
}
}
});

function handleFileSelect(event) {
const fileInput = event.target;
const iconPreview = document.querySelector('.icon-preview');
Expand Down
15 changes: 8 additions & 7 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,21 +492,18 @@
}

foreach ($payments as $payment) {
$paymentIconFolder = $payment['id'] <= 31 ? 'images/uploads/icons/' : 'images/uploads/logos/';
$paymentIconFolder = $payment['id'] <= ? 'images/uploads/icons/' : 'images/uploads/logos/';
$inUse = in_array($payment['id'], $paymentsInUse);
?>
<div class="payments-payment"
data-enabled="<?= $payment['enabled']; ?>"
data-in-use="<?= $inUse ? 'yes' : 'no' ?>"
data-paymentid="<?= $payment['id'] ?>"
title="<?= $inUse ? translate('cant_delete_payment_method_in_use', $i18n) : ($payment['enabled'] ? translate('disable', $i18n) : translate('enable', $i18n)) ?>"
onClick="togglePayment(<?= $payment['id'] ?>)">
title="<?= $inUse ? translate('cant_delete_payment_method_in_use', $i18n) : ($payment['enabled'] ? translate('disable', $i18n) : translate('enable', $i18n)) ?>">
<img src="<?= $paymentIconFolder.$payment['icon'] ?>" alt="Logo" />
<span class="payment-name">
<?= $payment['name'] ?>
</span>
<span class="payment-name" contenteditable="true" title="<?= translate("rename_payment_method", $i18n) ?>"><?= $payment['name'] ?></span>
<?php
if ($payment['id'] > 31 && !$inUse) {
if (!$inUse) {
?>
<div class="delete-payment-method" title="<?= translate('delete', $i18n) ?>" data-paymentid="<?= $payment['id'] ?>">x</div>
<?php
Expand All @@ -522,6 +519,10 @@
<i class="fa-solid fa-circle-info"></i>
<?= translate('payment_methods_info', $i18n) ?>
</p>
<p>
<i class="fa-solid fa-circle-info"></i>
<?= translate('rename_payment_methods_info', $i18n) ?>
</p>
</div>
<header>
<h2 class="second-header"><?= translate("add_custom_payment", $i18n) ?></h2>
Expand Down
1 change: 1 addition & 0 deletions styles/dark-theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ header .logo .logo-image {
.account-section,
.avatar-select,
.logo-search,
.icon-search,
.dropdown-content,
.sort-options,
.statistic,
Expand Down
6 changes: 5 additions & 1 deletion styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,10 @@ main > .contain {
object-fit: contain;
}

.payments-list .payments-payment > .payment-name {
cursor: text;
}

.payments-list .payments-payment .delete-payment-method {
padding: 5px;
font-weight: bold;
Expand Down Expand Up @@ -841,7 +845,7 @@ input[type="checkbox"] {
}

.icon-search {
width: 100px;
width: 112px;
height: 224px;
top: 50px;
right: 0px;
Expand Down

0 comments on commit fb3f689

Please sign in to comment.