From a442f92e2858db6437ff56c478fd99b038499cec Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Mon, 13 May 2024 12:32:07 -0500 Subject: [PATCH] Use exists() for some SQL statements; send thank-you email to patron donors who donate again --- scripts/ingest-fa-payments | 18 +++++++++-------- scripts/process-pending-payments | 33 +++++++++++++++++++++++++------- www/feeds/download.php | 9 +++++---- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/scripts/ingest-fa-payments b/scripts/ingest-fa-payments index c287b768..b3401495 100755 --- a/scripts/ingest-fa-payments +++ b/scripts/ingest-fa-payments @@ -52,14 +52,16 @@ $faItemsPerPage = 20; // How many items are on a full page of FA results? // If /tmp/last-fa-donation doesn't exist, get all transactions from today and create the file. function InsertTransaction($transactionId){ - $exists = Db::QueryInt('SELECT count(*) - from - ( select 1 - from Payments - where TransactionId = ? - union select 1 - from PendingPayments - where TransactionId = ? ) x', + $exists = Db::QueryInt('SELECT exists( + select * + from + ( select 1 + from Payments + where TransactionId = ? + union select 1 + from PendingPayments + where TransactionId = ? ) x + )', [$transactionId, $transactionId]); if(!$exists){ diff --git a/scripts/process-pending-payments b/scripts/process-pending-payments index fa2c4855..30736e1c 100755 --- a/scripts/process-pending-payments +++ b/scripts/process-pending-payments @@ -74,9 +74,11 @@ try{ $log->Write('Processing donation ' . $pendingPayment->TransactionId . ' ...'); if(Db::QueryInt(' - SELECT count(*) - from Payments - where TransactionId = ? + SELECT exists( + select * + from Payments + where TransactionId = ? + ) ', [$pendingPayment->TransactionId]) > 0){ $log->Write('Donation already exists in database.'); continue; @@ -201,10 +203,12 @@ try{ if($payment->User !== null){ // Are we already a patron? if(Db::QueryInt(' - SELECT count(*) - from Patrons - where UserId = ? - and Ended is null + SELECT exists( + select * + from Patrons + where UserId = ? + and Ended is null + ) ', [$payment->UserId]) == 0){ // Not a patron yet, add them to the Patrons Circle @@ -224,6 +228,21 @@ try{ $log->Write('Adding donor as patron ...'); $patron->Create(); } + elseif(!$payment->IsRecurring && !$payment->IsMatchingDonation){ + // User is already a patron, but they made another non-recurring, non-matching donation. + // Send a thank-you email. + + $log->Write('Sending thank you email to patron donor donating extra.'); + $em = new Email(); + $em->To = $payment->User->Email; + $em->ToName = $payment->User->Name; + $em->From = EDITOR_IN_CHIEF_EMAIL_ADDRESS; + $em->FromName = EDITOR_IN_CHIEF_NAME; + $em->Subject = 'Thank you for supporting Standard Ebooks!'; + $em->Body = Template::EmailDonationThankYou(); + $em->TextBody = Template::EmailDonationThankYouText(); + $em->Send(); + } } elseif(!$payment->IsRecurring && !$payment->IsMatchingDonation){ // Fully-anonymous, non-recurring donation eligible for the Patrons Circle. We can't notify them, but do notify the admins. diff --git a/www/feeds/download.php b/www/feeds/download.php index d0663bbb..948c7dc5 100644 --- a/www/feeds/download.php +++ b/www/feeds/download.php @@ -25,10 +25,11 @@ try{ $isUserAgentAllowed = false; if(isset($_SERVER['HTTP_USER_AGENT'])){ $isUserAgentAllowed = Db::QueryInt(' - SELECT count(*) - from FeedUserAgents - where instr(?, UserAgent) - limit 1 + SELECT exists( + select * + from FeedUserAgents + where instr(?, UserAgent) + ) ', [$_SERVER['HTTP_USER_AGENT']]); }