Files
WTS-Public/inc/HelperFunctions.php
T
2026-10-27 20:22:37 -05:00

715 lines
22 KiB
PHP

<?php
/// @file HelperFunctions.php
/// This file contains a number of helper functions that are commonly used
/// to translate to and from various formats (e.g. text to db and db to text
/// and web to db and db to web).
///
/// @attention This program is copyright (c) 2009 - 2024 by Smartware Computing, all
/// rights reserved.
/// \n\n
/// This software, and/or material is the property of Smartware Computing. All
/// use, disclosure, and/or reproduction not specifically authorized by Smartware
/// Computing is prohibited.
///
/// @author David Smart, Smartware Computing
///
require_once("Config.inc.php");
//require_once("dbQueries.php");
///@cond DONT_DOXYGEN
// Standard script protection for files that should not be directly accessed
if (stristr($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
print "Web wanderer coming from IP " . $_SERVER["REMOTE_ADDR"] . ", you cannot access an include file directly.";
exit;
}
///@endcond
define("TR_ENTRY", 0);
define("TR_INTERNAL", 1);
define("TR_EXIT", 2);
$AppRoot = "";
$TracePath = "";
$timeFuncs = array(); # Holds the starting time of a func entry for func timing...
/// SetTraceRoot
///
/// Set the root folder for the trace, which is then subtracted when writing the log
/// to make the output smaller - fewer columns.
///
/// @param $folder is the root folder to sutract from filenames.
///
function SetTraceRoot($folder, $tracePath = "./inc/TraceLog/") {
global $AppRoot;
if (!is_dir($tracePath)) {
mkdir($tracePath, 0777, true);
}
//TraceProgram(__FILE__,__FUNCTION__,__LINE__,"SetTraceRoot($folder)");
$AppRoot = "$folder";
$TracePath = $tracePath;
}
// Example:
// Elapsed, tag, nest, type, timestamp, file, line, function, user text.
// 134.4000, 67, 3, 2, 17:24.37, /volume1/web/X10/index.php, 393, / ButtonDefinition,
//
// tag is a unique number for the given process thread. overlapping are then visible.
/// TraceFunctions
///
/// Traces into a log file the path of the program where there are
/// trace functions called.
///
/// @param $file is the current file, and is usually populated with __FILE__
/// @param $func is the current function and is usually populated with __FUNCTION__
/// @param $line is the current line number and is usually populated with __LINE__
/// @param $msg is the text message to put in the trace report and is whatever you want
/// @param $flag is one of these values: TR_ENTRY, TR_INTERNAL | "", TR_EXIT
///
function TraceProgram($file,$func,$line,$msg,$flag=1) {
global $time_start, $timeFuncs;
global $AppRoot;
static $nest = 0;
static $tag = 0;
$func_elapsed = "";
$uTnow = microtime(true);
$elapsed = $uTnow - $time_start;
switch($flag) {
case TR_ENTRY:
$timeFuncs[$nest] = $elapsed;
$flag = 0;
if ($tag == 0)
$tag = mt_rand(0,999); // abs(($uTnow * 1000000) % 1000); // rand(1, 99);
$pre = " \\ ";
break;
case TR_EXIT:
$flag = 2;
$pre= " / ";
$nest--;
if ($nest < 0) {
printf("<font color='red'>ERROR in Nesting. Depth -1 from %s, %s: %d [%s]</font><br/>",
$file, $func, $line, $msg);
}
$func_elapsed = sprintf("%9.4f", $elapsed - $timeFuncs[$nest]);
break;
default:
$flag = 1;
$pre = " | ";
break;
}
if (strlen($AppRoot) && strpos($file, $AppRoot) >= 0) {
$file = substr($file, strlen($AppRoot));
}
// if ($nest < 0)
// $nest = 0;
// + Elapsed,
// | + Function Elapsed
// | | +tag, +nest,
// | | | | +type,
// | | | | | +timestamp,
// | | | | | | +file, +line,
// | | | | | | | | +whitespace
// | | | | | | | | | +flag
// | | | | | | | | | | +function,
// | | | | | | | | | | | +user text.
$o = sprintf("%9.4f, %9s, %03d, %2d, %s, %s, %-26s, %5s, %s%s%-24s, %s",
$elapsed,
$func_elapsed,
$tag,
$nest,
$flag,
date("H:i.s"),
$file,
$line,
str_repeat(" ",$nest), $pre, $func,
$msg);
// Uncomment these when you need to see the details from the command line, but don't
// want to generate the tracefile.
//
//if (!count($_SESSION)) // && $flag == TR_ENTRY) // zero if running from a command shell
// printf("%s\n", $o);
if (TRACELOG) {
$fh = @fopen(TRACEPATH . "/" . TRACEFILE, "at");
if ($fh) {
if ($flag == TR_ENTRY && $func == "")
fprintf($fh, "\r\n\r\n");
fprintf($fh, "%s\n", $o);
fclose($fh);
} else {
echo "Configuration Error, cannot write to trace file [" . TRACEPATH ."/" . TRACEFILE . "\n";
}
}
// Track the stack depth
if ($flag == TR_ENTRY)
$nest++;
}
function deleteFilesWithWildcard($directory, $pattern) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "del($directory, $pattern)", TR_ENTRY);
if (!is_dir($directory)) {
throw new InvalidArgumentException("Directory does not exist: $directory");
}
$searchPath = rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $pattern;
$files = glob($searchPath);
if ($files === false) {
throw new RuntimeException("Error reading files with glob()");
}
// Delete each file
foreach ($files as $file) {
if (is_file($file)) { // Ensure it's a file, not a directory
if (@unlink($file)) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Deleted $file");
} else {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "Delete Failed on $file");
}
}
}
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
}
function GetFolderUp(string $path): ?string {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_ENTRY);
if (trim($path) === '') { // Basic validation
return null; // Empty path
}
$normalizedPath = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path); // Normalize slashes for cross-platform compatibility
$parent = dirname($normalizedPath); // Use dirname to get one level up
if ($parent === '.' || $parent === DIRECTORY_SEPARATOR) { // If dirname returns '.' for relative paths, handle it
$parent = null; // No parent directory
}
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $parent, TR_EXIT);
return $parent;
}
function GetPrevEvent($curItem = '') {
global $EventsPath, $EventFormFields;
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_ENTRY);
$prevEvent = '';
$ref = ($curItem != '') ? $curItem : date("Y-m-d");
$events = GetDocMap($EventsPath, $EventFormFields, 'ProgramDate');
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "$ref: " . print_r($events, true));
foreach (array_keys($events) as $event) {
if ($ref > $event) {
$prevEvent = $event;
}
}
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $prevEvent, TR_EXIT);
return $prevEvent;
}
function GetNextEvent($curItem = '') {
global $EventsPath, $EventFormFields;
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_ENTRY);
$nextEvent = '';
$ref = ($curItem != '') ? $curItem : date("Y-m-d");
$events = GetDocMap($EventsPath, $EventFormFields, 'ProgramDate');
foreach (array_keys($events) as $event) {
if ($nextEvent == '') {
if (($curItem == '') && ($ref <= $event)) {
$nextEvent = $event;
} else if (($curItem != '') && ($ref < $event)) {
$nextEvent = $event;
}
}
}
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "[$nextEvent]", TR_EXIT);
return $nextEvent;
}
function EmitForm($formFields, $formData, $pathToArtifacts, $thisItem, $showNavigation = false) {
global $PlacesPath, $PlaceFormFields;
global $ProgChairPath, $ProgChairFormFields;
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "$pathToArtifacts, $thisItem", TR_ENTRY);
$CK_EditorFieldIDs = array();
$showHR = false;
printf("<table border='%d' width='100%%'>\n", TBL_BORDER);
if ($showNavigation) {
$prev = GetPrevEvent($thisItem);
$next = GetNextEvent($thisItem);
if ($prev != '') {
$linkPrev = "<a href='?Action=EventEdit&ProgramDate=$prev'>&lt;==</a>";
} else {
$linkPrev = "&lt;==";
}
if ($next != '') {
$linkNext = "<a href='?Action=EventEdit&ProgramDate=$next'>==&gt;</a>";
} else {
$linkNext = "==&gt;";
}
printf("<tr><td align='center'>%s&nbsp;&nbsp;Navigate&nbsp;&nbsp;%s</td><td><hr></td></tr>\n", $linkPrev, $linkNext);
$showHR = true;
}
foreach ($formFields as $key => $data) {
if ($data['bg']) {
printf("<tr><td colspan='2'><hr></td></tr>\n");
$showHR = true;
}
printf("<tr>\n");
printf("\t<td valign='top' nowrap><b><label for='%s'>%s</label></b></td>\n",
$key, $data['Label']);
$value = ($formData[$key]) ?? $formFields[$key]['Default'];
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "[$value]");
switch ($data['FieldType']) {
case 'text':
if ($formFields[$key]['RW'] == 'W') {
printf("<td><input type='text' name='%s' id='%s' value='%s' placeholder='%s' size='80' /></td>\n",
$key, $key, $value, $formFields[$key]['Placeholder']);
} else {
printf("<td>%s<input type='hidden' name='%s' id='%s' value='%s' placeholder='%s' size='80' /></td>\n",
$value, $key, $key, $value, $formFields[$key]['Placeholder']);
}
break;
case 'date':
printf("<td>\n");
if ($formFields[$key]['RW'] == 'W') {
printf("<input type='date' name='%s' id='%s' value='%s' %s />\n",
$key, $key, $value, ($formFields[$key]['RW'] != 'W') ? 'disabled' : '');
} else {
printf("%s<input type='hidden' name='%s' id=%s' value='%s' /></td>\n",
DateToMDY($value), $key, $key, $value);
}
printf("</td>\n");
break;
case 'time':
printf("<td><input type='time' name='%s' id='%s' value='%s' /></td>\n",
$key, $key, $value);
break;
case 'bool':
printf("<td><input type='checkbox' name='%s' id='%s' value='1' %s /></td>\n",
$key, $key, ($value) ? "checked" : "");
break;
case 'html':
printf("<td><textarea class='textarea' name='%s' id='%s' cols='60' rows='5' />%s</textarea></td>\n",
$key, $key, $value);
$CK_EditorFieldIDs[] = $key;
break;
case 'pix':
printf("<td><input type='file' name='%s' id='%s' data-preview='%sPreview' accept='image/*' />\n",
$key, $key, $key
);
if (isset($formData[$key])) {
$pix = $formData[$key];
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "key: $key, " . $pix);
$fqfn = ($pix != '') ? $pathToArtifacts . '/' . $pix : '';
if (!file_exists($fqfn)) {
$pix = $fqfn = '';
}
} else {
$pix = $fqfn = '';
}
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "pathToArtifacts: $pathToArtifacts, fqfn: $fqfn");
printf("<div class='image-container'><img id='%sPreview' src='%s' alt='Preview' style='display:%s; ' /></div>\n",
$key, $fqfn, ($pix == '') ? 'none' : 'block');
if ($fqfn != '') {
printf("<button type='button' onclick='DeletePix(\"%s\",\"%s\");' >Delete pix</button>",
$thisItem, $key);
}
//printf("</span>\n");
printf("</td>\n");
break;
case 'chair':
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "pathToArtifacts: $pathToArtifacts");
//TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($formFields, true));
$map = GetDocMap($ProgChairPath, $ProgChairFormFields, 'ProgramChair');
TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($map, true));
TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($formData, true));
printf("<td><select name='%s' id='%s'>", $key, $key);
printf("\t<option '%s'>%s</option>\n", '', '');
foreach ($map as $key => $lkupValue) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $formData[$key] . ", $key => $lkupValue");
printf("\t<option '%s' %s>%s</option>\n", $key,
($formData[$key] == $lkupValue) ? 'selected' : '',
$lkupValue);
}
printf("</select>\n");
printf("</td>");
break;
case 'place':
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "pathToArtifacts: $pathToArtifacts, 'placeName'");
//TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($formFields, true));
$map = GetDocMap($PlacesPath, $PlaceFormFields, 'PlaceName');
TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($map, true));
TraceProgram(__FILE__, __FUNCTION__, __LINE__, print_r($formData, true));
printf("<td><select name='%s' id='%s'>", $key, $key);
printf("\t<option '%s'>%s</option>\n", '', '');
foreach ($map as $key => $lkupValue) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $formData[$key] . ", $key => $lkupValue");
printf("\t<option '%s' %s>%s</option>\n", $key,
($formData[$key] == $lkupValue) ? 'selected' : '',
$lkupValue);
}
printf("</select>\n");
printf("</td>");
break;
default:
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "unexpected path from '" . $data['FieldType'] . "'");
break;
}
//printf("\t<td>%s</td>\n", $data['FieldType']);
printf("</tr>\n");
}
?>
<tr><td colspan='2'><hr></td></tr>
<tr>
<td>
</td>
<td>
<input type='submit' />&nbsp;&nbsp;&nbsp;<input type='reset' />
</td>
</tr>
</table>
<!-- End of Form Table -->
<script>
// Store editor instances so we can access their data later
const editors = {};
<?php
$template1 = <<<EOM
// Initialize editor
ClassicEditor
.create(document.querySelector('#%s'), {
ckfinder: {
uploadUrl: 'Upload.php'
}
})
.then(editor => {
editors.%s = editor;
})
.catch(error => {
console.error('Error initializing %s:', error);
});
EOM;
foreach ($CK_EditorFieldIDs as $editField) {
printf($template1, $editField, $editField, $editField);
}
?>
</script>
<?php
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
return $CK_EditorFieldIDs;
}
function GetDocMap($path, $formFields, $tagOfInterest) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "$path", TR_ENTRY);
$map = array(); // <folder> => <place name>
$listOfItems = array();
$folderList = glob("$path/*");
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "\n$path: " . print_r($folderList, true));
foreach ($folderList as $folder) {
$listOfItems[] = basename($folder);
}
foreach ($listOfItems as $place) {
//printf("[%s][%s]<br/>\n", $path, $place);
$fqPath = sprintf("%s/%s", $path, $place);
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $fqPath);
$success = ReadTableData($fqPath, $formFields, $tag);
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $success);
$name = $tag[$tagOfInterest] ?? 'untitled';
$map[$place] = $name;
}
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
return $map;
}
function DateToMDY($inDate) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "[$inDate]", TR_ENTRY);
if ($inDate != '') {
$parts = explode("-", $inDate);
$mmddyyyy = sprintf("%s/%s/%s", $parts[1], $parts[2], $parts[0]);
} else {
$mmddyyyy = $inDate;
}
TraceProgram(__FILE__, __FUNCTION__, __LINE__, $mmddyyyy, TR_EXIT);
return $mmddyyyy;
}
function TimeTo12HR($t) {
$AMPM = '';
$parts = explode(':', $t);
if ($parts[0] == '00') {
$parts[0] = '12';
$AMPM = 'AM';
} else if ($parts[0] > 12) {
$parts[0] -= 12;
$AMPM = 'PM';
}
return sprintf("%s %s", implode(':', $parts), $AMPM);
}
function EmitPageHeader($title, $subtitle = '') {
$sub = ($subtitle != '') ? "<br/>$subtitle" : "";
echo "
<table width='100%' border='" . TBL_BORDER . "' class='ThemeColor'>
<tr>
<td width='10' rowspan='2'>
<a href='index.php'><img src='inc/SiteLogo.png' height='80px' alt='logo' /></a>
</td>
<td nowrap='nowrap' class='Center' >
<span class='PageTitle' >$title</span>
<span class='HeadingTitle Center'>$sub</span>
</td>
<td width='10' nowrap='nowrap' align='right'>
<a href='#' onclick='SelectTheme(\"dark\");'><img src='inc/BonW.png' title='Select and save dark theme in a cookie' ></a>
<a href='#' onclick='SelectTheme(\"lite\");'><img src='inc/WonB.png' title='Select and save light theme in a cookie' ></a>
<br/>
<a href='Admin.php'>Go Admin</a>
</td>
</tr>
</table>
";
}
function EmitHTMLStart($title) {
global $ckEditor_FQFN;
?>
<!doctype html>
<html>
<head>
<title>Admin Center</title>
<!-- Choose the default theme; dark or lite -->
<link rel='stylesheet' href='inc/theme-dark.css' />
<!-- link rel='stylesheet' href='inc/lite-theme.css' / -->
<link rel='icon' type='image/png' href='inc/SiteLogo.png' />
<script type='text/javascript' src='inc/theme.js'></script>
<?php printf("<script src=\"%s\"></script>\n", $ckEditor_FQFN); ?>
</head>
<?php
}
function EmitHTMLEnd() {
echo "</html>\n";
}
function EmitBodyStart() {
echo "<body>\n";
}
function EmitBodyEnd() {
?>
<script>
// Load the users cookie-saved theme
var theme = getCookie("theme");
if (theme != "") {
reloadCSS(theme);
}
</script>
<?php echo GeneratePageFooter(); ?>
</body>
<?php
}
function GeneratePageFooter($extraHTML = "", $helpTag = false) {
global $AppName;
$html = $extraHTML;
$html .= "<div class='PageFooter'>\n";
$html .= GenerateTransactionInfo();
if ($helpTag)
$html .= " <a href='mailto:" . CONTACT . "?Subject=$AppName'>Need help?</a>\n";
$html .= "</div>\n";
return $html;
}
function GenerateTransactionInfo($webIF = true) {
global $time_start, $AppName, $link;
// Show the CPU time for the transaction
$time_end = microtime(true);
$time_elapsed = sprintf("%3.3f", $time_end - $time_start);
return sprintf("<font size='-2'>%s App v%s, transaction completed in %s seconds.</font>",
$AppName, AppVersion, $time_elapsed);
}
function EmitEvent($event) {
global $EventsPath, $EventFormFields;
global $PlacesPath, $PlaceFormFields;
$EventPath = $EventsPath .'/'. $event . '/';
$success = ReadTableData($EventPath, $EventFormFields, $evData);
$evData['ProgramDate'] = $event;
printf("<table border='%d' width='100%%'>\n", TBL_BORDER);
$prev = GetPrevEvent($event);
$next = GetNextEvent($event);
if ($prev != '') {
$linkPrev = "<a href='?Action=EventView&ProgramDate=$prev'>&lt;==</a>";
} else {
$linkPrev = "&lt;==";
}
if ($next != '') {
$linkNext = "<a href='?Action=EventView&ProgramDate=$next'>==&gt;</a>";
} else {
$linkNext = "==&gt;";
}
printf("<tr><td align='center'>%s&nbsp;&nbsp;Navigate&nbsp;&nbsp;%s</td><td><hr></td></tr>\n", $linkPrev, $linkNext);
foreach ($EventFormFields as $key => $data) {
if ($data['bg']) {
printf("<tr><td colspan='2'><hr></td></tr>\n");
}
printf("<tr>\n");
printf("\t<td valign='top' width='10%%' nowrap><b><label for='%s'>%s</label></b></td>\n",
$key, $data['Label']);
$value = $evData[$key] ?? $EventFormFields[$key]['Default'];
switch ($data['FieldType']) {
case 'text':
printf("<td>%s</td>\n", $value);
break;
case 'date':
printf("<td>%s</td>\n", DateToMDY($value));
break;
case 'time':
printf("<td>%s</td>\n", TimeTo12HR($value));
break;
case 'bool':
printf("<td><input type='checkbox' name='%s' value='1' %s disabled /></td>\n",
$key, ($value) ? "checked" : "");
break;
case 'html':
printf("<td>%s</td>\n", $value);
break;
case 'pix':
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "$key, $value");
$pix = $value;
$fqfn = ($pix != '') ? $EventPath . '/' . $pix : '';
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "$pix, $fqfn, $EventPath");
if (file_exists($fqfn)) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "EventPath: $EventPath, fqfn: $fqfn");
printf("<td><div class='image-container'><img id='%sPreview' src='%s' alt='Preview' style='display:block;' /></div>\n",
$key, $fqfn);
printf("</td>\n");
}
break;
case 'chair':
printf("<td>%s</td>\n", $value);
break;
case 'place':
$refPlaceName = $evData[$key] ?? '';
printf("<td>\n");
//printf("%s<br/>", $refPlaceName);
$placeHash = GetDocMap($PlacesPath, $PlaceFormFields, 'PlaceName');
//printf("<pre>%s</pre>", print_r($placeHash, true));
foreach ($placeHash as $folder => $placeName) {
// TraceProgram(__FILE__, __FUNCTION__, __LINE__, $formData[$key] . ", $folder => $placeName");
if ($placeName == $refPlaceName) {
//printf("%s, %s<br/>", $placeName, $folder);
EmitPlace($folder);
}
}
printf("</td>\n");
break;
default:
break;
}
printf("</tr>\n");
}
?>
</table>
<!-- End of Presentation Table -->
<?php
}
function EmitPlace($place) {
global $PlacesPath, $PlaceFormFields;
$fqPath = sprintf("%s/%s", $PlacesPath, $place);
$success = ReadTableData($fqPath, $PlaceFormFields, $evData);
if ($success) {
//printf("evData: <pre>%s</pre>\n", print_r($evData, true));
printf("%s<br/>%s<br/>\n", $evData['PlaceName'], $evData['PlaceAddress']);
$imgFQFN = sprintf("%s/%s/%s", $PlacesPath, $place, $evData['PlacePix']);
printf("<img src='%s' width='600' />\n", $imgFQFN);
}
}
/// EmitAdminFormType1
///
/// This is in the Editor_Event.php at the top of the file.
///
/// This is the admin event manager form
/// New item: [date picker] Pick Place: [Pick list] Pick Chair: [Pick list]
/// Edit item: [Pick list]
/// [Submit] [Reset]
/// EmitAdminFormType2
///
/// Emit a small form for the Admin page
/// New item: [ free form text ]
/// Edit item: [ selection list ]
/// [Submit] [Reset]
/// Validate that the form is not empty in order to accept it.
///
function EmitAdminFormType2($args) {
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_ENTRY);
?>
<blockquote>
<form method='post' id='<?php echo $args['FormID']; ?>' onsubmit='return validateMyForm(event,this);' >
<input type='hidden' name='Action' value='<?php echo $args['ActionValue']; ?>' />
<table border='<?php echo TBL_BORDER; ?>' width='100%'>
<tr>
<td width='20%' nowrap><?php echo $args['Field1Label']; ?></td>
<td><input type='text' name='<?php echo $args['Input1Name']; ?>' id='inp1' value='' size='50' /></td>
</tr>
<tr>
<td><?php echo $args['Field2Label']; ?></td>
<td><select name='<?php echo $args['Select1Name']; ?>' id='sel1' >
<?php printf("\t\t\t\t<option value='%s'>%s</option>\n", '', ''); ?>
<?php
$map = GetDocMap($args['DocPath'], $args['FormFields'], $args['dbKey']);
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "\nmap: " . print_r($map, true));
foreach ($map as $key => $value) {
printf("\t\t\t\t<option value='%s'>%s</option>\n", $key, $value);
}
?> </select>
</td>
</tr>
<tr>
<td></td>
<td><input type='submit' />&nbsp;&nbsp;&nbsp;<input type='reset' /></td>
</tr>
</table>
</form>
<script>
function validateMyForm(event, f) {
var i1 = f.querySelector('#inp1');
var s1 = f.querySelector('#sel1');
if (i1.value.trim() === '' && s1.selectedIndex === 0) {
event.preventDefault();
alert("You need to enter text for a new item, or pick from the list to edit an existing item.");
i1.focus();
return false; // Form not valid - reject the submit
}
return true;
}
</script>
</blockquote>
<?php
TraceProgram(__FILE__, __FUNCTION__, __LINE__, "", TR_EXIT);
}
?>