Location: PHPKode > projects > ARTIS WaterMon > artis-watermon-beta/options.php
<?php
	require_once("functions.php");
	checkSecurity();
?>

<html>
	<head><title>ARTIS - Options</title></head>
	<body><p>
	<?php

	// Display Items
	// 'Type' Select box
	function drawTypeSelectBox($type="Pump") {
		$type = strtolower($type);
		echo "<select name='type' id='type'>\n";
		echo "<option";	if($type=="reservoir") { echo " SELECTED"; } echo ">Reservoir</option>";
//		echo "<option"; if($type=="equation reservoir") { echo " SELECTED"; } echo ">Equation Reservoir</option>";
		echo "<option";	if($type=="rain gauge") { echo " SELECTED";} echo ">Rain Gauge</option>";
		echo "<option";	if($type=="flow meter") { echo " SELECTED"; } echo ">Flow Meter</option>";
		echo "<option";	if($type=="pump") { echo " SELECTED"; } echo ">Pump</option>";
		echo "<option"; if($type=="line") { echo " SELECTED"; } echo ">Line</option>";		
		echo "</select>";
	}
	
	function drawUserListSelectBox($owner=0) {
		global $dbuser,$dbpassword,$dbname;
		mysql_connect('localhost', $dbuser, $dbpassword) or die("<p>MySQL Connect failed: ".mysql_error()."</p>");
		mysql_select_db($dbname) or die("<p>MySql Select DB failed: ".mysql_error()."</p>");
		$query = "SELECT * FROM users";
		$result = mysql_query($query) or die("<p>Query Failed: " .mysql_error()."</p><p><pre>$query</pre></p>");
		echo "<select name='user' id='user'>";
		if(mysql_num_rows($result) > 0) {

			while($row = mysql_fetch_array($result)) {
				echo "<option value='" . $row['id'] . "'";
				if($owner == $row['id']) { echo " SELECTED "; }
				echo ">" . $row['friendly_name'] . "</option>";
			}
		}
		echo "</select>";		
	}
	
	// Default Miniscreen
	function displayDefaultNavigation() {
	?>
			ARTIS Options: [ <a href="options.php?action=add">Add Item</a> ] [ <a href="options.php?action=addpage">Add Page</a> ] [ <a href="options.php?action=manage" target="Main_Document">Manage</a> ] [ <a href="main.php" target="Main_Document">Refresh</a> ] (C) 2004/2005 Sam Moffatt/Toowoomba City Council ( BETA )
	<?php
	}
	
	function addpage() {
		?><p><form method="post" action="options.php?action=doaddpage">Add Page: Short Name: <input type="text" name="username"> Long (Friendly) Name: <input type="text" name="friendly_name"><input type="submit" value="Add"> <a href="options.php">Cancel</a></form></p><?php
	}
	
	function doAddPage() {
		global $dbuser,$dbpassword,$dbname;
		if(isset($_REQUEST['username'])) {
			$query = "INSERT INTO users VALUES ('','{$_REQUEST['username']}','{$_REQUEST['friendly_name']}',0)";
			$database = mysql_connect('localhost',$dbuser,$dbpassword) or die("<p>{$_REQUEST['friendly_name']} not added (DB Connect Failed): ".mysql_error().". <a href='options.php'>Return</a></p>");
			mysql_select_db($dbname,$database) or die("<p>{$_REQUEST['friendly_name']} not added  (DB Select Failed): ".mysql_error().". <a href='options.php'>Return</a></p>");
			mysql_query($query) or die("<p>{$_REQUEST['friendly_name']} not added (Query Failed): ".mysql_error().". <a href='options.php'>Return</a></p>");
			echo("<p>{$_REQUEST['friendly_name']} added. <a href='options.php'>Return</a></p>");
			mysql_close($database);
		}

	}
	
	
	// Management Panel (Main screen)
	function managementpanel() {
		global $dbuser,$dbpassword,$dbname;
		// Connect to database
		mysql_connect('localhost', $dbuser, $dbpassword) or die("<p>MySQL Connect failed: ".mysql_error()."</p>");
		mysql_select_db($dbname) or die("<p>MySql Select DB failed: ".mysql_error()."</p>");
//		$query = "SELECT * FROM display WHERE owner=0";
		$query = "SELECT * FROM users";
		$user_result = mysql_query($query) or die("<p>Query Failed: " .mysql_error()."</p><p><pre>$query</pre></p>");
		
		
		?>
		<h1>ARTIS Water Monitor Management Panel</h1><hr>
		<a href="main.php">Return to main screen</a><br>
		<?php
		while($user = mysql_fetch_array($user_result)) {
			$query = "SELECT * FROM display WHERE owner={$user['id']}";
			$item_result = mysql_query($query) or die("<p>Query Failed: " . mysql_error()."</p><p><pre>$query</pre></p></p>");
			echo "<b>".$user['friendly_name']."</b> [ <a href='options.php?action=deletepage&id={$user['id']}'>Delete Page</a> ]";
			if(!mysql_num_rows($item_result) > 0) {
				echo "<p>No items.</p>";
			} else {
			?>
		<table>
			<tr><th>ID</th><th>Type</th><th>Name</th><th>Left</th><th>Top</th><th>Options</th></tr>
			<?php
				while($row = mysql_fetch_array($item_result)) {
					echo "<tr>";
					echo "<td>{$row['itemid']}</td>";
					echo "<td>{$row['type']}</td>";
					echo "<td>{$row['shortname']}</td>";
					echo "<td>{$row['left']}</td>";
					echo "<td>{$row['top']}</td>";
					echo "<td>[ <a href='options.php?action=edit&id={$row['id']}'>Edit</a> ] [ <a href='options.php?action=delete&id={$row['id']}'>Delete</a> ]</td>";
					echo "</tr>\n";
				}					
			?>
		</table>
		<?php
			}
		}
		mysql_close();
		// We'll have to do the other users...when it gets implemented :)
	}
	
	function userPanel() {
		echo "<p>";
//		echo "<form action=\"options.php?action=changeview\" method=\"post\">"
		echo "Select Display Type:";
		drawUserListSelectBox(1);
//		echo "<input type=\"button\" value=\"View\" onclick=\"javascript:parent.Main_Document.location='main.php?owner='+user.SelectedIndex]; return true\"><a href=\"options.php\">Done</a>";
		echo "<input type=\"button\" value=\"View\" " 
			. "onclick=\"javascript:parent.Main_Document.location='main.php?owner='+user.SelectedIndex]; "
			. "return true\"><a href=\"options.php\">Done</a>";
//		echo "</form>";
		echo "</p>";
	}
	
	function editPanel() {
		global $dbuser,$dbpassword,$dbname;
		if($_REQUEST['id'] != 0) {
			mysql_connect('localhost', $dbuser, $dbpassword) or die("<p>MySQL Connect failed: ".mysql_error()."</p>");
			mysql_select_db($dbname) or die("<p>MySql Select DB failed: ".mysql_error()."</p>");
			$query = "SELECT * FROM display WHERE id={$_REQUEST['id']}";
			$result = mysql_query($query) or die("<p>Query Failed: " .mysql_error()."</p><p><pre>$query</pre></p>");
			$row = mysql_fetch_array($result);
			?>
			<h1><?php echo $row['shortname']; ?> Details</h1><hr>
			<form method="post" action="options.php?action=doedit">
			<input type="hidden" name="id"  value="<?php echo $_REQUEST['id']; ?>">
			<table>
				<tr><td>ID:</td><td><input type="text" name="itemid" size="5" value="<?php echo $row['itemid']; ?>"></td></tr>
				<tr><td>Name:</td><td><input type="text" name="shortname" value="<?php echo $row['shortname']; ?>"></td></tr>
				<tr><td>User:</TD><td><?php drawUserListSelectBox($row['owner']); ?></td></tr>
				<tr><td>Type</td><td><?php drawTypeSelectBox($row['type']); ?> </td></tr>
				<tr><td>Left:</td><td><input type="text" name="left" value="<?php echo $row['left']; ?>"></td></tr>
				<tr><td>Top:</td><td><input type="text" name="top" value="<?php echo $row['top']; ?>"></td></tr>
				<tr><td>Right:</td><td><input type="text" name="right" value="<?php echo $row['right']; ?>"></td></tr>
				<tr><td>Bottom:</td><td><input type="text" name="bottom" value="<?php echo $row['bottom']; ?>"></td></tr>
				<tr><td>Graph URL:</td><td><input type="text" name="graphurl" value="<?php echo $row['graphurl']; ?>"></td></tr>
				<tr><td>Error URL:</td><td><input type="text" name="errorurl" value="<?php echo $row['errorurl']; ?>"></td></tr>				
				<tr><td><input type="submit" value="Edit"></td><td><a href="options.php?action=manage">Cancel</a></td></tr>
			</table>
			</form>			
			<?php
			mysql_close();
		} else {
			echo "<p>Error: False ID!</p>";
		}
	}
	
	function deletepage() {
		global $dbuser,$dbpassword,$dbname;
		if(isset($_REQUEST['id'])) {
			$query = "DELETE FROM display WHERE owner = {$_REQUEST['id']}";
			mysql_connect('localhost', $dbuser, $dbpassword) or die("Connect Failed: " . mysql_error());
			mysql_select_db($dbname) or die("Select DB Failed: " . mysql_error());
			mysql_query($query) or die("Query failed: " . mysql_error()."<br>$query<br>");
			$query = "DELETE FROM users WHERE id = {$_REQUEST['id']}";
			mysql_query($query) or die("Query failed: " . mysql_error()."<br>$query<br>");
			echo "<P>Deleted.<a href='options.php?action=manage'>Return to management panel</a></P>";
		} else {
				echo "<P>FAILED! <a href='options.php?action=manage'>Return to management panel</a></P>";
		}
	}
	
	function deletepanel() {
		global $dbuser,$dbpassword,$dbname;
		if(isset($_REQUEST['id'])) {
			$query = "DELETE FROM display WHERE id = {$_REQUEST['id']}";
			mysql_connect('localhost', $dbuser, $dbpassword) or die("Connect Failed: " . mysql_error());
			mysql_select_db($dbname) or die("Select DB Failed: " . mysql_error());
			mysql_query($query) or die("Query failed: " . mysql_error()."<br>$query<br>");
			echo "<P>Deleted.<a href='options.php?action=manage'>Return to management panel</a></P>";
		} else {
				echo "<P>FAILED! <a href='options.php?action=manage'>Return to management panel</a></P>";
		}
	}
	
	// Work Items 
	// Add item (Miniscreen)
	function add() {
		?><p><form method="post" action="options.php?action=doadd">Add Item: Display: <?php echo drawUserListSelectBox(); ?> ID: <input type="text" name="itemid" size="5"> Name: <input type="text" name="shortname"> Type: <?php drawTypeSelectBox(); ?> Left: <input type="text" name="left" size="3"> Top: <input type="text" name="top" size="3"> Bottom: <input type="text" name="bottom" size="3" value="0">  Right: <input type="text" name="right" size="3" value="0"><input type="hidden" name="graphurl" value=""><input type="hidden" name="errorurl" value=""> <input type="hidden" name="equation" size="5" value=""><input type="submit" value="Add"> <a href="options.php">Cancel</a></form></p><?php
	}
	
	// Add an item
	function doadd() {
		global $dbuser,$dbpassword,$dbname;
		if(isset($_REQUEST['itemid'])) {
			$query = "INSERT INTO display VALUES ('','{$_REQUEST['itemid']}','{$_REQUEST['shortname']}','{$_REQUEST['type']}',{$_REQUEST['top']},{$_REQUEST['left']},{$_REQUEST['bottom']},{$_REQUEST['right']},'{$_REQUEST['equation']}','{$_REQUEST['graphurl']}','{$_REQUEST['errorurl']}',{$_REQUEST['user']})";
			$database = mysql_connect('localhost',$dbuser,$dbpassword) or die("<p>{$_REQUEST['type']} not added (DB Connect Failed): ".mysql_error().". <a href='options.php'>Return</a></p>");
			mysql_select_db($dbname,$database) or die("<p>{$_REQUEST['type']} not added  (DB Select Failed): ".mysql_error().". <a href='options.php'>Return</a></p>");
			mysql_query($query) or die("<p>{$_REQUEST['type']} not added (Query Failed): ".mysql_error().". <a href='options.php'>Return</a></p>");
			echo("<p>{$_REQUEST['type']} added. <a href='options.php'>Return</a></p>");
			mysql_close($database);
		}
	}
	
	function doEdit() {
		global $dbuser, $dbpassword, $dbname;
		$id = intval($_REQUEST['id']);
		$query = "UPDATE display SET ";
		$query .= buildUpdateQuery($_REQUEST,"itemid shortname type left top bottom right graphurl errorurl",1);
		$query .= " WHERE id = $id ";
		$database = mysql_connect('localhost', $dbuser, $dbpassword) or die("<p>{$_REQUEST['shortname']} {$_REQUEST['type']} not updated (DB Connect Failed): ".mysql_error().". <a href='options.php'>Return</a></p>");
		mysql_select_db($dbname, $database) or die("<p>{$_REQUEST['shortname']} {$_REQUEST['type']} not updated  (DB Select Failed): ".mysql_error().". <a href='options.php'>Return</a></p>");
		mysql_query($query) or die("<p>{$_REQUEST['shortname']} {$_REQUEST['type']} not updated (Query Failed): ".mysql_error().". <a href='options.php'>Return</a></p>");
		echo "{$_REQUEST['shortname']} {$_REQUEST['type']} updated. <a href='options.php?action=manage'>Manage Page</a> - <a href='main.php'>Main Page</a>";
	}
	
	function doChangeView() {
		global $dbuser, $dbpassword, $dbname;
		
	}
	
	// Task Selection
	if(isset($_REQUEST['action'])) {
		switch($_REQUEST['action']) {
			case "add": 
				add();
				break;
			case "doadd": 
				doadd();
				break;
			case "manage":
				managementpanel();
				break;
			case "edit":
				editpanel();
				break;
			case "doedit":
				doEdit();
				break;
			case "delete":
				deletepanel();
				break;
			case "deletepage":
				deletepage();
				break;
			case "changeuser":
				userpanel();
				break;
			case "changeview":
				doChangeView();
				break;
			case "addpage":
				addpage();
				break;
			case "doaddpage";
				doAddPage();
				break;
			default:
				displayDefaultNavigation();
				break;
		}
	} else {
		displayDefaultNavigation();
	}
?></p></body>
</html>
Return current item: ARTIS WaterMon