<?php
ini_set("display_startup_errors","1");
ini_set("display_errors","1");
error_reporting(E_ALL);


require ("mysqlconnect.inc.php");
include "header.php";
$JSINCLUDE = "softmanager.js";

/******* Init the interface  ******/
session_start(); //web side
if (isset($_REQUEST['app']))
	$app = $_REQUEST['app'];
else $app = "";
if (isset($_REQUEST['comm']))
	$comm = $_REQUEST['comm'];
else $comm = "";
if (isset($_REQUEST['para']))
	$para = $_REQUEST['para'];
else $para = "";
if (isset($_REQUEST['input']))
	$input = $_REQUEST['input'];
else $input = "";
if (isset($_REQUEST['output']))
	$output = $_REQUEST['output'];
else $output = "";

if((isset($_POST['add']))) {
$sta = addApp($app, $comm, $para, $input, $output);
} elseif ((isset($_POST['rem']))){
$sta = remApp($app);
} elseif ((isset($_POST['mod']))) {
$sta = modApp($app, $comm, $para, $input, $output);
}

?>


<html>
<head>
<script type="text/javascript" src="<? echo $JSINCLUDE;?>">
</script>
<title> useradmin page</title>
<style type="text/css">@import url(../styles/layout.css);</style>
</head>
<body>
<div id="content">
<br><br><br>
<form name="update" enctype="multipart/form-data" method='post' action="<?$_SERVER['PHP_SELF']?>">
<table>
<tr>
<td>ID:</td><td><input type="span" size="12" maxlength="22" id="id" name="id"></td></tr><tr>
<td>Application Name:</td><td><input type="span" size="19" maxlength="12" id="app" name="app"></td></tr><tr>
<td>Command to execute:</td><td><input type="text" size="32" maxlength="500" id="comm" name="comm"></td></tr><tr>
<td>Parameters:</td><td><input type="text" size="12" maxlength="100" id="para" name="para"></td></tr><tr>
<td>Input file:</td><td><input type="text" size="12" maxlength="22" id="input" name="input"></td></tr><tr>
<td>Output file:</td><td><input type="text" size="12" maxlength="22" id="output" name="output"></td>
</tr>
</table>

<table>
<tr>
<td colspan=2 align=center><input type="submit" value="add" name="add"></td>
<td colspan=1 align=center><input type="submit" value="rem" name="rem"></td>
<td colspan=2 align=center><input type="submit" value="mod" name="mod"></td>
<td colspan=2 align=center><input type="submit" value="reset" name="reset"></td>
</tr>
</table>
</form>




<?
	$dbconnection = mysql_connect($dbhost,$dbuser,$dbpass) or die ("Could not connect!" . mysql_error());
	mysql_select_db($database);	

if (!isset($id)){
echo "";	
} else {



}

?>

<table border=1>
<tr>
<th>&nbsp</th>
<th>Id:</th>
<th>Application:</th>
<th>Command:</th>
<th>Parameters:</th>
<th>Input File:</th>
<th>Output File:</th>
</tr>

<?

/* SQL statement to query the database */
$commandtable = $GLOBALS['commandtable'];
$query = "SELECT * FROM $commandtable"; 

/* query the database */
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)){
print("
<tr>
<td><INPUT TYPE=\"radio\" NAME=\"update\" VALUE=\"".$row["softname"]."\" onClick=\"if (this.checked) updateSoftInfo('".$row["softname"]."');\"></td>
<td>".$row["id"]."</td>
<td>".$row["softname"]."</td>
<td>".$row["command"]."</td>
<td>".$row["parameters"]."</td>
<td>".$row["input_file"]."</td>
<td>".$row["output_file"]."</td>
</tr>
");

}

?>
</table>
</div>
</body>
</html>
<?

function addApp($softname, $command, $parameters, $input_file, $file_output)
{
if($softname) {
	$fields = extractFields($GLOBALS['COMMAND_TABLE_FIELDS']);
	$fields = parseinitfields($GLOBALS['COMMAND_TABLE_FIELDS']);
	$commandtable = $GLOBALS['commandtable'];
        $query = "INSERT INTO $commandtable (".$fields.") VALUES ('$softname', '$command','$parameters','$input_file','$file_output')";
        $result = mysql_query($query);
        if($result) {
                return $result;
        } else {
                return false;
        }
        }
}

function modApp($softname, $command, $parameters, $input_file, $file_output)
{
if($softname) {
        remApp($softname);
	$commandtable = $GLOBALS['commandtable'];
	$fields = parseinitfields($GLOBALS['COMMAND_TABLE_FIELDS']);
        $query = "REPLACE INTO $commandtable (".$fields.") VALUES ('$softname', '$command','$parameters','$input_file','$file_output')";
        $result = mysql_query($query);

        if($result) {
                return $result;
        } else {
                return false;
        }
        }
}
/* */
function remApp($softname)
{
	$commandtable = $GLOBALS['commandtable'];
        $query = "DELETE FROM $commandtable WHERE softname = '$softname'";
	//print "DELETE FROM $commandtable WHERE softname = '$softname'";
        if($result = mysql_query($query)) {
                return $result;
        } else {
                return false;
        }
}

?>
