Bootswatch, Summernote, and Captcheck mods for Mods for HESK (mods-for-hesk.com). In use at support.netsyms.com.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

calendar_reminders.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/php -q
  2. <?php
  3. define('IN_SCRIPT',1);
  4. define('HESK_PATH', dirname(dirname(__FILE__)) . '/');
  5. $LOCATION = 'Calendar Reminders Cron Job';
  6. #echo HESK_PATH."\n";
  7. // Get required files and functions
  8. require(HESK_PATH . 'hesk_settings.inc.php');
  9. require(HESK_PATH . 'inc/common.inc.php');
  10. if (defined('HESK_DEMO')) {
  11. echo '>>>>>>> DEMO MODE IS ENABLED. CRON JOBS CANNOT BE EXECUTED WHILE IN DEMO MODE! <<<<<<<';
  12. die();
  13. }
  14. if (hesk_check_maintenance(false)) {
  15. // If Debug mode is ON show "Maintenance mode" message
  16. $message = $hesk_settings['debug_mode'] ? $hesklang['mm1'] : '';
  17. $message .= "\n";
  18. die($message);
  19. }
  20. hesk_load_cron_database_functions();
  21. hesk_dbConnect();
  22. $modsForHesk_settings = mfh_getSettings();
  23. $skip_events = $modsForHesk_settings['enable_calendar'] == 0;
  24. if ($hesk_settings['debug_mode']) {
  25. echo "Starting Calendar Reminders...\n";
  26. }
  27. // Get all reminders that have a reminder date that is now or earlier, and an email has not been sent for the event yet.
  28. /*
  29. * Reminder units:
  30. * 0 - minutes
  31. * 1 - hours
  32. * 2 - days
  33. * 3 - weeks
  34. */
  35. $old_timeformat_setting = $hesk_settings['timeformat'];
  36. $hesk_settings['timeformat'] = 'Y-m-d H:i:s';
  37. $current_date = hesk_date();
  38. $case_statement = "CASE
  39. WHEN `unit` = '0' THEN DATE_SUB(`event`.`start`, INTERVAL `reminder`.`amount` MINUTE)
  40. WHEN `unit` = '1' THEN DATE_SUB(`event`.`start`, INTERVAL `reminder`.`amount` HOUR)
  41. WHEN `unit` = '2' THEN DATE_SUB(`event`.`start`, INTERVAL `reminder`.`amount` DAY)
  42. WHEN `unit` = '3' THEN DATE_SUB(`event`.`start`, INTERVAL `reminder`.`amount` WEEK)
  43. END";
  44. $sql = "SELECT `reminder`.`id` AS `reminder_id`, `reminder`.`user_id` AS `user_id`, `reminder`.`event_id` AS `event_id`,
  45. `event`.`name` AS `event_name`, `event`.`location` AS `event_location`, `event`.`comments` AS `event_comments`,
  46. `category`.`name` AS `event_category`, `event`.`start` AS `event_start`, `event`.`end` AS `event_end`,
  47. `event`.`all_day` AS `event_all_day`, `user`.`language` AS `user_language`, `user`.`email` AS `user_email`,
  48. " . $case_statement . " AS `reminder_date`, 'EVENT' AS `type`
  49. FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "calendar_event_reminder` AS `reminder`
  50. INNER JOIN `" . hesk_dbEscape($hesk_settings['db_pfix']) . "calendar_event` AS `event`
  51. ON `reminder`.`event_id` = `event`.`id`
  52. INNER JOIN `" . hesk_dbEscape($hesk_settings['db_pfix']) . "categories` AS `category`
  53. ON `event`.`category` = `category`.`id`
  54. INNER JOIN `" . hesk_dbEscape($hesk_settings['db_pfix']) . "users` AS `user`
  55. ON `reminder`.`user_id` = `user`.`id`
  56. WHERE (" . $case_statement . ") <= '{$current_date}'
  57. AND `email_sent` = '0'";
  58. $rs = hesk_dbQuery($sql);
  59. $reminders_to_flag = array();
  60. $tickets_to_flag = array();
  61. $included_email_functions = false;
  62. if (hesk_dbNumRows($rs) > 0 && !$skip_events) {
  63. require(HESK_PATH . 'inc/email_functions.inc.php');
  64. $included_email_functions = true;
  65. }
  66. $successful_emails = 0;
  67. $failed_emails = 0;
  68. while ($row = hesk_dbFetchAssoc($rs)) {
  69. if ($skip_events) {
  70. return true;
  71. }
  72. if (mfh_sendCalendarReminder($row, $modsForHesk_settings)) {
  73. $reminders_to_flag[] = $row['reminder_id'];
  74. $successful_emails++;
  75. if ($hesk_settings['debug_mode']) {
  76. $debug_msg = "Sent e-mail reminder for event: {$row['event_name']} to {$row['user_email']}\n";
  77. echo $debug_msg;
  78. mfh_log_debug($LOCATION, $debug_msg, 'CRON');
  79. }
  80. } else {
  81. $failed_emails++;
  82. $warning_text = "Failed to send reminder e-mail for event: {$row['event_name']} to {$row['user_email']}. This will be re-sent next time reminders are processed.\n";
  83. mfh_log_warning($LOCATION, $warning_text, 'CRON');
  84. echo $warning_text;
  85. }
  86. }
  87. if (count($reminders_to_flag) > 0) {
  88. foreach ($reminders_to_flag as $reminder_id) {
  89. $sql = "UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "calendar_event_reminder` SET `email_sent` = '1' WHERE `id` = " . intval($reminder_id);
  90. hesk_dbQuery($sql);
  91. }
  92. }
  93. if ($hesk_settings['debug_mode']) {
  94. $debug_msg = "Finished Calendar Reminders. {$successful_emails} reminder e-mails sent. {$failed_emails} emails failed to send.\n";
  95. echo $debug_msg;
  96. mfh_log_debug($LOCATION, $debug_msg, 'CRON');
  97. }
  98. // Overdue tickets
  99. if ($hesk_settings['debug_mode']) {
  100. echo "Starting Overdue Tickets...\n";
  101. }
  102. $sql = "SELECT `ticket`.`id` AS `id`, `ticket`.`trackid` AS `trackid`, `ticket`.`name` AS `name`, `ticket`.`subject` AS `subject`,
  103. `ticket`.`message` AS `message`, `ticket`.`category` AS `category`, `ticket`.`priority` AS `priority`,
  104. `ticket`.`owner` AS `owner`, `ticket`.`status` AS `status`, `ticket`.`email` AS `email`, `ticket`.`dt` AS `dt`,
  105. `ticket`.`lastchange` AS `lastchange`, `ticket`.`due_date` AS `due_date`, `user`.`language` AS `user_language`, `user`.`email` AS `user_email`,
  106. `ticket`.`custom1` AS `custom1`, `ticket`.`custom2` AS `custom2`, `ticket`.`custom3` AS `custom3`, `ticket`.`custom4` AS `custom4`,
  107. `ticket`.`custom5` AS `custom5`, `ticket`.`custom6` AS `custom6`, `ticket`.`custom7` AS `custom7`, `ticket`.`custom8` AS `custom8`,
  108. `ticket`.`custom9` AS `custom9`, `ticket`.`custom10` AS `custom10`, `ticket`.`custom11` AS `custom11`, `ticket`.`custom12` AS `custom12`,
  109. `ticket`.`custom13` AS `custom13`, `ticket`.`custom14` AS `custom14`, `ticket`.`custom15` AS `custom15`, `ticket`.`custom16` AS `custom16`,
  110. `ticket`.`custom17` AS `custom17`, `ticket`.`custom18` AS `custom19`, `ticket`.`custom19` AS `custom19`, `ticket`.`custom20` AS `custom20`,
  111. `ticket`.`html` AS `html`
  112. FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "tickets` AS `ticket`
  113. INNER JOIN `" . hesk_dbEscape($hesk_settings['db_pfix']) . "statuses` AS `status`
  114. ON `ticket`.`status` = `status`.`ID`
  115. AND `status`.`IsClosed` = 0
  116. LEFT JOIN `" . hesk_dbEscape($hesk_settings['db_pfix']) . "users` AS `user`
  117. ON `ticket`.`owner` = `user`.`id`
  118. WHERE `due_date` IS NOT NULL
  119. AND `due_date` <= '{$current_date}'
  120. AND `overdue_email_sent` = '0'";
  121. $successful_emails = 0;
  122. $failed_emails = 0;
  123. $rs = hesk_dbQuery($sql);
  124. if (hesk_dbNumRows($rs) > 0 && !$included_email_functions) {
  125. require(HESK_PATH . 'inc/email_functions.inc.php');
  126. $included_email_functions = true;
  127. }
  128. $user_rs = hesk_dbQuery("SELECT `id`, `isadmin`, `categories`, `email`,
  129. CASE WHEN `heskprivileges` LIKE '%can_view_unassigned%' THEN 1 ELSE 0 END AS `can_view_unassigned`
  130. FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "users` WHERE `notify_overdue_unassigned` = '1'
  131. AND (`heskprivileges` LIKE '%can_view_tickets%' OR `isadmin` = '1')");
  132. $users = array();
  133. while ($row = hesk_dbFetchAssoc($user_rs)) {
  134. $users[] = $row;
  135. }
  136. $tickets_to_flag = array();
  137. while ($row = hesk_dbFetchAssoc($rs)) {
  138. if (mfh_sendOverdueTicketReminder($row, $users, $modsForHesk_settings)) {
  139. $tickets_to_flag[] = $row['id'];
  140. $successful_emails++;
  141. if ($hesk_settings['debug_mode']) {
  142. $debug_msg = "Sent overdue e-mail for ticket: {$row['trackid']} to user id: {$row['owner']}\n";
  143. mfh_log_debug($LOCATION, $debug_msg, 'CRON');
  144. echo $debug_msg;
  145. }
  146. } else {
  147. $failed_emails++;
  148. $warning_text = "Failed to send overdue reminder for ticket: {$row['trackid']} to user id: {$row['owner']}. This will be re-sent next time overdue tickets are processed.\n";\
  149. mfh_log_warning($LOCATION, $warning_text, 'CRON');
  150. echo $warning_text;
  151. }
  152. }
  153. if (count($tickets_to_flag) > 0) {
  154. foreach ($tickets_to_flag as $ticket_id) {
  155. $sql = "UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "tickets` SET `overdue_email_sent` = '1' WHERE `id` = " . intval($ticket_id);
  156. hesk_dbQuery($sql);
  157. }
  158. }
  159. if ($hesk_settings['debug_mode']) {
  160. $debug_msg = "Finished Overdue Tickets. {$successful_emails} e-mails sent. {$failed_emails} emails failed to send.\n";
  161. echo $debug_msg;
  162. mfh_log_debug($LOCATION, $debug_msg, 'CRON');
  163. }
  164. $hesk_settings['timeformat'] = $old_timeformat_setting;