Skip to content

Commit

Permalink
feat: add email for notifications to household members
Browse files Browse the repository at this point in the history
  • Loading branch information
ellite committed Feb 22, 2024
1 parent f8defb6 commit 06f0e26
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 40 deletions.
84 changes: 50 additions & 34 deletions endpoints/cronjobs/sendnotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use PHPMailer\PHPMailer\Exception;

require_once 'conf.php';
require_once $webPath . 'includes/connect_endpoint_crontabs.php';
require_once $webPath . 'includes/connect_endpoint.php';

$query = "SELECT * FROM notifications WHERE id = 1";
$result = $db->query($query);
Expand All @@ -30,58 +30,74 @@
$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');
while ($rowSubscription = $resultSubscriptions->fetchArray(SQLITE3_ASSOC)) {
$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.";
Expand Down
5 changes: 4 additions & 1 deletion endpoints/household/household.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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 = "v1.6.0";
$version = "v1.7.0";
?>
11 changes: 11 additions & 0 deletions migrations/000009.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
// This migration adds an "email" column to the members table.
// It allows the user to disable payment methods without deleting them.

/** @noinspection PhpUndefinedVariableInspection */
$columnQuery = $db->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 ""');
}
10 changes: 6 additions & 4 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
7 changes: 7 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@
?>
<div class="form-group-inline" data-memberid="<?= $member['id'] ?>">
<input type="text" name="member" value="<?= $member['name'] ?>" placeholder="Member">
<?php
if ($member['id'] !== 1) {
?>
<input type="text" name="email" value="<?= $member['email'] ?? "" ?>" placeholder="<?= translate("email", $i18n) ?>">
<?php
}
?>
<button class="image-button medium" onClick="editMember(<?= $member['id'] ?>)" name="save">
<img src="images/siteicons/save.png" title="<?= translate('save_member', $i18n) ?>">
</button>
Expand Down

0 comments on commit 06f0e26

Please sign in to comment.