From 26363dd5f364b5494c526a9769626b03bba45273 Mon Sep 17 00:00:00 2001 From: Miguel Ribeiro Date: Sun, 25 Feb 2024 14:39:40 +0100 Subject: [PATCH] feat: add email for notifications to household members --- endpoints/cronjobs/sendnotifications.php | 82 ++++++++++++++---------- endpoints/household/household.php | 5 +- includes/i18n/de.php | 1 + includes/i18n/el.php | 1 + includes/i18n/en.php | 1 + includes/i18n/es.php | 1 + includes/i18n/fr.php | 1 + includes/i18n/jp.php | 1 + includes/i18n/pt.php | 1 + includes/i18n/tr.php | 1 + includes/i18n/zh_cn.php | 1 + includes/i18n/zh_tw.php | 1 + includes/version.php | 2 +- migrations/000009.php | 11 ++++ scripts/settings.js | 10 +-- settings.php | 12 ++++ 16 files changed, 93 insertions(+), 39 deletions(-) create mode 100644 migrations/000009.php diff --git a/endpoints/cronjobs/sendnotifications.php b/endpoints/cronjobs/sendnotifications.php index 55003538f..b1705a721 100644 --- a/endpoints/cronjobs/sendnotifications.php +++ b/endpoints/cronjobs/sendnotifications.php @@ -30,8 +30,10 @@ $currencies[$currencyId] = $row; } - $querySubscriptions = "SELECT * FROM subscriptions WHERE notify = 1 AND inactive = 0"; - $resultSubscriptions = $db->query($querySubscriptions); + $stmt = $db->prepare('SELECT * FROM subscriptions WHERE notify = :notify AND inactive = :inactive ORDER BY payer_user_id ASC'); + $stmt->bindValue(':notify', 1, SQLITE3_INTEGER); + $stmt->bindValue(':inactive', 0, SQLITE3_INTEGER); + $resultSubscriptions = $stmt->execute(); $notify = []; $i = 0; $currentDate = new DateTime('now'); @@ -39,49 +41,63 @@ $nextPaymentDate = new DateTime($rowSubscription['next_payment']); $difference = $currentDate->diff($nextPaymentDate)->days + 1; if ($difference === $days) { - $notify[$i]['name'] = $rowSubscription['name']; - $notify[$i]['price'] = $rowSubscription['price'] . $currencies[$rowSubscription['currency_id']]['symbol']; + $notify[$rowSubscription['payer_user_id']][$i]['name'] = $rowSubscription['name']; + $notify[$rowSubscription['payer_user_id']][$i]['price'] = $rowSubscription['price'] . $currencies[$rowSubscription['currency_id']]['symbol']; $i++; } } if (!empty($notify)) { + require $webPath . 'libs/PHPMailer/PHPMailer.php'; require $webPath . 'libs/PHPMailer/SMTP.php'; require $webPath . 'libs/PHPMailer/Exception.php'; - $dayText = $days == 1 ? "tomorrow" : "in " . $days . " days"; - $message = "The following subscriptions are up for renewal " . $dayText . ":\n"; - foreach ($notify as $subscription) { - $message .= $subscription['name'] . " for " . $subscription['price'] . "\n"; - } - - $mail = new PHPMailer(true); - $mail->CharSet="UTF-8"; - $mail->isSMTP(); + $stmt = $db->prepare('SELECT * FROM user WHERE id = :id'); + $stmt->bindValue(':id', 1, SQLITE3_INTEGER); + $result = $stmt->execute(); + $defaultUser = $result->fetchArray(SQLITE3_ASSOC); + $defaultEmail = $defaultUser['email']; + $defaultName = $defaultUser['username']; - $mail->Host = $smtpAddress; - $mail->SMTPAuth = true; - $mail->Username = $smtpUsername; - $mail->Password = $smtpPassword; - $mail->SMTPSecure = 'tls'; - $mail->Port = $smtpPort; + foreach ($notify as $userId => $perUser) { + $dayText = $days == 1 ? "tomorrow" : "in " . $days . " days"; + $message = "The following subscriptions are up for renewal " . $dayText . ":\n"; - $getUser = "SELECT * FROM user WHERE id = 1"; - $user = $db->querySingle($getUser, true); - $email = $user['email']; - $name = $user['username']; - - $mail->setFrom($fromEmail, 'Wallos App'); - $mail->addAddress($email, $name); - - $mail->Subject = 'Wallos Notification'; - $mail->Body = $message; + foreach ($perUser as $subscription) { + $message .= $subscription['name'] . " for " . $subscription['price'] . "\n"; + } + + $mail = new PHPMailer(true); + $mail->CharSet="UTF-8"; + $mail->isSMTP(); + + $mail->Host = $smtpAddress; + $mail->SMTPAuth = true; + $mail->Username = $smtpUsername; + $mail->Password = $smtpPassword; + $mail->SMTPSecure = 'tls'; + $mail->Port = $smtpPort; + + $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId'); + $stmt->bindValue(':userId', $userId, SQLITE3_INTEGER); + $result = $stmt->execute(); + $user = $result->fetchArray(SQLITE3_ASSOC); - if ($mail->send()) { - echo "Notifications sent"; - } else { - echo "Error sending notifications: " . $mail->ErrorInfo; + $email = !empty($user['email']) ? $user['email'] : $defaultEmail; + $name = !empty($user['name']) ? $user['name'] : $defaultName; + + $mail->setFrom($fromEmail, 'Wallos App'); + $mail->addAddress($email, $name); + + $mail->Subject = 'Wallos Notification'; + $mail->Body = $message; + + if ($mail->send()) { + echo "Notifications sent"; + } else { + echo "Error sending notifications: " . $mail->ErrorInfo; + } } } else { echo "Nothing to notify."; diff --git a/endpoints/household/household.php b/endpoints/household/household.php index 094700350..08759af1c 100644 --- a/endpoints/household/household.php +++ b/endpoints/household/household.php @@ -30,9 +30,12 @@ if (isset($_GET['memberId']) && $_GET['memberId'] != "" && isset($_GET['name']) && $_GET['name'] != "") { $memberId = $_GET['memberId']; $name = validate($_GET['name']); - $sql = "UPDATE household SET name = :name WHERE id = :memberId"; + $email = $_GET['email'] ? $_GET['email'] : ""; + $email = validate($email); + $sql = "UPDATE household SET name = :name, email = :email WHERE id = :memberId"; $stmt = $db->prepare($sql); $stmt->bindParam(':name', $name, SQLITE3_TEXT); + $stmt->bindParam(':email', $email, SQLITE3_TEXT); $stmt->bindParam(':memberId', $memberId, SQLITE3_INTEGER); $result = $stmt->execute(); diff --git a/includes/i18n/de.php b/includes/i18n/de.php index 419dbea8b..52e5dd9ec 100644 --- a/includes/i18n/de.php +++ b/includes/i18n/de.php @@ -96,6 +96,7 @@ "delete_member" => "Mitglied löschen", "cant_delete_member" => "Hauptmitglied kann nicht gelöscht werden", "cant_delete_member_in_use" => "Mitglied mit Abonnement kann nicht gelöscht werden", + 'household_info' => "Über das E-Mail-Feld können die Haushaltsmitglieder über auslaufende Abonnements benachrichtigt werden.", "notifications" => "Benachrichtigungen", "enable_email_notifications" => "E-Mail Benachrichtigung aktivieren", "notify_me" => "Benachrichtige mich", diff --git a/includes/i18n/el.php b/includes/i18n/el.php index cd75e425d..f47d4b062 100644 --- a/includes/i18n/el.php +++ b/includes/i18n/el.php @@ -96,6 +96,7 @@ "delete_member" => "Διαγραφή μέλους", "cant_delete_member" => "Δεν ειναι δυνατή η διαγραφή του βασικού μέλους", "cant_delete_member_in_use" => "Δεν ειναι δυνατή η διαγραφή μέλους που χρησιμοποιείται", + 'household_info' => "Το πεδίο ηλεκτρονικού ταχυδρομείου επιτρέπει στα μέλη του νοικοκυριού να ειδοποιούνται για συνδρομές που πρόκειται να λήξουν.", "notifications" => "Ειδοποιήσεις", "enable_email_notifications" => "Ενεργοποίηση ειδοποιήσεων με email", "notify_me" => "Ειδοποίησε με", diff --git a/includes/i18n/en.php b/includes/i18n/en.php index f3d6e2e13..f80783966 100644 --- a/includes/i18n/en.php +++ b/includes/i18n/en.php @@ -96,6 +96,7 @@ "delete_member" => "Delete Member", "cant_delete_member" => "Can't delete main member", "cant_delete_member_in_use" => "Can't delete member in use in subscription", + 'household_info' => "Email field allows for household members to be notified of subscriptions about to expire.", "notifications" => "Notifications", "enable_email_notifications" => "Enable email notifications", "notify_me" => "Notify me", diff --git a/includes/i18n/es.php b/includes/i18n/es.php index 62d287728..83c6f8d29 100644 --- a/includes/i18n/es.php +++ b/includes/i18n/es.php @@ -96,6 +96,7 @@ "delete_member" => "Eliminar Miembro", "cant_delete_member" => "No se puede eliminar el miembro principal", "cant_delete_member_in_use" => "No se puede eliminar el miembro en uso en la suscripción", + "household_info" => "El campo de correo electrónico permite notificar a los miembros del hogar las suscripciones que están a punto de caducar.", "notifications" => "Notificaciones", "enable_email_notifications" => "Habilitar notificaciones por correo electrónico", "notify_me" => "Notificarme", diff --git a/includes/i18n/fr.php b/includes/i18n/fr.php index 6f832d925..caa7fc481 100644 --- a/includes/i18n/fr.php +++ b/includes/i18n/fr.php @@ -96,6 +96,7 @@ "delete_member" => "Supprimer le membre", "cant_delete_member" => "Impossible de supprimer le membre principal", "cant_delete_member_in_use" => "Impossible de supprimer le membre utilisé dans l'abonnement", + "household_info" => "Le champ Courriel permet aux membres du ménage d'être informés des abonnements arrivant à expiration.", "notifications" => "Notifications", "enable_email_notifications" => "Activer les notifications par courriel", "notify_me" => "Me prevenir", diff --git a/includes/i18n/jp.php b/includes/i18n/jp.php index 70eb54ed2..d365fdde2 100644 --- a/includes/i18n/jp.php +++ b/includes/i18n/jp.php @@ -96,6 +96,7 @@ "delete_member" => "世帯員を削除", "cant_delete_member" => "世帯主は削除出ません", "cant_delete_member_in_use" => "定期購入を使用中の世帯員は削除できません", + "household_info" => "Eメールフィールドでは、世帯のメンバーに購読期限が近づいたことを通知することができます。", "notifications" => "通知", "enable_email_notifications" => "電子メール通知を有効にする", "notify_me" => "通知", diff --git a/includes/i18n/pt.php b/includes/i18n/pt.php index 896e1a226..3058b0cd1 100644 --- a/includes/i18n/pt.php +++ b/includes/i18n/pt.php @@ -96,6 +96,7 @@ "delete_member" => "Apagar Membro", "cant_delete_member" => "Não pode apagar o membro principal", "cant_delete_member_in_use" => "Não pode apagar membro em uso em subscrição", + "household_info" => "O campo E-mail permite que os membros do agregado sejam notificados das subscrições que estão prestes a expirar.", "notifications" => "Notificações", "enable_email_notifications" => "Activar notificações por email", "notify_me" => "Notificar-me", diff --git a/includes/i18n/tr.php b/includes/i18n/tr.php index 2fa03e150..de4c519d2 100644 --- a/includes/i18n/tr.php +++ b/includes/i18n/tr.php @@ -96,6 +96,7 @@ "delete_member" => "Üyeyi Sil", "cant_delete_member" => "Ana üyeyi silemezsiniz", "cant_delete_member_in_use" => "Abonelikte kullanılan üyeyi silemezsiniz", + "household_info" => "E-posta alanı, hane üyelerinin süresi dolmak üzere olan aboneliklerden haberdar edilmesini sağlar.", "notifications" => "Bildirimler", "enable_email_notifications" => "E-posta bildirimlerini etkinleştir", "notify_me" => "Beni bilgilendir", diff --git a/includes/i18n/zh_cn.php b/includes/i18n/zh_cn.php index 3cd309ee7..780743c51 100644 --- a/includes/i18n/zh_cn.php +++ b/includes/i18n/zh_cn.php @@ -103,6 +103,7 @@ "delete_member" => "删除成员", "cant_delete_member" => "不能删除主要成员", "cant_delete_member_in_use" => "不能删除拥有订阅的成员", + "household_info" => "电子邮件字段允许通知家庭成员订阅即将过期。", "notifications" => "通知", "enable_email_notifications" => "启用电子邮件通知", "notify_me" => "通知提前时间", diff --git a/includes/i18n/zh_tw.php b/includes/i18n/zh_tw.php index 845a8e6b2..81cce9795 100644 --- a/includes/i18n/zh_tw.php +++ b/includes/i18n/zh_tw.php @@ -103,6 +103,7 @@ "delete_member" => "刪除成員", "cant_delete_member" => "無法刪除主要成員", "cant_delete_member_in_use" => "無法刪除擁有訂閱的成員", + "household_info" => "電子郵件欄位允許家庭成員收到訂閱即將到期的通知。", "notifications" => "通知", "enable_email_notifications" => "啟用電子信箱通知", "notify_me" => "通知提前時間", diff --git a/includes/version.php b/includes/version.php index 3bf0b00fd..af14e124f 100644 --- a/includes/version.php +++ b/includes/version.php @@ -1,3 +1,3 @@ \ No newline at end of file diff --git a/migrations/000009.php b/migrations/000009.php new file mode 100644 index 000000000..0fd0215ac --- /dev/null +++ b/migrations/000009.php @@ -0,0 +1,11 @@ +query("SELECT * FROM pragma_table_info('household') where name='email'"); +$columnRequired = $columnQuery->fetchArray(SQLITE3_ASSOC) === false; + +if ($columnRequired) { + $db->exec('ALTER TABLE household ADD COLUMN email TEXT DEFAULT ""'); +} \ No newline at end of file diff --git a/scripts/settings.js b/scripts/settings.js index a0974e797..1708f51ea 100644 --- a/scripts/settings.js +++ b/scripts/settings.js @@ -113,12 +113,14 @@ function removeMember(memberId) { function editMember(memberId) { var saveButton = document.querySelector(`div[data-memberid="${memberId}"] button[name="save"]`); - var inputElement = document.querySelector(`div[data-memberid="${memberId}"] input[name="member"]`); + var memberNameElement = document.querySelector(`div[data-memberid="${memberId}"] input[name="member"]`); + var memberEmailElement = document.querySelector(`div[data-memberid="${memberId}"] input[name="email"]`); saveButton.classList.add("disabled"); saveButton.disabled = true; - if (inputElement) { - var memberName = encodeURIComponent(inputElement.value); - var url = `endpoints/household/household.php?action=edit&memberId=${memberId}&name=${memberName}`; + if (memberNameElement) { + var memberName = encodeURIComponent(memberNameElement.value); + var memberEmail = memberEmailElement ? encodeURIComponent(memberEmailElement.value) : ''; + var url = `endpoints/household/household.php?action=edit&memberId=${memberId}&name=${memberName}&email=${memberEmail}`; fetch(url) .then(response => { diff --git a/settings.php b/settings.php index 31000da74..44bea3a9e 100644 --- a/settings.php +++ b/settings.php @@ -119,6 +119,13 @@ ?>
+ + " placeholder=""> + @@ -142,6 +149,11 @@ } ?>
+
+

+

+

+