diff --git a/endpoints/cronjobs/sendnotifications.php b/endpoints/cronjobs/sendnotifications.php index 55003538f..b70479ce7 100644 --- a/endpoints/cronjobs/sendnotifications.php +++ b/endpoints/cronjobs/sendnotifications.php @@ -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); @@ -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/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..cd738d838 --- /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..1ba685e21 100644 --- a/settings.php +++ b/settings.php @@ -119,6 +119,13 @@ ?>
+ + " placeholder=""> +