handle reminders before card expiration and account deletion

This commit is contained in:
asus
2024-04-11 11:34:27 +02:00
parent 6ecffc25f7
commit 6a7de4dadd
5 changed files with 386 additions and 111 deletions

View File

@@ -47,6 +47,7 @@ function handle_transfert_validation_CIPF($user_id) {
update_card_date_expiration_CIPF($user_id);
set_card_number_CIPF($user_id);
set_card_renew_CIPF($user_id);
reset_emails_reminders_choices_CIPF($user_id);
send_emails_CIPF('transfert_success', $user_id);
}
@@ -141,20 +142,45 @@ function handle_reminders_before_card_expire_CIPF($user_id) {
return;
}
foreach ($card_reminders as $duration) {
$date_reminder = $date_limit->modify($duration);
$date_diff = date_diff($date_limit, $date_reminder);
$date_diff = (int)$date_diff->format('%R%a');
$is_before_reminders = false;
$reminder_list = array();
foreach ($card_reminders as $reminder_key => $value) {
$duration = get_duration_CIPF($reminder_key, 'card_expiration');
/*
* if false, this reminder is not for 'card_expiration'
* else, add to 'reminder_list'
*
*/
if (false === $duration) {
continue;
}
$reminder_list[] = $reminder_key;
$date_now = date_create('now');
$current_date_diff = date_diff($date_limit, $date_now);
$current_date_diff = (int)$current_date_diff->format('%R%a');
$diff = compare_diff_with_today_CIPF($date_limit, $duration);
if ($diff > 0) {
$is_before_reminders = true;
continue;
}
$is_before_reminders = false;
$diff = abs($current_date_diff - $date_diff);
if (is_email_reminder_choice_CIPF($reminder_key, $user_id)) {
continue;
}
set_email_reminder_choice_CIPF($reminder_key, $user_id);
send_emails_CIPF('card_will_expire', $user_id);
return; // don't send multiple emails
}
if ($diff === 0) {
// send_emails_CIPF('card_will_expire', $user_id);
// return; // don't send multiple emails
/*
* if didnt return, it either means that :
* - today is before any reminder
* - today is after all reminders
* use 'is_before_reminders' to know
*
*/
if ($is_before_reminders) {
foreach ($reminder_list as $reminder) {
unset_email_reminder_choice_CIPF($reminder, $user_id);
}
}
}
@@ -171,7 +197,6 @@ function handle_reminders_before_card_expire_CIPF($user_id) {
*/
function handle_reminders_before_account_deleted_CIPF($user_id) {
Plgntls::debug_infos();
$account_reminders = Cipf::DURATION_REMINDERS_BEFORE_ACCOUNT_DELETE;
$duration_deletion = Cipf::DURATION_ACCOUNT_DELETE_AFTER_EXPIRE;
if (!isset_acf_card_expiration_CIPF($user_id)) {
@@ -186,23 +211,105 @@ function handle_reminders_before_account_deleted_CIPF($user_id) {
}
$date_deletion = $date_limit->modify($duration_deletion);
foreach ($account_reminders as $reminder) {
$date_reminder = $date_deletion->modify($reminder);
$date_diff = date_diff($date_deletion, $date_reminder);
$date_diff = (int)$date_diff->format('%R%a');
$date_now = date_create('now');
$current_date_diff = date_diff($date_deletion, $date_now);
$current_date_diff = (int)$current_date_diff->format('%R%a');
$is_before_reminders = false;
$reminder_list = array();
/*
* get the acf reminders,
* - continue the ones not for account deletion
* - continue if date is not yet soon enough
* - continue if email was already sent
*
*/
$reminders_choices = get_emails_reminders_choices_CIPF($user_id);
foreach ($reminders_choices as $reminder_key => $value) {
$duration = get_duration_CIPF($reminder_key, 'account_deletion');
/*
* if false, this reminder is not for 'card_expiration'
* else, add to 'reminder_list'
*
*/
if (false === $duration) {
continue;
}
$reminder_list[] = $reminder_key;
// i add +3 in case there is a difference of maximum 3 days between a 31 days month and a 28 days february
if (abs($current_date_diff) <= (abs($date_diff) + 3)) {
// send_emails_CIPF('account_will_be_deleted', $user_id);
// return; // don't send multiple emails
$diff = compare_diff_with_today_CIPF($date_deletion, $duration);
if ($diff > 0) {
$is_before_reminders = true;
continue;
}
$is_before_reminders = false;
if (is_email_reminder_choice_CIPF($reminder_key, $user_id)) {
continue;
}
/*
unset_email_reminder_choice_CIPF($reminder_key, $user_id);
*/
set_email_reminder_choice_CIPF($reminder_key, $user_id);
send_emails_CIPF('account_will_be_deleted', $user_id);
return; // don't send multiple emails
}
/*
* if didnt return, it either means that :
* - today is before any reminder
* - today is after all reminders
* use 'is_before_reminders' to know
*
*/
if ($is_before_reminders) {
foreach ($reminder_list as $reminder) {
unset_email_reminder_choice_CIPF($reminder, $user_id);
}
}
}
/*
* extract the duration
* returns false if not for the right type
*
*/
function get_duration_CIPF($reminder, $type) {
Plgntls::debug_infos();
$explode_reminder = explode('/', $reminder);
$explode_reminder = array_map('trim', $explode_reminder);
if ($explode_reminder[0] !== $type) {
return false;
}
$duration = $explode_reminder[1];
return $duration;
}
/*
* find the diffs :
* - the intended diff between the deletion date and the reminder
* - the diff between the deletion date and today
* and return the difference
*
*/
function compare_diff_with_today_CIPF($date, $duration) {
Plgntls::debug_infos();
$date_reminder = $date->modify($duration);
$date_diff = date_diff($date, $date_reminder);
$date_diff = (int)$date_diff->format('%R%a');
$date_now = date_create('now');
$current_date_diff = date_diff($date, $date_now);
$current_date_diff = (int)$current_date_diff->format('%R%a');
return ($date_diff - $current_date_diff);
}
?>