From 19cf14c1aa4be8aebeed5d771221d494c6c45e66 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Thu, 17 Oct 2024 11:55:03 -0500 Subject: [PATCH] Add automatic donation drives --- config/phpstan/phpstan.neon | 6 +- lib/Constants.php | 29 ++- lib/DonationDrive.php | 95 +++++++ templates/DonationAlert.php | 19 +- templates/DonationCounter.php | 2 +- templates/DonationProgress.php | 82 ++---- www/collections/get.php | 6 +- www/css/core.css | 3 +- www/ebooks/ebook.php | 440 +++++++++++++++++---------------- www/ebooks/index.php | 10 +- 10 files changed, 396 insertions(+), 296 deletions(-) create mode 100644 lib/DonationDrive.php diff --git a/config/phpstan/phpstan.neon b/config/phpstan/phpstan.neon index b4d66a71..1b711a5d 100644 --- a/config/phpstan/phpstan.neon +++ b/config/phpstan/phpstan.neon @@ -15,10 +15,8 @@ parameters: - %rootDir%/../../../scripts dynamicConstantNames: - SITE_STATUS - - DONATION_HOLIDAY_ALERT_ON - - DONATION_ALERT_ON - - DONATION_DRIVE_ON - - DONATION_DRIVE_COUNTER_ON + - DONATION_DRIVE_ENABLED + - DONATION_DRIVE_COUNTER_ENABLED earlyTerminatingMethodCalls: Template: - Emit403 diff --git a/lib/Constants.php b/lib/Constants.php index 494de623..7d112cb0 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -74,15 +74,26 @@ const ARTWORK_UPLOADS_LOG_FILE_PATH = '/var/log/local/artwork-uploads.log'; // M define('PD_YEAR', intval((new DateTimeImmutable('now', new DateTimeZone('America/Juneau')))->format('Y')) - 96); // Latest continental US time zone. define('PD_STRING', 'January 1, ' . (PD_YEAR + 1)); -define('DONATION_HOLIDAY_ALERT_ON', NOW > new DateTimeImmutable('November 15, ' . NOW->format('Y')) || NOW < new DateTimeImmutable('January 7, ' . NOW->add(new DateInterval('P1Y'))->format('Y'))); -define('DONATION_ALERT_ON', DONATION_HOLIDAY_ALERT_ON || rand(1, 4) == 2); +// Controls the progress bar donation dialog. +const DONATION_DRIVES_ENABLED = true; // **`TRUE`** to enable automatic donation drives; **`FALSE`** to disable all donation drives. +const DONATION_DRIVE_DATES = [ + new DonationDrive( + 'Spring drive', + new DateTimeImmutable('Second Monday of May'), + new DateTimeImmutable('Second Monday of May +2 weeks'), + 50, + 20 + ), + new DonationDrive( + 'Holiday drive', + NOW < new DateTimeImmutable('November 15') ? new DateTimeImmutable('November 15') : new DateTimeImmutable('November 15 -1 year'), + NOW < new DateTimeImmutable('January 7') ? new DateTimeImmutable('January 7') : new DateTimeImmutable('January 7 +1 year'), + 50, + 20 + ) + ]; -// Controls the progress bar donation dialog -const DONATION_DRIVE_ON = true; -const DONATION_DRIVE_START = new DateTimeImmutable('May 20, 2024 00:00:00 America/New_York'); -const DONATION_DRIVE_END = new DateTimeImmutable('June 3, 2024 23:59:00 America/New_York'); - -// Controls the countdown donation dialog -const DONATION_DRIVE_COUNTER_ON = false; +// Controls the countdown donation dialog, basically unused right now. +const DONATION_DRIVE_COUNTER_ENABLED = false; const DONATION_DRIVE_COUNTER_START = new DateTimeImmutable('May 2, 2022 00:00:00 America/New_York'); const DONATION_DRIVE_COUNTER_END = new DateTimeImmutable('May 8, 2022 23:59:00 America/New_York'); diff --git a/lib/DonationDrive.php b/lib/DonationDrive.php new file mode 100644 index 00000000..0840a6b0 --- /dev/null +++ b/lib/DonationDrive.php @@ -0,0 +1,95 @@ + $donationDrive->Start && NOW < $donationDrive->End){ + return $donationDrive; + } + } + + return null; + } + + protected function GetDonationCount(): int{ + if(!isset($this->_DonationCount)){ + $this->_DonationCount = Db::QueryInt(' + SELECT sum(cnt) + from + ( + ( + # Anonymous patrons, i.e. from AOGF + select count(*) cnt from Payments + where + UserId is null + and + ( + (IsRecurring = true and Amount >= 10 and Created >= ?) + or + (IsRecurring = false and Amount >= 100 and Created >= ?) + ) + ) + union all + ( + # All non-anonymous patrons + select count(*) as cnt from Patrons + where Created >= ? + ) + ) x + ', [$this->Start, $this->Start, $this->Start]); + } + return $this->_DonationCount; + } + + protected function GetTargetDonationCount(): int{ + if(!isset($this->_TargetDonationCount)){ + $this->_TargetDonationCount = $this->BaseTargetDonationCount; + + if($this->DonationCount > $this->BaseTargetDonationCount){ + $this->_TargetDonationCount = $this->_TargetDonationCount + $this->StretchTargetDonationCount; + } + } + + return $this->_TargetDonationCount; + } + + protected function GetStretchDonationCount(): int{ + if(!isset($this->_StretchDonationCount)){ + $this->_StretchDonationCount = $this->DonationCount - $this->BaseTargetDonationCount; + if($this->_StretchDonationCount < 0){ + $this->_StretchDonationCount = 0; + } + } + + return $this->_StretchDonationCount; + } + + protected function GetIsStretchEnabled(): bool{ + if(!isset($this->_IsStretchEnabled)){ + $this->_IsStretchEnabled = false; + + if($this->StretchTargetDonationCount > 0 && $this->DonationCount >= $this->BaseTargetDonationCount){ + $this->_IsStretchEnabled = true; + } + } + + return $this->_IsStretchEnabled; + } +} diff --git a/templates/DonationAlert.php b/templates/DonationAlert.php index 069f2598..8f202920 100644 --- a/templates/DonationAlert.php +++ b/templates/DonationAlert.php @@ -1,5 +1,20 @@ - as an undismissable popup. Serve a
to Kindle instead. See https://github.com/standardebooks/web/issues/204 +` as an undismissable popup. Serve a `
` to Kindle instead. + // See . $element = 'aside'; if(stripos($_SERVER['HTTP_USER_AGENT'] ?? '', 'kindle') !== false){ diff --git a/templates/DonationCounter.php b/templates/DonationCounter.php index 67d74f17..c74863af 100644 --- a/templates/DonationCounter.php +++ b/templates/DonationCounter.php @@ -1,6 +1,6 @@ DONATION_DRIVE_COUNTER_END){ +if(!DONATION_DRIVE_COUNTER_ENABLED || ($autoHide ?? $_COOKIE['hide-donation-alert'] ?? false) || NOW > DONATION_DRIVE_COUNTER_END){ return; } diff --git a/templates/DonationProgress.php b/templates/DonationProgress.php index 1366a621..ce32c27a 100644 --- a/templates/DonationProgress.php +++ b/templates/DonationProgress.php @@ -1,54 +1,23 @@ NOW // If the drive hasn't started yet - || - NOW > DONATION_DRIVE_END // If the drive has ended + $donationDrive === null // There is no donation drive running right now. ){ return; } $autoHide = $autoHide ?? true; $showDonateButton = $showDonateButton ?? true; -$totalCurrent = Db::QueryInt(' - SELECT sum(cnt) - from - ( - ( - # Anonymous patrons, i.e. from AOGF - select count(*) cnt from Payments - where - UserId is null - and - ( - (IsRecurring = true and Amount >= 10 and Created >= ?) - or - (IsRecurring = false and Amount >= 100 and Created >= ?) - ) - ) - union all - ( - # All non-anonymous patrons - select count(*) as cnt from Patrons - where Created >= ? - ) - ) x - ', [DONATION_DRIVE_START, DONATION_DRIVE_START, DONATION_DRIVE_START]); -$totalTarget = $baseTarget; -$deadline = DONATION_DRIVE_END->format('F j'); -$timeLeft = NOW->diff(DONATION_DRIVE_END); +$deadline = $donationDrive->End->format('F j'); +$timeLeft = NOW->diff($donationDrive->End); $timeString = ''; if($timeLeft->days < 1 && $timeLeft->h < 20){ $timeString = 'Just hours'; @@ -71,13 +40,6 @@ else{ $timeString = 'Only ' . $timeString; } } - -$stretchOn = false; -if($stretchTarget > 0 && $totalCurrent >= $baseTarget){ - $stretchOn = true; - $stretchCurrent = $totalCurrent - $baseTarget; - $totalTarget = $baseTarget + $stretchTarget; -} ?>