Files
HotLunches/hotlunch_schedule_copy.php
T
2026-10-19 15:22:52 -07:00

277 lines
11 KiB
PHP

<?php
require_once("include/globals.php");
if (SessionActive() != 1 || ! UserLoggedIn()) {
header("Location: /");
exit();
}
// Ensure that only valid user types have access to this module
$AllowedUserTypes = array('SuperUser','System Administrator','Application Administrator','Application Volunteer','Schedule Volunteer','Administration Team','Schedule Team');
if (checkPermission($AllowedUserTypes) != 1) {
echo("<br>\n");
$error_message = getLanguageText('ERRORMSG-ACCESS-DENIED');
include "form.messages.php";
return;
}
// startLog("hotlunch_schedule_copy.php");
// Start the template and output buffering
startTemplate();
$templatePageTitle = getLanguageText('TEXT-COPY-SCHEDULE');
$cutoff = date("Y-m-d",time() - 60 * 60 * 24 * 450);
writeLog("cutoff: $cutoff");
$Schedules = array();
$ScheduleNums = array();
$supplier_school_num = intval($school_record['supplier_school_num']);
if ($supplier_school_num == 0) $supplier_school_num = $school_num;
writeLog("current_school_num: $school_num");
writeLog("supplier_school_num: $supplier_school_num");
if ($supplier_school_num > 0) {
$query = "SELECT * FROM hotlunch_schedules WHERE school_num = $supplier_school_num AND status != 'Deleted' ORDER BY schedule_start_date DESC";
writeLog($query);
$schedules = db_query($query);
writeLog("Num records: ".db_num_rows($schedules));
if (db_num_rows($schedules)) {
while ($row = db_fetch_assoc($schedules)) {
$snum = $row['record_num'];
if (!in_array($snum, $ScheduleNums)) {
$ScheduleNums[] = $snum;
$numMenus = db_getVal("SELECT count(*) FROM hotlunch_menu_dates WHERE school_num = $supplier_school_num AND schedule_num = $snum");
writeLog("numMenus: $numMenus");
if ($numMenus > 0) {
$numItems = db_getVal("SELECT count(*) FROM hotlunch_menu_items WHERE school_num = $supplier_school_num AND menu_num IN " .
"(SELECT record_num FROM hotlunch_menu_dates WHERE school_num = $supplier_school_num AND schedule_num = $snum)");
if ($numItems > 0) {
$row['numMenus'] = $numMenus;
$Schedules[] = $row;
}
}
}
}
}
}
writeLog("Count: ".count($Schedules));
$query = "SELECT * FROM hotlunch_schedules WHERE school_num = $school_num AND status != 'Deleted' ORDER BY schedule_start_date DESC";
writeLog($query);
$schedules = db_query($query);
writeLog("Num records: ".db_num_rows($schedules));
while ($row = db_fetch_assoc($schedules)) {
$snum = $row['record_num'];
if (!in_array($snum, $ScheduleNums)) {
$ScheduleNums[] = $snum;
$numMenus = db_getVal("SELECT count(*) FROM hotlunch_menu_dates WHERE school_num = $school_num AND schedule_num = $snum");
writeLog("numMenus: $numMenus");
if ($numMenus > 0) {
$numItems = db_getVal("SELECT count(*) FROM hotlunch_menu_items WHERE school_num = $school_num AND menu_num IN " .
"(SELECT record_num FROM hotlunch_menu_dates WHERE school_num = $school_num AND schedule_num = $snum)");
if ($numItems > 0) {
$row['numMenus'] = $numMenus;
$Schedules[] = $row;
}
}
}
}
$resellerCode = $school_record['reseller'];
writeLog("resellerCode: $resellerCode");
if (strlen($resellerCode) && $resellerCode != 'hotlunch') {
$query = "SELECT school_name,record_num FROM schools " .
"WHERE reseller = '$resellerCode' " .
"AND status IN ('Active','Special','Linked') " .
"AND record_num NOT IN ($school_num,$supplier_school_num) " .
"ORDER BY school_name";
writeLog($query);
$rschools = db_query($query);
if (db_num_rows($rschools)) {
while ($row = db_fetch_assoc($rschools)) {
$school = $row['record_num'];
$query = "SELECT * FROM hotlunch_schedules WHERE school_num = $school AND status != 'Deleted' ORDER BY schedule_start_date DESC";
writeLog($query);
$schedules = db_query($query);
writeLog("Num records: ".db_num_rows($schedules));
while ($row = db_fetch_assoc($schedules)) {
$snum = $row['record_num'];
if (!in_array($snum, $ScheduleNums)) {
$ScheduleNums[] = $snum;
$numMenus = db_getVal("SELECT count(*) FROM hotlunch_menu_dates WHERE school_num = $school AND schedule_num = $snum");
writeLog("numMenus: $numMenus");
if ($numMenus > 0) {
$row['numMenus'] = $numMenus;
$Schedules[] = $row;
}
}
}
}
}
}
writeLog("Count: ".count($Schedules));
if (count($Schedules) == 0) {
$error_message = getLanguageText('MESSAGE-NO-SCHEDULES-AVAILABLE');
include "form.messages.php";
endTemplate();
return;
}
?>
<center>
<div id='error-msg-div' style='background-color: #ff0000; color: #ffffff;'></div>
<div id='spinner-div' style='z-index: 99999;'></div>
<br>
<form method='post' name='schedule-copy-form' id='schedule-copy-form'>
<table width='450' cellpadding='5' class='content-table'>
<tr>
<td align='left'>
<p><b><?php displayLanguageText('TEXT-SOURCE-SCHEDULE'); ?>:</b></p>
<p><label for='source_schedule'><b><?php displayLanguageText('TEXT-SCHEDULE'); ?>:</b></label><br>
<select name='source_schedule' id='source_schedule' class='select-width600'>
<option value='0'><?php displayLanguageText('TEXT-SELECT-A-SCHEDULE'); ?></option>
<?php
$pnum = 0;
$menuText = getLanguageText('TEXT-MENUS');
for ($i = 0; $i < count($Schedules); $i++) {
$row = $Schedules[$i];
$cnum = $row['school_num'];
$sname = $row['schedule_name'];
$stype = $row['schedule_type'];
$snum = $row['record_num'];
$sdate = $row['schedule_start_date'];
$numMenus = $row['numMenus'];
$sday = date("w",strtotime($sdate));
$swday = date("D",strtotime($sdate));
if ($cnum != $pnum && $supplier_school_num > 0) {
if ($pnum > 0) echo("</optgroup>\n");
$school_name = db_getVal("SELECT school_name FROM schools WHERE record_num = $cnum");
$school_name = str_replace("'","&apos;",$school_name);
echo("<optgroup label='$school_name'>\n");
}
$pnum = $cnum;
$sdate = getLanguageText('TEXT-START-DATE').": $sdate";
echo("<option value='$snum' sday='$sday'>$sname - $stype ($menuText: $numMenus - $sdate)</option>\n");
}
if ($supplier_school_num > 0) echo("</optgroup>\n");
?>
</select></p>
<div id='schedule-info-div' style='margin-left: 30px;'></div>
<br>
<p><b><?php displayLanguageText('TEXT-NEW-SCHEDULE'); ?>:</b></p>
<p><label for='new_schedule'><b><?php displayLanguageText('TEXT-SCHEDULE-NAME'); ?>:</b></label><br>
<input name='new_schedule' id='new_schedule' size='40'></p>
<p><label for='ordering_start'><b><?php displayLanguageText('TEXT-ORDERING-START-DATE'); ?>:</b></label><br>
<input name='ordering_start' id='ordering_start' size='20' class='datetime-field'><br>
<span class='small'><?php displayLanguageText('TEXT-DATE-ORDERING-WILL-COMMENCE-FOR-THIS-SCHEDULE'); ?></span></p>
<p><label for='ordering_end'><b><?php displayLanguageText('TEXT-ORDERING-END-DATE'); ?>:</b></label><br>
<input name='ordering_end' id='ordering_end' size='20' class='datetime-field'></p>
<br>
<p><b><?php displayLanguageText('TEXT-SCHEDULE-COPY-MESSAGE'); ?></b></p>
<p><label for='schedule_start'><b><?php displayLanguageText('TEXT-SCHEDULE-START-DATE'); ?>:</b></label><br>
<input name='schedule_start' id='schedule_start' size='15' class='date-field'><br>
<span class='small'><?php displayLanguageText('TEXT-FIRST-MENU-DATE'); ?></span></p>
</td>
</tr>
</table>
<br>
<button id='copy-schedule' class='btn btn--outline btn--xsmall'> <?php displayLanguageText('TEXT-COPY-SCHEDULE'); ?> </button>
</form>
</center>
<script>
$(document).ready(function() {
<?php include "include/ajax.Setup.php" ?>
$("#source_schedule").change(function() {
var snum = $(this).val();
$.getJSON( "ajax.DataTable.Fetch.php",
{
table_name: "hotlunch_schedules",
sequence_flag: "N",
action: "schedule-info",
schedule_num: snum
},
function(results) {
$("#schedule-info-div").html(results.content);
$("#new_schedule").val(results.new_schedule);
$("#ordering_start").val(results.ordering_start);
$("#ordering_end").val(results.ordering_end);
$("#schedule_start").val(results.schedule_start);
}
);
});
$("#copy-schedule").on('click',function(e) {
e.preventDefault();
var schedule_num = $("#source_schedule").val();
var schedule_title = $("#new_schedule").val();
var order_start = $("#ordering_start").val();
var order_end = $("#ordering_end").val();
var schedule_start = $("#schedule_start").val();
var noSchedule = "<?php displayLanguageText('ERRORMSG-NO-SCHEDULES-SELECTED'); ?>";
var missingData = "<?php displayLanguageText('ERRORMSG-MISSING-FORM-DATA'); ?>";
if (schedule_num == '0') {
alert(noSchedule);
return(false);
}
if (schedule_title.length == 0 || order_start.length == 0 || order_end.length == 0 || schedule_start.length == 0) {
alert(missingData);
return(false);
}
$("#spinner-div").html("<p><img src='images/spinner.gif'></p>");
$("#schedule-copy-form").hide();
$.ajax({
url: "ajax.ScheduleCopy.php",
data: {
source_schedule: schedule_num,
schedule_title: schedule_title,
order_start: order_start,
order_end, order_end,
schedule_start: schedule_start
},
dataType: "json"
}).done(function(data) {
$("#spinner-div").html("");
$("#schedule-copy-form").show();
if (data.ajax_return_code != 0) {
$("#error-msg-div").html(data.ajax_message);
return false;
}
var newSchedule = data.schedule_num;
location.href = "?action=schedule-reports&schedule_num="+newSchedule;
return true;
});
});
});
</script>
<?php
endTemplate();
?>