<?

	#---------------------------------------------------------------------#
	# This source code is (c)2000 White Pajamas, Inc                      #
	# see http://www.stonekeep.com/license.html for licensing information #
	#---------------------------------------------------------------------#

	#----------------------------------------------------------------------
	# $Id: runreports.php3,v 1.15 2000/12/14 19:13:59 shevett Exp $
	#----------------------------------------------------------------------

	include "./conf/keystone.conf.default";
	if (file_exists("./conf/keystone.conf")) {
		include "./conf/keystone.conf";
	}
	include "./db.php3";
	$result = db_connect("");
	include "./utils.php3";
	include "./reputils.php3";
	
	$repinfo = ParseReport("./reports/$repname");	
	
	# Run any PHP header code we might have
	LogIt("DEBUG","runreports","PHPSTART is $repinfo[phpstart]");
	LogIt("DEBUG","runreports","PHPHEAD is $repinfo[phphead]");
	LogIt("DEBUG","runreports","PHPITERATE is $repinfo[phpiterate]");

	if ("$repinfo[phpstart]") {
		LogIt("DEBUG","runreports","running phpstart...");
		eval("$repinfo[phpstart];");
	}

?>


<table width=100% border=0>
<tr valign=top>
	<td width=15%>
		<?
			$now = time();
#			echo date("m/d/y",$now) . "<br>";
#			echo date("H:i",$now);
			echo date($sys_shortdatefmt,$now) . "<br>";
			echo date($sys_timefmt,$now);
		?>
	</td>
	<td align=center>
		<h3><? echo $repinfo[title] ?></h3><br>
	</td>
	<td  width=15% align=right>
		Keystone <? echo $sys_version ?><br>
		<? echo $repname ?> <? echo $repinfo[version] ?>
	</td>
</tr>
</table>
<hr>
<?
	if ("$repinfo[phphead]") {
		LogIt("DEBUG","runreports","running phphead...");
		eval("$repinfo[phphead];");
	} else {
		echo "<table border=1>";
		echo "<tr>";
			for ($i = 1; $i <= $repinfo[ccounter]; $i++) {
				echo "<th>" . $repinfo[label][$i] . "</td>\n";
			}
		echo "</tr>";
	}
		for ($i = 1; $i <= $repinfo[vcounter]; $i++) {
			$varname = "VAR$i";
			eval("\$tmp = \"\$$varname\";");
			if (eregi('date|time', $repinfo[vdtype][$i], $junk)) {
				LogIt("DEBUG","runreports","its a date or a time.");

				# Deduce the date and time based on what was spun out
				# from the PromptDate thingy.

				eval ("\$hr=\"\$${varname}_hour\";");
				eval ("\$mi=\"\$${varname}_minute\";");
				eval ("\$year=\"\$${varname}_year\";");
				eval ("\$month=\"\$${varname}_month\";");
				eval ("\$day=\"\$${varname}_day\";");
				$tmp = InterpretDate($mi,$hr,$day,$month,$year);
				LogIt("DEBUG","runreports","Value for $varname is $tmp, which is ".date('m/d/y H:i',$tmp));
			}
			# Okay - now substitute that number into the various commands, SQL and PHP...
			$repinfo[sql]				=	ereg_replace("VAR$i","$tmp", $repinfo[sql]);
			$repinfo[phpiterate]=	ereg_replace("VAR$i","$tmp", $repinfo[phpiterate]);
			$repinfo[phphead]		=	ereg_replace("VAR$i","$tmp", $repinfo[phphead]);
			$repinfo[phpstart]	=	ereg_replace("VAR$i","$tmp", $repinfo[phpstart]);
			$repinfo[phpbody]		=	ereg_replace("VAR$i","$tmp", $repinfo[phpbody]);
			$repinfo[phpfoot]		=	ereg_replace("VAR$i","$tmp", $repinfo[phpfoot]);
			$repinfo[phpend]		=	ereg_replace("VAR$i","$tmp", $repinfo[phpend]);
 			LogIt("DEBUG","runreports","SQL is now $repinfo[sql]");
		}

		if ("$repinfo[sql]") {
			$dbh = db_query("$repinfo[sql]");
		}
		if ("$repinfo[phpiterate]") {
			$itcmd = $repinfo[phpiterate];
		} else {
			$itcmd = "\$dbd = db_fetch_array(\$dbh)";
		}
		LogIt("DEBUG","runreports","iteration command is $itcmd");
		while (1) {
			eval("\$result = ($itcmd);");
			if (! "$result") {
				break;
			}
			if ("$repinfo[phpbody]") {
				eval("$repinfo[phpbody]");
				continue;
			} 
			echo "<tr>\n";
			for ($i = 1; $i <= $repinfo[ccounter]; $i++) {
				echo "<td>";
				$cname = $repinfo[display][$i];
				if ($sys_server == "postgresql" && ereg("count(.*)", $cname, $junk)) {
					$cname = "count";
				}
				if ($repinfo[cdtype][$i] == "date") {
					echo date($sys_shortdatefmt, $dbd[$cname]);
				} else if ($repinfo[cdtype][$i] == "time") {
					echo date($sys_timefmt, $dbd[$cname]);
				} else if ($repinfo[cdtype][$i] == "datetime") {
					echo date($sys_datefmt, $dbd[$cname]);
				} else {
					echo htmlspecialchars(stripslashes($dbd[$cname]));
				}
				if ($dbd[$cname] == "") {
					echo "&nbsp;";
				}
				echo "</td>\n";
			}
			echo "</tr>\n";
		}
	?>

		
</table>

