183 lines
6.7 KiB
PHP
183 lines
6.7 KiB
PHP
<?php
|
|
//
|
|
// Basic processing to generate and send email reminders
|
|
//
|
|
require_once("/m/data1/app.hotlunches.net/include/queuemail.php");
|
|
|
|
exit();
|
|
|
|
|
|
// Set current mode if required
|
|
if (!isset($mode)) $mode = "";
|
|
|
|
// Set output display mode
|
|
if (!isset($displayMode)) $displayMode = "screen";
|
|
|
|
|
|
// Handle filtering
|
|
$dow = date("D",time());
|
|
$schoolWhere = "";
|
|
if (!isset($schoolWhere)) $schoolWhere = "AND volunteer_reminder_day = '$dow' ";
|
|
if (!isset($userWhere)) $userWhere = "";
|
|
|
|
|
|
|
|
$today = date("Y.m.d",time());
|
|
|
|
if ($displayMode == "cron") $fp = fopen("/m/data1/app.hotlunches.net/logs/email/email.volunteers.$today.log","w");
|
|
else echo(getLanguageText('TEXT-WEEKLY-VOLUNTEER-REMINDER') . "\n");
|
|
|
|
$stime = time();
|
|
$start = date("Y-m-d H:i:s",$stime);
|
|
$week_start_date = date("Y-m-d",$stime);
|
|
$week_end_date = date("Y-m-d",$stime + 60 * 60 * 24 * 7);
|
|
|
|
$emailsSent = 0;
|
|
|
|
|
|
// Get set of schools that are sending on this day of the week
|
|
// $schools = db_query("SELECT * FROM schools WHERE record_num IN (1) ORDER BY school_name");
|
|
$schools = db_query("SELECT * FROM schools WHERE status = 'Active' $schoolWhere ORDER BY school_name");
|
|
|
|
while ($school = db_fetch_array($schools)) {
|
|
$school_name = $school['school_name'];
|
|
$school_num = $school['record_num'];
|
|
$subject = $school['volunteer_reminder_title'];
|
|
$default_language = $school['default_language'];
|
|
$date_format = $school['date_format'];
|
|
|
|
$week_end_date = date("Y-m-d",time() + 60 * 60 * 24 * 7);
|
|
|
|
if ($displayMode == "screen") echo("Processing: $school_name<br>\n");
|
|
if ($displayMode == "cron") echo("Processing: $school_name\n");
|
|
if ($displayMode == "cron") fwrite($fp,"Processing: $school_name\n");
|
|
|
|
$school_count = 0;
|
|
|
|
|
|
// Get set of linked sites
|
|
$query = "SELECT * FROM schools " .
|
|
"WHERE linked_site = $school_num " .
|
|
"AND status = 'Linked' " .
|
|
"AND email_reminder_day != 'None' " .
|
|
"ORDER BY link_sequence,link_title";
|
|
$linked_sites = db_query($query);
|
|
$linked_nums = $school_num;
|
|
if (db_num_rows($linked_sites)) {
|
|
while ($row = db_fetch_array($linked_sites)) {
|
|
$linked_nums .= "," . $row['record_num'];
|
|
}
|
|
}
|
|
|
|
// Get set of current schedules for this school
|
|
$query = "SELECT DISTINCT schedule_num,hlmd.record_num,hlmd.menu_date FROM hotlunch_menu_dates AS hlmd,hotlunch_schedules AS hls " .
|
|
"WHERE hlmd.school_num IN ($linked_nums) " .
|
|
"AND hlmd.schedule_num = hls.record_num " .
|
|
"AND hls.status IN ('Active','Completed','Closed') " .
|
|
"AND hls.schedule_type = 'Hot Lunch' " .
|
|
"AND menu_date BETWEEN '$week_start_date' AND '$week_end_date' " .
|
|
"ORDER BY sequence,menu_date";
|
|
$schedule_results = db_query($query);
|
|
$currSchedules = "";
|
|
|
|
// If there are no menu dates then continue
|
|
if (db_num_rows($schedule_results) == 0) {
|
|
if ($displayMode == "screen") echo(" No Schedules or Menus<br>\n<br>\n");
|
|
if ($displayMode == "cron") echo(" No Schedules or Menus\n\n");
|
|
if ($displayMode == "cron") fwrite($fp," No Schedules or Menus\n\n");
|
|
continue;
|
|
}
|
|
|
|
|
|
$menuNums = array();
|
|
while ($row = db_fetch_assoc($schedule_results)) {
|
|
$menuNums[] = $row['record_num'];
|
|
}
|
|
|
|
$menu_numbers = implode(",",$menuNums);
|
|
|
|
$query = "SELECT hlv.school_num,hlv.schedule_num,hlv.menu_num,hlv.user_num,hlmd.menu_title,hlmd.sequence,hlmd.menu_date,u.first_name,u.last_name,u.email,u.home_phone,u.record_num AS user_num " .
|
|
"FROM hotlunch_volunteers AS hlv, hotlunch_menu_dates AS hlmd, users AS u " .
|
|
"WHERE hlmd.school_num IN ($linked_nums) " .
|
|
"AND hlmd.record_num IN ($menu_numbers) " .
|
|
"AND hlv.school_num IN ($linked_nums) " .
|
|
"AND hlmd.record_num = hlv.menu_num " .
|
|
"AND u.school_num = $school_num " .
|
|
"AND hlv.user_num = u.record_num " .
|
|
"AND hlv.deleted != 'Y' " .
|
|
"UNION " .
|
|
"SELECT 0,0,0,0,'','2200-12-31','zzzzzzzz','zzzzzzzz','','',1 FROM normalize_chars " .
|
|
"ORDER BY 8,7,5,6";
|
|
$volunteers = db_query($query);
|
|
|
|
if (db_num_rows($volunteers) == 1) {
|
|
if ($displayMode == "screen") echo(" No Volunteers Found<br>\n<br>\n");
|
|
if ($displayMode == "cron") echo(" No Volunteers Found\n\n");
|
|
if ($displayMode == "cron") fwrite($fp," No Volunteers Found\n\n");
|
|
continue;
|
|
}
|
|
|
|
// Setup email
|
|
$reminder_msg = str_replace("\r","",trim(db_getVal("SELECT text_data FROM text_data WHERE school_num = $school_num AND text_id = 'Volunteer Reminder'")));
|
|
|
|
// Setup email headers
|
|
$email_from = setReplyAddress($school['reply_email']);
|
|
$from_name = $school['reply_description'];
|
|
|
|
if ($displayMode == "cron") fwrite($fp,"Email From: $email_from\n");
|
|
if ($displayMode == "cron") fwrite($fp,"From Name: $from_name\n");
|
|
|
|
|
|
$prev_user = 0;
|
|
$emailList = "";
|
|
$emailErrors = "";
|
|
while ($user = db_fetch_array($volunteers)) {
|
|
extract($user);
|
|
|
|
// If the user is different
|
|
if ($user_num != $prev_user) {
|
|
if ($prev_user > 0) {
|
|
// Send the reminder to the user
|
|
if ($mode != "log-only") {
|
|
if (!QueueEmail($email,$email_from,$subject,$email_message,$from_name,$to_name)) {
|
|
$Msg = "ERROR Sending\n";
|
|
$emailErrors .= "$to_name $home_phone $email\n";
|
|
}
|
|
else {
|
|
$emailsSent++;
|
|
$emailList .= "$to_name $home_phone $email\n";
|
|
}
|
|
}
|
|
|
|
if ($displayMode == "cron") fwrite($fp,"$Msg\nEmail: $email\nSubject: $subject\n$email_message\n----------------------------------------------------\n");
|
|
else echo(nl2br("$Msg\nEmail: $email\nSubject: $subject\n$email_message\n----------------------------------------------------\n"));
|
|
}
|
|
|
|
$prev_user = $user_num;
|
|
$to_name = "$first_name $last_name";
|
|
$email_message = $reminder_msg . "\n\n$to_name:\n";
|
|
}
|
|
|
|
$email_message .= " -> " . date($date_format,strtotime($menu_date)) . " - " . $menu_title . "\n";
|
|
}
|
|
|
|
if ($displayMode == "cron") fwrite($fp,"\n\n");
|
|
else echo("\n\n");
|
|
}
|
|
|
|
$etime = time();
|
|
$end = date("Y-m-d H:i:s",$etime);
|
|
|
|
if ($displayMode == "cron") fwrite($fp,"Start Time: $start\nEnd Time: $end\n\n");
|
|
echo("Start Time: $start\nEnd Time: $end\n\n");
|
|
|
|
$duration = $etime -$stime;
|
|
$hours = intval($duration / (60 * 60));
|
|
$minutes = intval(($duration % (60 * 60)) / 60);
|
|
$seconds = $duration % 60;
|
|
|
|
if ($displayMode == "cron") fwrite($fp,sprintf("Duration: %d:%02d:%02d - Sent: %d\n\n",$hours,$minutes,$seconds,$emailsSent));
|
|
printf("Duration: %d:%02d:%02d - Sent: %d\n\n",$hours,$minutes,$seconds,$emailsSent);
|
|
|
|
?>
|