<?php
/*
$Id: save_report.php 579 2014-05-17 21:23:44Z bhockney $
(C) 2003-2014 by Bob Hockney <zeus@ix.netcom.com>

confirm save of report definitions

This code is distributed under the terms of GNU GPL
*/
?>

<?php
$link = db("open", "$database");

if (strtolower($FWLOG["action"]) == "commit") {
	$ORIG_FWLOG = $FWLOG;
	if (db("exists", "SELECT count(*) FROM $reports", $link)) {
		$name = trim($FWLOG["save_report"]);
		if (preg_match("/^[a-zA-Z0-9_]+\$/", "$name") or $name == "") {
			$report = $name;
			//if committing, UPDATE if report code already exists, otherwise INSERT
			unset ($FWLOG["action"]); // don't save these
			unset ($FWLOG["page"]);
			unset ($FWLOG['report_order']);
			unset ($FWLOG['webfwlog_sessid']);
			unset ($FWLOG["report"]);
			unset ($FWLOG["save_report"]);
			unset ($FWLOG["select_report"]);
			unset ($FWLOG["show_select_data_source"]);
			unset ($FWLOG["data_tables"]);
			unset ($FWLOG["ulog_tables"]); // deprecated
			unset ($FWLOG["ulog_table"]); // deprecated
			unset ($FWLOG["orig_report"]); // removed
			unset ($FWLOG["update_all"]); // removed
			unset ($FWLOG["show_sql"]); // deprecated, use be_verbose
			$definition = db("escape", serialize($FWLOG), $link);
			$rownum = 0;
			if (db("nextrow", db("statement", "SELECT code FROM $reports WHERE code ='".db("escape", "$report", $link)."'", $link))) {
				if (db("exists", "SELECT last_saved FROM $reports", $link)) {
					$l_saved = ", last_saved = $time ";
				} else {
					$l_saved = "";
				}
				if (isset($FWLOG["description"])) {
					$description = db("escape", "{$FWLOG["description"]}", $link);
				} else {
					$description = "";
				}
				if (!db("statement", "UPDATE $reports 
					SET description = '$description',
						definition  = '$definition'
						$l_saved
					WHERE code='".db("escape", "$report", $link)."'", $link)
					)
					$status = "\tERROR: REPORT NOT SAVED\n";
			} else {
				if (db("statement", "INSERT INTO $reports
					(code, description, definition) 
					VALUES ('".db("escape", "$report", $link)."','"
					.db("escape", "{$FWLOG["description"]}", $link)."', '$definition')", $link)) {
					if (db("exists", "SELECT last_saved, last_accessed FROM $reports", $link)) {
						db("statement", "UPDATE $reports 
							SET last_saved = $time,
								last_accessed = $time
							WHERE code='".db("escape", "$report", $link)."'", $link);
					}
				} else {
					$status = "\tERROR: REPORT NOT SAVED\n";
				}
			}
			$FWLOG = $ORIG_FWLOG;
			$FWLOG["report"] = $report;
			unset ($name); 
			$status = "\tREPORT {$FWLOG["report"]} SAVED\n";
			db("close", $link);
			include "include/edit_report.php";
			exit();
		}
	} else {
		$status = "<font color='red'>CANNOT OPEN DATATBASE<br>REPORT NOT SAVED</font>";
		$FWLOG["report"] = $FWLOG["save_report"];
	} // End of "save"
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
		"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Webfwlog - Save Report</title>
  <LINK REL=StyleSheet HREF="style.css" TYPE="text/css" MEDIA=screen>
</head>

<body bgcolor=#FFFFFF>
	<div align="center"><h1><?php print "{$config["title"]}"; ?></h1></div>
	<?php if ($config["debug"]) include "include/debug.php"; ?>

<form action="<?php print "$self"; ?>" method="POST">

<?php
	if (isset($FWLOG["save_report"])) {
		$code = $FWLOG["save_report"];
	} else {
		$code = $FWLOG["report"];
	}
	$rownum = 0;
	if (db("exists", "SELECT count(*) FROM $reports", $link) and
		$result = db("statement", "SELECT code FROM $reports WHERE code='".db("escape", "$code", $link)."'", $link) and
		db("nextrow", $result))
			$savecode = $code;

	db("close", $link);

	foreach ($FWLOG as $a => $b) {
			if (!($a == "page" or $a == "data_tables"))
			print "<input type='hidden' name=\"".htmlspecialchars("$a", ENT_QUOTES)
				."\" value=\"".htmlspecialchars("$b", ENT_QUOTES)."\">\n";
	}
?>
<input type='hidden' name='page' value='save'>

<div style="margin-top:5em; " align="center">
<font size="+2">
<table border=1>
  <caption>
     SAVE REPORT DEFINITION<p>
	 <?php if (isset($status)) print "$status"; ?>
  </caption>
    <tr>
    	<td colspan="2">
			<font size="+2">
			<strong>
			<?php if (isset($savecode)) {
				 print "\t<font color='red'>$code exists:<br>Committing will overwrite existing definition!</font>\n";
			} else {
				print "\t<font color='blue'>PLEASE ENTER A REPORT CODE<br>\n" .
					  "\tMust Contain only Alphanumeric + '_' : [a-zA-Z0-9_]+</font>\n";
			}
			?>
			</strong>
			</font>
		</td>	
	</tr>
	<tr>
		<td style="width:8em; text-align: left; ">Description:</td>
		<td style="width:15em; text-align: left; " colspan=2><?php if (isset($FWLOG["description"])) print htmlspecialchars("{$FWLOG["description"]}", ENT_QUOTES); ?></td>      
    </tr>
    <tr>
      <td style="text-align: left; ">Report Code:</td>
      <td style="text-align: left; "><input type="text" name="save_report" size="20" value="<?php print htmlspecialchars("$code", ENT_QUOTES); ?>"></td>
    </tr>
    <tr>
    	<td colspan="2">
			<input style="width: 16ex; " type="submit" name="action" value="Refresh">&nbsp;&nbsp;
			<input style="width: 16ex; " type="submit" name="action" value="Commit">&nbsp;&nbsp;
			<input style="width: 16ex; " type="submit" name="save" value="Cancel">
		</td>
	</tr>
</table>
</font>
</div>

</form>

</body>
</html>

<!-- vim:set ts=4: -->
