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

377 lines
15 KiB
PHP

<?php
$DialogCustomization = array();
function LoadAction($FormActionName) {
global $GlobalText;
$query = "SELECT * FROM _form_actions WHERE action_name = '$FormActionName'";
$results = db_query($query);
if (db_num_rows($results)) {
$row = db_fetch_assoc($results);
$DialogTitle = $row['action_dialog_title'];
if (strpos($DialogTitle, 'TEXT-') === 0) $DialogTitle = getLanguageText($DialogTitle);
if (strpos($DialogTitle, 'GlobalText.') === 0) {
$pindex = str_replace('GlobalText.','',$DialogTitle);
$DialogTitle = $GlobalText[$pindex];
}
$row['action_dialog_title'] = $DialogTitle ;
return($row);
}
else {
return(array());
}
}
function LoadActionFields($FormActionName) {
global $GlobalText;
$return_data = array();
$FormDefaults = array();
$DialogFields = array();
// Get form fields
$query = "SELECT * FROM _form_fields WHERE action_name = '$FormActionName' ORDER BY sequence";
$formfields = db_query($query);
if (db_num_rows($formfields)) {
$SortField = "";
while ($row = db_fetch_assoc($formfields)) {
$fname = $row['action_fields_column_name'];
$fprompt = $row['action_fields_field_prompt'];
if (strpos($fprompt, 'TEXT-') === 0 || strpos($fprompt, 'CODE-') === 0) {
$row['action_fields_field_prompt'] = getLanguageText($fprompt);
}
if (strpos($fprompt, 'GlobalText.') === 0) {
$pindex = str_replace('GlobalText.','',$fprompt);
$row['action_fields_field_prompt'] = $GlobalText[$pindex];
}
if (strlen($row['action_fields_additional_message'])) {
$extraMsg = $row['action_fields_additional_message'];
if (strpos($extraMsg, 'TEXT-') === 0) $extraMsg = getLanguageText($extraMsg);
$row['action_fields_additional_message'] = $extraMsg ;
}
if (strlen($SortField) == 0) $SortField = $fname;
if (strlen($row['action_fields_default_field_value'])) {
$FormDefaults[] = array($fname, $row['action_fields_default_field_value']);
}
$DialogFields[] = $row;
}
}
$return_data['FormDefaults'] = $FormDefaults;
$return_data['DialogFields'] = $DialogFields;
return($return_data);
}
function BuildDialogForm($DialogTitle, $DialogFields, $DialogName, $FormName) {
global $FormActionName;
echo("<div id='$DialogName' title='$DialogTitle' style='overflow-y: scroll; height: 500px'>\n");
echo(" <center>\n");
echo(" <form id='$FormName'>\n");
echo(" <br>\n");
BuildForm($DialogTitle, $DialogFields, $DialogName, $FormName);
echo(" <br>\n");
echo(" <br>\n");
echo("</div>\n");
}
function BuildForm($DialogTitle, $DialogFields, $DialogName, $FormName) {
global $FormActionName;
global $school_num, $base_school_num, $CurrentFormAction;
global $LookupArrays, $DialogCustomization;
global $GlobalText;
// startLog("BuildForm");
$Required = getLanguageText('TEXT-REQUIRED');
$Select = getLanguageText('TEXT-SELECT');
$HiddenFields = "";
echo(" <table>\n");
echo(" <tr>\n");
echo(" <td align='left'>\n");
echo(" <div id='form-fields-div'>\n");
for ($i = 0; $i < count($DialogFields); $i++) {
$entry = $DialogFields[$i];
if (!array_key_exists('action_fields_column_name',$entry)) continue;
$fname = $entry['action_fields_column_name'];
$fprompt = $entry['action_fields_field_prompt'];
$ftype = $entry['action_fields_screen_field_type'];
$extraMsg = $entry['action_fields_additional_message'];
if (array_key_exists("$fname.action_fields_field_prompt", $DialogCustomization)) $fprompt = $DialogCustomization["$fname.action_fields_field_prompt"];
if (array_key_exists("$fname.action_fields_additional_message", $DialogCustomization)) $extraMsg = $DialogCustomization["$fname.action_fields_additional_message"];
$dfname = $fname."_default";
$HiddenFields .= " <input type='hidden' id='$dfname'>\n";
if ($ftype == 'exclude') continue;
$customCSS = "";
if (strlen($entry['action_fields_custom_css'])) {
$customCSS = $entry['action_fields_custom_css'];
$customCSS = " class='$customCSS'";
}
if ($ftype != 'hidden') {
$fprompt = "<span id='$fname-prompt'>$fprompt</span>";
echo(" <p id='{$fname}_p' $customCSS><b>$fprompt:</b>");
}
$rflag = $entry['action_fields_required_field_flag'];
if (array_key_exists("$fname.action_fields_required_field_flag", $DialogCustomization)) $rflag = $DialogCustomization["$fname.action_fields_required_field_flag"];
$required = 'required';
if ($rflag == 'N') $required = 'optional';
$align = 'ltext';
switch ($ftype) {
case 'integer':
case 'decimal':
case 'money':
$align = 'rtext';
case 'text':
$flength = intval($entry['action_fields_text_length']);
$field = "<input type='text' name='$fname' id='$fname' size='$flength' class='form-field $align ui-widget-content ui-corner-all $required' autocomplete='off'>";
if ($required == 'required') echo(" <span class='small'>($Required)</span>");
echo("<br>\n");
echo(" $field");
if (strlen($extraMsg)) echo("<br>\n <span class='small'>$extraMsg</span>");
echo("</p>\n");
break;
case 'textbox':
$flength = intval($entry['action_fields_text_length']);
$field = "<textarea cols='$flength' rows='5' name='$fname' id='$fname' ='$flength' class='form-field ui-widget-content ui-corner-all $required'></textarea>";
if ($required == 'required') echo(" <span class='small'>($Required)</span>");
echo("<br>\n");
echo(" $field");
if (strlen($extraMsg)) echo("<br>\n <span class='small'>$extraMsg</span>");
echo("</p>\n");
break;
case 'editbox':
$flength = intval($entry['action_fields_text_length']);
$field = "<textarea cols='$flength' rows='5' name='$fname' id='$fname' ='$flength' class='form-field ui-widget-content ui-corner-all summernote $required'></textarea>";
if ($required == 'required') echo(" <span class='small'>($Required)</span>");
echo("<br>\n");
echo(" $field");
if (strlen($extraMsg)) echo("<br>\n <span class='small'>$extraMsg</span>");
echo("</p>\n");
break;
case 'hidden':
$today = date("Y-m-d", time());
$now = date("Y-m-d H:i:s", time());
$fvalue = $entry['action_fields_default_field_value'];
switch($fvalue) {
case 'today':
$fvalue = $today;
break;
case 'now':
$fvalue = $now;
break;
}
if ($fname == 'action_name') $fvalue = $CurrentFormAction;
$field = "<input type='hidden' name='$fname' id='$fname' value='$fvalue'>";
echo(" $field\n");
break;
case 'date':
$flength = 12;
$field = "<input type='text' name='$fname' id='$fname' size='$flength' " .
"class='form-field date-field ui-widget-content ui-corner-all $required'>";
if ($required == 'required') echo(" <span class='small'>($Required)</span>");
echo("<br>\n");
echo(" $field");
if (strlen($extraMsg)) echo("<br>\n <span class='small'>$extraMsg</span>");
echo("</p>\n");
break;
case 'datetime':
$flength = 20;
$field = "<input type='text' name='$fname' id='$fname' size='$flength' " .
"class='form-field datetime-field ui-widget-content ui-corner-all $required'>";
if ($required == 'required') echo(" <span class='small'>($Required)</span>");
echo("<br>\n");
echo(" $field");
if (strlen($extraMsg)) echo("<br>\n <span class='small'>$extraMsg</span>");
echo("</p>\n");
break;
case 'select':
case 'select-records':
$options_field = $entry['action_fields_select_options'];
$options = array();
if (isset($LookupArrays)) {
$fkey = $FormActionName.'.'.$fname;
if (array_key_exists($fkey, $LookupArrays)) {
$options = $LookupArrays[$fkey];
}
}
// Parse set of select options that are defined in the _form_fields record
if (count($options) == 0 && strlen($options_field)) {
if ($options_field == 'YN') {
$options = array('Y' => 'Yes','N' => 'No');
}
if (strpos($options_field, ",") > 0) {
$option_fields = explode(",", $options_field);
if (count($option_fields) >= 1) {
for ($n = 0; $n < count($option_fields); $n++) {
$option = str_replace("'","",$option_fields[$n]);
$options[$option] = $option;
}
}
}
}
// Parse a file to get the set of options
if (count($options) == 0 && strlen($entry['action_fields_select_file'])) {
$options = GetFileListArray($entry['action_fields_select_file'], "", "");
}
// Execure an SQL query to get a set of records for the select list
if (count($options) == 0 && strlen($entry['action_fields_select_records_sql'])) {
$query = $entry['action_fields_select_records_sql'];
if (stripos($query, "where ") !== false) {
$query = str_ireplace("where ", "where school_num = $school_num and ", $query);
}
else {
if (stripos($query, "order by ") !== false) {
$query = str_ireplace("order by ", "where school_num = $school_num order by ", $query);
}
else {
$query .= " where school_num = $school_num";
}
}
$results = db_query($query);
if (db_num_rows($results)) {
$options = array();
while ($row = db_fetch_array($results)) {
$nfields = (count($row) / 2) - 1;
$rnum = 0;
if (array_key_exists('record_num', $row)) $rnum = $row['record_num'];
$title = "";
for ($j = 0; $j < $nfields; $j++) {
if (strlen($row[$j])) $title .= $row[$j].' ';
}
$title = trim($title);
$options[$rnum] = $title;
}
}
}
$field = "<select name='$fname' id='$fname' class='form-field $required ui-widget-content ui-corner-all'>\n";
$field .= " <option value=''>$Select: $fprompt</option>\n";
$OptionSelected = "";
$optgroup_started = false;
foreach ($options AS $value => $option) {
if (is_array($option)) {
$value = $entry[0];
$otext = $entry[1];
if (strpos($otext, 'TEXT-') === 0 || strpos($otext, 'CODE-') === 0) $otext = getLanguageText($otext);
if (strpos($otext, 'GlobalText.') === 0) {
$pindex = str_replace('GlobalText.','',$otext);
$otext = $GlobalText[$pindex];
}
}
else {
$otext = $option;
if (strpos($otext, 'TEXT-') === 0 || strpos($otext, 'CODE-') === 0) $otext = getLanguageText('TEXT-'.str_replace(' ','-',$otext));
if (strpos($otext, 'GlobalText.') === 0) {
$pindex = str_replace('GlobalText.','',$otext);
$otext = $GlobalText[$pindex];
}
}
if (strstr($value, 'OPTGROUP-') !== false) {
if ($optgroup_started) $field .= " </optgroup>\n";
$field .= " <optgroup label='$otext'>\n";
$optgroup_started = true;
}
else {
$field .= " <option value='$value'$OptionSelected>$otext</option>\n";
}
$OptionSelected = "";
}
if ($optgroup_started) $field .= " </optgroup>\n";
$field .= " </select>";
if ($required == 'required') echo(" <span class='small'>($Required)</span>");
echo("<br>\n");
echo(" $field");
if (strlen($extraMsg)) echo("<br>\n <span class='small'>$extraMsg</span>");
echo("</p>\n");
break;
}
}
$hiddenParameterFields = "";
if (isset($_GET) && count($_GET) > 0) {
echo(" <!-- ------------------------- -->\n");
foreach ($_GET AS $key => $value) {
if ($key != 'action') $hiddenParameterFields .= " <input type='hidden' name='$key' id='$key' value='$value'>\n";
}
echo($hiddenParameterFields);
echo(" <!-- ------------------------- -->\n\n");
}
echo($HiddenFields);
if (isset($CurrentFormAction) && strlen($CurrentFormAction)) {
echo(" <input type='hidden' name='action_name' id='action_name' value='$CurrentFormAction'>\n");
}
echo(" <input type='hidden' name='record_num' id='record_num'>\n");
echo(" </div>\n");
echo(" </td>\n");
echo(" </tr>\n");
echo(" </table>\n");
echo(" </form>\n");
echo(" <br>\n");
echo(" <br>\n");
echo(" <button id='save-$DialogName'> ".getLanguageText('TEXT-SAVE')." </button>");
echo(" <button id='close-$DialogName'> ".getLanguageText('TEXT-CLOSE')." </button>");
echo(" </center>\n");
}
?>