Files
HotLunches/wallpapers.popup.php
2026-10-19 15:22:52 -07:00

542 lines
22 KiB
PHP

<?php
// Get parameters
unset($naction);
if (array_key_exists('naction',$_GET)) $naction = $_GET['naction'];
if (array_key_exists('naction',$_POST)) $naction = $_POST['naction'];
$centerMessages = true;
//
// Handle any inserts or updates
//
if (isset($naction)) {
switch ($naction) {
case "add":
// Set insert defaults
$time_block_date = $schedule_start_date;
$time_block_end_date = $schedule_end_date;
$appointment_duration = 0;
$break_interval = 0;
$break_frequency = 1;
// Check if there are time blocks defined for this schedule already
$results = db_query("SELECT * FROM schedule_times WHERE school_num = $school_num AND schedule_num = $schedule_num");
if (db_num_rows($results)) {
$row = db_fetch_array($results);
$appointment_duration = $row['appointment_duration'];
$break_interval = $row['break_interval'];
$break_frequency = $row['break_frequency'];
}
// Display the form
$next_naction = "insert";
$ButtonText = getLanguageText('TEXT-INSERT-RECORD');
include "schedule_time_blocks.popup.form.php";
return;
case "edit":
// Get the current record
$results = db_query("SELECT * FROM schedule_times WHERE school_num = $school_num AND schedule_num = $schedule_num AND record_num = $timeblock_num");
$row = db_fetch_array($results);
extract($row);
$stime = strtotime($start_time);
$etime = strtotime($end_time);
$time_block_date = date("Y-m-d",$stime);
$time_block_end_date = date("Y-m-d",$etime);
// Display the form
$next_naction = "update";
$ButtonText = getLanguageText('TEXT-UPDATE-RECORD');
include "schedule_time_blocks.popup.form.php";
return;
case "insert":
extract($_POST);
$entered_appointment_duration = $appointment_duration;
// Setup parameters for form
$ButtonText = getLanguageText('TEXT-INSERT-RECORD');
$next_naction = "insert";
// Set end loop date if a single block is being added
if ($timeblocks_added == 'single') {
$time_block_end_date = $time_block_date;
$selected_days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
}
// Get the dates if selected dates are being added
$selectedDates = array();
if ($timeblocks_added == 'calendar') {
$selected_days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
for ($i = 0; $i < count($selected_dates); $i++) {
if (strlen($selected_dates[$i])) $selectedDates[] = $selected_dates[$i];
}
// Ensure that some dates were selected
if (count($selectedDates) == 0) {
$error_message = getLanguageText('ERRORMSG-NO-DATES-SELECTED');
include "schedule_time_blocks.popup.form.php";
return;
}
$time_block_date = $selectedDates[0];
$time_block_end_date = $selectedDates[count($selectedDates) - 1];
}
// Check that the end date is >= start date
if ($time_block_end_date < $time_block_date) {
$error_message = getLanguageText('ERRORMSG-DATE-RANGE-ERROR');
include "schedule_time_blocks.popup.form.php";
return;
}
// Check that there are some days selected
if (count($selected_days) == 0) {
$error_message = getLanguageText('ERRORMSG-NO-DAYS-SELECTED');
include "schedule_time_blocks.popup.form.php";
return;
}
// Format the time values to perform the basic checks
$start_time = "$time_block_date $start_hour:$start_minute";
$end_time = "$time_block_date $end_hour:$end_minute";
$ostart_time = $start_time;
$oend_time = $end_time;
$stime = strtotime($start_time);
$etime = strtotime($end_time);
// Validate the start and end times
if ($etime <= $stime) {
$error_message = getLanguageText('ERRORMSG-END-TIME-BEFORE-START-TIME');
include "schedule_time_blocks.popup.form.php";
return;
}
// Ensure duration is > 0
$appointment_duration = intval($appointment_duration);
if ($appointment_duration == 0) {
$appointment_duration = intval(($etime - $stime) / 60);
}
// Setup loop to check for overlapping time blocks
$sdate = strtotime($time_block_date);
$edate = strtotime($time_block_end_date);
$date = $sdate;
// Check for overlapping time blocks
while ($date <= $edate) {
// Check the day of the week
$dow = date("l",$date);
if (!in_array($dow,$selected_days)) {
$date += 24 * 60 * 60;
continue;
}
// Check for selected dates
if ($timeblocks_added == 'calendar') {
$cdate = date("Y-m-d",$date);
if (!in_array($cdate,$selectedDates)) {
$date += 24 * 60 * 60;
continue;
}
}
// Format the datetime for each loop date
$time_block_date = date("Y-m-d",$date + 2 * 60 * 60);
$start_time = "$time_block_date $start_hour:$start_minute";
$end_time = "$time_block_date $end_hour:$end_minute";
// Check for overlapping time blocks
$query = "SELECT * FROM schedule_times " .
"WHERE school_num = $school_num " .
"AND schedule_num = $schedule_num " .
"AND (('$start_time' BETWEEN start_time AND end_time) " .
"OR ('$end_time' BETWEEN start_time AND end_time))";
$results = db_query($query);
$num = db_num_rows($results);
if (db_num_rows($results)) {
$error_message = getLanguageText('ERRORMSG-OVERLAPPING-TIME-BLOCKS') . ' - ' . date($school_record['date_format'],$date);
include "schedule_time_blocks.popup.form.php";
return;
}
$date += 24 * 60 * 60;
}
// If this is a confirmation check if the times have been changed
if (isset($confirm_block_length) && $confirm_block_length == 'Y') {
if ($stime != $confirm_stime || $etime != $confirm_etime) unset($confirm_block_length);
}
if (isset($confirm_start_time) && $confirm_start_time == 'Y') {
if ($stime != $confirm_stime) unset($confirm_start_time);
}
if (isset($confirm_end_time) && $confirm_end_time == 'Y') {
if ($etime != $confirm_etime) unset($confirm_end_time);
}
// Check for extremely long time block values
if (!isset($confirm_block_length) && $confirm_block_length != "Y") {
if (($stime + 60 * 60 * 12) < $etime) {
$warning_message = getLanguageText('WARNING-VERY-LONG-TIME-BLOCK');
$warning_message .= "<br>\n" . getLanguageText('MESSAGE-PLEASE-CONFIRM-ENTRY');
$confirmBlockLength = "Y";
include "schedule_time_blocks.popup.form.php";
return;
}
}
// Check for unexpected start time
if (!isset($confirm_start_time) && $confirm_start_time != "Y") {
if ($start_hour < "07" || $start_hour > "20") {
$warning_message = getLanguageText('WARNING-UNEXPECTED-START-TIME');
$warning_message .= "<br>\n" . getLanguageText('MESSAGE-PLEASE-CONFIRM-ENTRY');
$confirmStartTime = "Y";
include "schedule_time_blocks.popup.form.php";
return;
}
}
// Check for unexpected end time
if (!isset($confirm_end_time) && $confirm_end_time != "Y") {
if ($end_hour < "07" || $end_hour > "21") {
$warning_message = getLanguageText('WARNING-UNEXPECTED-END-TIME');
$warning_message .= "<br>\n" . getLanguageText('MESSAGE-PLEASE-CONFIRM-ENTRY');
$confirmEndTime = "Y";
include "schedule_time_blocks.popup.form.php";
return;
}
}
// Insert the time block(s)
$date = $sdate;
while ($date <= $edate) {
// Check the day of the week
$dow = date("l",$date);
if (!in_array($dow,$selected_days)) {
$date += 24 * 60 * 60;
continue;
}
// Check for selected dates
if ($timeblocks_added == 'calendar') {
$cdate = date("Y-m-d",$date);
if (!in_array($cdate,$selectedDates)) {
$date += 24 * 60 * 60;
continue;
}
}
// Format the datetime for each loop date
$time_block_date = date("Y-m-d",$date + 2 * 60 * 60);
$start_time = "$time_block_date $start_hour:$start_minute";
$end_time = "$time_block_date $end_hour:$end_minute";
$table = new Table(schedule_times);
$query = $table->BuildInsert();
db_query($query);
$date += 24 * 60 * 60;
}
// Set insert defaults
$start_time = $ostart_time;
$end_time = $oend_time;
$appointment_duration = $entered_appointment_duration;
$next_naction = "insert";
$ButtonText = getLanguageText('TEXT-INSERT-RECORD');
// Display the form
$form_message = getLanguageText('RECORD-INSERTED-MSG');
include "schedule_time_blocks.popup.form.php";
return;
case "update":
extract($_POST);
// Setup parameters for form
$ButtonText = getLanguageText('TEXT-UPDATE-RECORD');
$next_naction = "update";
// Format the time values
$start_time = "$time_block_date $start_hour:$start_minute";
$end_time = "$time_block_date $end_hour:$end_minute";
$stime = strtotime($start_time);
$etime = strtotime($end_time);
// Validate the start and end times
if ($etime <= $stime) {
$error_message = getLanguageText('ERRORMSG-END-TIME-BEFORE-START-TIME');
$start_time = $time_block['start_time'];
$end_time = $time_block['end_time'];
include "schedule_time_blocks.popup.form.php";
return;
}
// Ensure duration is > 0
if ($appointment_duration == 0) {
$error_message = getLanguageText('ERRORMSG-APPOINTMENT-DURATION-ZERO');
$appointment_duration = $time_block['appointment_duration'];
include "schedule_time_blocks.popup.form.php";
return;
}
// Get existing record
$results = db_query("SELECT * FROM schedule_times WHERE school_num = $school_num AND schedule_num = $schedule_num AND record_num = $timeblock_num");
$time_block = db_fetch_assoc($results);
// Check for overlapping time blocks
$query = "SELECT * FROM schedule_times " .
"WHERE school_num = $school_num " .
"AND schedule_num = $schedule_num " .
"AND record_num != $timeblock_num " .
"AND (('$start_time' > start_time AND end_time > '$start_time') " .
"OR ('$end_time' > start_time AND end_time > '$end_time'))";
$results = db_query($query);
if (db_num_rows($results)) {
$error_message = getLanguageText('ERRORMSG-OVERLAPPING-TIME-BLOCKS');
$start_time = $time_block['start_time'];
$end_time = $time_block['end_time'];
include "schedule_time_blocks.popup.form.php";
return;
}
// Check for extremely long time block values
if (!isset($confirm_block_length) && $confirm_block_length != "Y") {
if (($stime + 60 * 60 * 12) < $etime) {
$warning_message = getLanguageText('WARNING-VERY-LONG-TIME-BLOCK');
$warning_message .= "<br>\n" . getLanguageText('MESSAGE-PLEASE-CONFIRM-ENTRY');
$confirmBlockLength = "Y";
include "schedule_time_blocks.popup.form.php";
return;
}
}
// Check for unexpected start time
if (!isset($confirm_start_time) && $confirm_start_time != "Y") {
if ($start_hour < "07" || $start_hour > "20") {
$warning_message = getLanguageText('WARNING-UNEXPECTED-START-TIME');
$warning_message .= "<br>\n" . getLanguageText('MESSAGE-PLEASE-CONFIRM-ENTRY');
$confirmStartTime = "Y";
include "schedule_time_blocks.popup.form.php";
return;
}
}
// Check for unexpected end time
if (!isset($confirm_end_time) && $confirm_end_time != "Y") {
if ($end_hour < "07" || $end_hour > "21") {
$warning_message = getLanguageText('WARNING-UNEXPECTED-END-TIME');
$warning_message .= "<br>\n" . getLanguageText('MESSAGE-PLEASE-CONFIRM-ENTRY');
$confirmEndTime = "Y";
include "schedule_time_blocks.popup.form.php";
return;
}
}
// Check if there are appointments booked for this schedule already
$num_appts = db_getVal("SELECT count(*) FROM appointments " .
"WHERE school_num = $school_num " .
"AND schedule_num = $schedule_num " .
"AND appointment_type = 'Appointment' " .
"AND appointment_time >= '$start_time' " .
"AND appointment_time < '$end_time'");
// Check for changes to the appointment duration
$updateEndTimes = false;
if ($num_appts > 0 && $appointment_duration != $time_block['appointment_duration']) {
if ($appointment_duration < $time_block['appointment_duration']) {
$remainder = $time_block['appointment_duration'] % $appointment_duration;
if ($remainder == 0) {
// Update appointment end times
$updateEndTimes = true;
}
else {
$error_message = getLanguageText('ERRORMSG-UNABLE-TO-ADJUST-EXISTING-APPOINTMENTS') . " - $remainder";
include "schedule_time_blocks.popup.form.php";
return;
}
}
else {
// Calculate set of new appointment times for this block and build a where clause to locate invalid appointments
$where = "";
$stime = strtotime($start_time);
$etime = strtotime($end_time);
$time = $stime;
$duration = $appointment_duration * 60;
while ($time < $etime) {
if (strlen($where)) $where .= ",";
$where .= "'" . date("Y-m-d H:i:s",$time) . "'";
$time += $duration;
}
$query = "SELECT count(*) FROM appointments " .
"WHERE school_num = $school_num " .
"AND schedule_num = $schedule_num " .
"AND appointment_type = 'Appointment' " .
"AND appointment_time >= '$start_time' " .
"AND appointment_time < '$end_time' " .
"AND appointment_time NOT IN ($where)";
$num_invalid = db_getVal($query);
if ($num_invalid > 0) {
$error_message = getLanguageText('ERRORMSG-UNABLE-TO-ADJUST-EXISTING-APPOINTMENTS');
include "schedule_time_blocks.popup.form.php";
return;
}
$updateEndTimes = true;
}
}
// Update the record
$table = new Table(schedule_times);
$query = $table->BuildUpdate();
db_query($query);
// Check for orphaned appointments
$orphans = checkForOrphans($schedule_num);
if ($orphans && db_num_rows($orphans)) {
// Get new start and end hour
$timeStart = date("H:i:s",$stime);
$timeEnd = date("H:i:s",$etime);
// Get original start and end hour
$ostime = strtotime($time_block['start_time']);
$oetime = strtotime($time_block['end_time']);
$otimeStart = date("H:i:s",$ostime);
$otimeEnd = date("H:i:s",$oetime);
// If only the appointment date has changed then update the appointments
if ($timeStart == $otimeStart && $timeEnd == $otimeEnd && $appointment_duration == $time_block['appointment_duration'] &&
$break_interval == $time_block['break_interval'] && $break_frequency == $time_block['break_frequency']) {
// Appointment dates
$old_date = date("Y-m-d",$ostime);
$new_date = date("Y-m-d",$stime);
// Update all appointments with the new date
$ostart_time = date("Y-m-d H:i:s",$ostime);
$oend_time = date("Y-m-d H:i:s",$oetime);
// Get the entries for this time block
$appointments = db_query("SELECT * FROM appointments WHERE school_num = $school_num AND schedule_num = $schedule_num AND appointment_time >= '$ostart_time' AND appointment_time < '$oend_time'");
$acount = 0;
while ($row = db_fetch_assoc($appointments)) {
$astime = str_replace($old_date,$new_date,$row['appointment_time']);
$aetime = str_replace($old_date,$new_date,$row['appointment_end_time']);
$rnum = $row['record_num'];
db_query("UPDATE appointments SET appointment_time = '$astime',appointment_end_time = '$aetime' WHERE record_num = $rnum");
if ($row['appointment_type'] == 'Appointment') $acount++;
}
$form_message = getLanguageText('MESSAGE-TIMEBLOCK-APPOINTMENTS-UPDATED') . " - $acount";
}
else {
// Display warning and list orphaned appointments
$error_message = getLanguageText('ERRORMSG-CHANGE-CREATES-ORPHANED-APPOINTMENTS');
$warning_message = getLanguageText('MESSAGE-APPOINTMENTS-HAVE-TO-BE-DELETED');
include "form.messages.php";
// List appointments to be deleted
$TableWidth = $default_popup_form_width;
listAppointments($orphans);
// Restore the start and end times for this block
$start_time = $time_block['start_time'];
$end_time = $time_block['end_time'];
$time_block_date = date("Y-m-d",$stime);
$time_block_end_date = date("Y-m-d",$etime);
$query = $table->BuildUpdate();
db_query($query);
echo("<br>\n");
}
}
else {
$form_message = getLanguageText('RECORD-UPDATED-MSG');
if ($updateEndTimes == true) {
$duration = $appointment_duration * 60;
// Get set of appointments to process
$appointments = db_query("SELECT * FROM appointments " .
"WHERE school_num = $school_num " .
"AND schedule_num = $schedule_num " .
"AND appointment_time >= '$start_time' " .
"AND appointment_time < '$end_time'");
// Process each appointment found
while ($row = db_fetch_assoc($appointments)) {
$appt_time = strtotime($row['appointment_time']);
$end_time = date("Y-m-d H:i:s",$appt_time + $duration);
$rnum = $row['record_num'];
db_query("UPDATE appointments SET appointment_end_time = '$end_time' WHERE schedule_num = $schedule_num AND record_num = $rnum");
}
$form_message .= "<br>" . getLanguageText('MESSAGE-APPOINTMENT-TIMES-UPDATED');
}
}
// Display the form
include "schedule_time_blocks.popup.form.php";
return;
case "delete":
// Get the current record
$results = db_query("SELECT * FROM schedule_times WHERE school_num = $school_num AND schedule_num = $schedule_num AND record_num = $timeblock_num");
$row = db_fetch_array($results);
extract($row);
// Get count of appointments in this time block
$count = db_getVal("SELECT count(*) FROM appointments WHERE appointment_time BETWEEN '$start_time' AND '$end_time'");
if ($count > 0) {
$error_message = getLanguageText('ERRORMSG-APPOINTMENTS-IN-TIMEBLOCK');
include "form.messages.php";
return;
}
$stime = strtotime($start_time);
$etime = strtotime($end_time);
// Format the dTitle
$dTitle = "<b>" . getLanguageText('TEXT-TIME-BLOCK') . "</b><br>\n" . date($school_record['date_format'] . ' ' . $school_record['time_format'],$stime) . ' - ' . date($school_record['time_format'],$etime);
// Setup hidden fields
$HiddenFields = " <input type='hidden' name='schedule_num' value='$schedule_num'>\n";
include "delete.form.php";
return;
case "delete-confirmed":
// Delete time block
db_query("DELETE FROM schedule_times WHERE school_num = $school_num AND schedule_num = $schedule_num AND record_num = $timeblock_num");
$form_message = getLanguageText('RECORD-DELETED-MSG');
include "form.messages.php";
return;
}
}
?>