Browse Source

Calendar cron job now working

merge-requests/1/head
Mike Koch 6 years ago
parent
commit
8756200fee
  1. 38
      cron/calendar_reminders.php
  2. 17
      inc/email_functions.inc.php
  3. 7
      language/en/emails/calendar_reminder.txt
  4. 7
      language/en/emails/html/calendar_reminder.txt

38
cron/calendar_reminders.php

@ -17,10 +17,6 @@ if (hesk_check_maintenance(false)) {
die($message);
}
// Get other required includes
require(HESK_PATH . 'inc/email_functions.inc.php');
hesk_load_internal_api_database_functions();
hesk_dbConnect();
@ -45,7 +41,7 @@ $case_statement = "CASE
$sql = "SELECT `reminder`.`id` AS `reminder_id`, `reminder`.`user_id` AS `user_id`, `reminder`.`event_id` AS `event_id`,
`event`.`name` AS `event_name`, `event`.`location` AS `event_location`, `event`.`comments` AS `event_comments`,
`category`.`name` AS `event_category`, `event`.`start` AS `event_start`, `event`.`end` AS `event_end`,
`event`.`all_day` AS `event_all_day`, `user`.`language` AS `user_language`, `user`.`email` AS `user_email`
`event`.`all_day` AS `event_all_day`, `user`.`language` AS `user_language`, `user`.`email` AS `user_email`,
" . $case_statement . " AS `reminder_date`
FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "calendar_event_reminder` AS `reminder`
INNER JOIN `" . hesk_dbEscape($hesk_settings['db_pfix']) . "calendar_event` AS `event`
@ -58,15 +54,39 @@ $sql = "SELECT `reminder`.`id` AS `reminder_id`, `reminder`.`user_id` AS `user_i
AND `email_sent` = '0'";
$rs = hesk_dbQuery($sql);
$modsForHesk_settings = NULL;
$reminders_to_flag = [];
if (hesk_dbNumRows($rs) > 0) {
require(HESK_PATH . 'inc/email_functions.inc.php');
$modsForHesk_settings = mfh_getSettings();
}
$i = 0;
$successful_emails = 0;
$failed_emails = 0;
while ($row = hesk_dbFetchAssoc($rs)) {
$i++;
$successful_emails++;
if (mfh_sendCalendarReminder($row, $modsForHesk_settings)) {
$reminders_to_flag[] = $row['reminder_id'];
if ($hesk_settings['debug_mode']) {
echo "Sent e-mail reminder for event: {$row['event_name']} to {$row['user_email']}\n";
}
} else {
$failed_emails++;
echo "Failed to send reminder email for event: {$row['event_name']} to {$row['user_email']}. This will be re-sent next time reminders are processed.\n";
}
}
echo "Sent e-mail reminder for event: {$row['event_name']}\n";
if (count($reminders_to_flag) > 0) {
foreach ($reminders_to_flag as $reminder_id) {
$sql = "UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "calendar_event_reminder` SET `email_sent` = '1' WHERE `id` = " . intval($reminder_id);
hesk_dbQuery($sql);
}
}
if ($hesk_settings['debug_mode']) {
echo "Finished Calendar Reminders. {$i} reminder e-mails sent. \n";
echo "Finished Calendar Reminders. {$successful_emails} reminder e-mails sent. {$failed_emails} emails failed to send.\n";
}

17
inc/email_functions.inc.php

@ -275,8 +275,17 @@ function mfh_sendCalendarReminder($reminder_data, $modsForHesk_settings) {
// Format email subject and message
$subject = str_replace('%%TITLE%%', $reminder_data['event_name'], $subject);
$message = hesk_getEmailMessage('calendar_reminder', NULL, $modsForHesk_settings, 1, 0, 1);
$message = mfh_processCalendarTemplate($message, $reminder_data);
$htmlMessage = hesk_getHtmlMessage('calendar_reminder', NULL, $modsForHesk_settings, 1, 0, 1);
$htmlMessage = mfh_processCalendarTemplate($htmlMessage, $reminder_data);
hesk_mail($reminder_data['user_email'], $subject, $message, $htmlMessage, $modsForHesk_settings);
return true;
}
function mfh_processCalendarTemplate($message, $reminder_data) {
global $hesk_settings;
if ($reminder_data['event_all_day'] == '1') {
$format = 'Y-m-d';
@ -284,21 +293,23 @@ function mfh_sendCalendarReminder($reminder_data, $modsForHesk_settings) {
$format = $hesk_settings['timeformat'];
}
$start_date = strtotime($event['event_start']);
$start_date = strtotime($reminder_data['event_start']);
$formatted_start_date = date($format, $start_date);
$formatted_end_date = '';
if ($reminder_data['event_start'] != $reminder_data['event_end']) {
$end_date = strtotime($event['event_end']);
$end_date = strtotime($reminder_data['event_end']);
$formatted_end_date = ' - ' . date($format, $end_date);
}
// Process replaced fields
$message = str_replace('%%TITLE%%', $reminder_data['event_name'], $message);
$message = str_replace('%%LOCATION%%', $reminder_data['event_location'], $message);
$message = str_replace('%%CATEGORY%%', $reminder_data['category_name'], $message);
$message = str_replace('%%CATEGORY%%', $reminder_data['event_category'], $message);
$message = str_replace('%%WHEN%%', $formatted_start_date . $formatted_end_date, $message);
$message = str_replace('%%COMMENTS%%', $reminder_data['event_comments'], $message);
return $message;
}

7
language/en/emails/calendar_reminder.txt

@ -0,0 +1,7 @@
An event is coming up soon!
----
Title: %%TITLE%%
Location: %%LOCATION%%
Category: %%CATEGORY%%
When: %%WHEN%%
Comments: %%COMMENTS%%

7
language/en/emails/html/calendar_reminder.txt

@ -0,0 +1,7 @@
<h3>An event is coming up soon!</h3>
<hr>
<b>Title:</b> %%TITLE%%
<br><b>Location:</b> %%LOCATION%%
<br><b>Category:</b> %%CATEGORY%%
<br><b>When:</b> %%WHEN%%
<br><b>Comments:</b> %%COMMENTS%%
Loading…
Cancel
Save