Profiles can be managed via the web interface

Sat, 26 Jul 2014 17:30:47 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 26 Jul 2014 17:30:47 +0200
changeset 137
e4518fd9b626
parent 136
264e5ee5abfc
child 138
e7e7a23af890

Profiles can be managed via the web interface

www-thermferm/css/style.css file | annotate | diff | comparison | revisions
www-thermferm/profiles.php file | annotate | diff | comparison | revisions
www-thermferm/utilities.php file | annotate | diff | comparison | revisions
--- a/www-thermferm/css/style.css	Sat Jul 26 13:18:00 2014 +0200
+++ b/www-thermferm/css/style.css	Sat Jul 26 17:30:47 2014 +0200
@@ -56,14 +56,23 @@
 }
 
 #errors {
-    height: 60px;
+    height: 35px;
     width: 978px;
     float: left;
+    text-align: center;
+    font-size: 1.5em;
+    margin-top: 10px;
     color: #ff0000;
 }
 
 #etable {
-    height: 360px;
+    height: 320px;
+    width: 978px;
+    float: left;
+}
+
+#atable {
+    height: 60px;
     width: 978px;
     float: left;
 }
@@ -86,6 +95,31 @@
     padding: 1px 3px 1px 3px;
 }
 
+/*
+ * Editor screens
+ */
+table.editor {
+    margin-left: auto;
+    margin-right: auto;
+    border-collapse: collapse;
+    background-color: #eee;
+}
+
+tr.editor {
+    height: 30px;
+}
+
+td.editname {
+    width: 150px;
+    padding: 1px 3px 1px 3px;
+}
+
+td.editfield {
+    width: 550px;
+    padding: 1px 3px 1px 3px;
+}
+
+
 #jqxTabs {
     width: 980px;
     height: 440px;
--- a/www-thermferm/profiles.php	Sat Jul 26 13:18:00 2014 +0200
+++ b/www-thermferm/profiles.php	Sat Jul 26 17:30:47 2014 +0200
@@ -23,35 +23,65 @@
 
 require_once('utilities.php');
 
-profile_admin();
+/*
+ * Each time this page is loaded, get the profiles from the server.
+ */
+$sock = open_socket();
+if ($sock == false) {
+    load('index.php');
+}
+
+socket_write($sock, "LIST PROFILES", 4096);
+$answer = "";
+while (1) {
+    $line = socket_read($sock, 4096);
+    if ($line === '')
+	break;
+    $answer .= $line;
+}
+socket_close($sock);
+/*
+ * $arr contains the complete reply of he LIST PROFILES command.
+ */
+$arr = explode("\r\n", $answer);
+
+
+if (isset($_GET['action'])) {
+    switch ($_GET['action']) {
+	case 'edit':		profile_edit();
+	    			break;
+	default:		break;
+    }
+} elseif (isset($_POST['action'])) {
+    switch ($_POST['action']) {
+	case 'testdata':	testdata();
+	    			break;
+	default:		break;
+    }
+} else {
+    profile_list();
+}
+
 exit;
 
 
-function profile_admin() {
-    if (isset($_GET['action'])) {
-	switch ($_GET['action']) {
-	    default:		break;
-	}
-    } elseif (isset($_POST['action'])) {
-	switch ($_POST['action']) {
-	    default:		break;
-	}
-    } else {
-	profile_list();
-    }
-}
 
 
-
+/*
+ * Profile add
+ *
+ * @param string $_POST['Name'] The rpofile name
+ */
 function profile_add() {
-    $cmd = "ADD PROFILE ".$_GET['Name'];
+
+    $cmd = "ADD PROFILE ".$_POST['Name'];
 
     $sock = open_socket();
     if ($sock != false) {
 	/*
 	 * Send command and absorb the result.
 	 */
-	socket_write($sock, $update_cmd, 4096);
+	socket_write($sock, $cmd, 4096);
 	while (1) {
 	    $line = socket_read($sock, 4096);
 	    if ($line === '')
@@ -59,15 +89,31 @@
 	}
 	socket_close($sock);
     }
+
+    unset($_POST['UUID']);
+    unset($_POST['Name']);
+    unset($_POST['key']);
+    unset($_POST['command']);
+    load('profiles.php');
 }
 
 
 
+/*
+ * Profile update
+ *
+ * @param string $_POST['UUID'] The profile UUID
+ * @param string $_POST['Name'] The profile name
+ * @param string $_POST['key'] The button pressed.
+ */
 function profile_update() {
     /*
      * Build the update command
      */
-    $update_cmd = "PROFILE ".$_GET['UUID'].",".$_GET['Name'];
+    if ($_POST['key'] == 'Delete')
+	$update_cmd = "DEL PROFILE ".$_POST['UUID'];
+    else
+        $update_cmd = "PROFILE ".$_POST['UUID'].",".$_POST['Name'];
 
     $sock = open_socket();
     if ($sock != false) {
@@ -82,6 +128,177 @@
     	}
     	socket_close($sock);
     }
+
+    unset($_POST['UUID']);
+    unset($_POST['Name']);
+    unset($_POST['key']);
+    unset($_POST['command']);
+    load('profiles.php');
+}
+
+
+
+/*
+ * Test input of a modified or new profile.
+ *
+ * @param string $_POST['UUID'] Unique record UUID
+ * @param string $_POST['Name'] Profile name
+ * @param string $_POST['key'] Key choice, Save or Cancel
+ * @param string $_POST['command'] Command used, 'add' or 'update'
+ *
+ * Return: 0 = Ok
+ *         1 = Missing data
+ *         2 = Name field too short
+ *         3 = Name already in use
+ *        99 = Cancel key
+ */
+function test_thedata() {
+
+    global $arr;
+
+    if (isset($_POST['UUID']) && isset($_POST['Name']) && isset($_POST['key']) && isset($_POST['command'])) {
+
+	if ($_POST['key'] == 'Cancel')
+	    return 99;
+
+	if (strlen($_POST['Name']) < 2)
+	    return 2;
+
+	if (startsWith($arr[0], "212")) {
+	    $j = 1;
+	    while (1) {
+		if (strcmp($arr[$j], ".") == 0)
+		    break;
+		$f = explode(",", $arr[$j]);
+		if (strcmp($f[0], $_POST['UUID']) && (strcmp($f[1], $_POST['Name']) == 0)) {
+		    return 3;
+		}
+		$j++;
+	    }
+	}
+
+    } else {
+	return 1;
+    }
+
+    return 0;
+}
+
+
+
+/*
+ * Test result from edit screen and do next action
+ */
+function testdata() {
+
+    $result = test_thedata();
+    $error = '';
+
+    switch ($result) {
+	case 0:	if ($_POST['command'] == 'add') {
+		    profile_add();
+		    return;
+	        } else if ($_POST['command'] == 'update') {
+		    profile_update();
+		    return;
+		}
+		break;
+	case 1:	$error = 'Missing data';
+		break;
+	case 2:	$error = 'The name is too short';
+		break;
+	case 3:	$error = 'The name is already in use, choose another one';
+		break;
+	case 99:	
+		load('profiles.php');
+		break;
+    }
+
+    if ($_POST['command'] == 'add') {
+	$heading = 'ThermFerm - Add Profile';
+    } else {
+	$heading = 'ThermFerm - Edit Profile';
+    }
+
+    edit_screen($_POST['UUID'], $_POST['Name'], $_POST['command'], $heading, $error);
+}
+
+
+
+/*
+ * Profiles edit screen. Used by profile_edit(), profile_add() and testdata()
+ *
+ * @param string $UUID The record UUID (fixed).
+ * @param string $Name The Profile Name.
+ * @param string $command 'add' or 'update'
+ * @param string $heading Pagina heading title.
+ * @Param string $error_message Blank or previous error.
+ */
+function edit_screen($UUID, $Name, $command, $heading, $error_message) {
+
+    $outstr  = build_header($heading);
+    $outstr .= '    <div id="errors">'.PHP_EOL;
+    $outstr .= '     '.$error_message.PHP_EOL;
+    $outstr .= '    </div> <!-- errors -->'.PHP_EOL;
+    $outstr .= '    <div id="etable">'.PHP_EOL;
+    $outstr .= '     <form method="POST" action="profiles.php">'.PHP_EOL;
+    $outstr .= '      <table class="editor">'.PHP_EOL;
+    $outstr .= '       <tr class="editor">'.PHP_EOL;
+    $outstr .= '        <td class="editname">UUID</td>'.PHP_EOL;
+    $outstr .= '        <td class="editfield">'.$UUID.'</td>'.PHP_EOL;
+    $outstr .= '       </tr>'.PHP_EOL;
+    $outstr .= '       <tr class="editor">'.PHP_EOL;
+    $outstr .= '        <td class="editname">Name</td>'.PHP_EOL;
+    $outstr .= '        <td class="editfield"><input type="text" name="Name" size="50" value="'.$Name.'"></td>'.PHP_EOL;
+    $outstr .= '       </tr>'.PHP_EOL;
+    $outstr .= '       <tr class="editor">'.PHP_EOL;
+    $outstr .= '        <td class="editname"><input type="submit" value="Save" name="key"></td>'.PHP_EOL;
+    $outstr .= '        <td class="editfield"><input type="submit" value="Cancel" name="key">';
+    $outstr .= '<input type="submit" value="Delete" name="key" style="margin-left: 100px;">';
+    $outstr .= '<input type="hidden" value="testdata" name="action">';
+    $outstr .= '<input type="hidden" value="'.$command.'" name="command">';
+    $outstr .= '<input type="hidden" value="'.$UUID.'" name="UUID"></td>'.PHP_EOL;
+    $outstr .= '       </tr>'.PHP_EOL;
+    $outstr .= '      </table>'.PHP_EOL;
+    $outstr .= '     </form>'.PHP_EOL;
+    $outstr .= '    </div> <!-- etable -->'.PHP_EOL;
+    $outstr .= build_footer();
+    echo $outstr;
+}
+
+
+
+/*
+ * Edit a Profile. Fetches the record data and shows the edit screen.
+ *
+ * @param string $_GET['action'] Must be 'edit'.
+ * @param string $_GET['UUID'] The UUID of the Profile.
+ */
+function profile_edit() {
+
+    global $arr;
+
+    if ($_GET['action'] == 'edit') {
+
+	if (startsWith($arr[0], "212")) {
+	    $j = 1;
+	    while (1) {
+		if (strcmp($arr[$j], ".") == 0)
+		    break;
+		$f = explode(",", $arr[$j]);
+		if (strcmp($f[0], $_GET['UUID']) == 0) {
+		    edit_screen($f[0], $f[1], 'update', 'ThermFerm - Edit Profile', '');
+		    return;
+		}
+		$j++;
+	    }
+	} else {
+	    load('profiles.php');
+	}
+
+    } else {
+	load('profiles.php');
+    }
 }
 
 
@@ -91,29 +308,13 @@
  * @link Add profile
  */
 function profile_list() {
-	
-    $sock = open_socket();
-    if ($sock == false) {
-    	echo "";
-    	return;
-    }
 
-    socket_write($sock, "LIST PROFILES", 4096);
-    $answer = "";
-    while (1) {
-    	$line = socket_read($sock, 4096);
-    	if ($line === '')
-	    break;
-    	$answer .= $line;
-    }
-    socket_close($sock);
-    $arr = explode("\r\n", $answer);
+    global $arr;
 
-    $outstr  = build_header();
+    $outstr  = build_header("ThermFerm - Profiles");
 
     $outstr .= '    <div id="errors">'.PHP_EOL;
     $outstr .= '    </div> <!-- errors -->'.PHP_EOL;
-
     $outstr .= '    <div id="etable">'.PHP_EOL;
     $outstr .= '     <table class="setup">'.PHP_EOL;
     $outstr .= '      <tr style="background-color: #FFCC01;">'.PHP_EOL;
@@ -129,12 +330,11 @@
 	    if (strcmp($arr[$j], ".") == 0)
 	    	break;
 	    $f = explode(",", $arr[$j]);
-	    $busy = 0;
 	    $outstr .= '      <tr class="setup">'.PHP_EOL;
 	    $outstr .= '       <td class="setup">'.$f[0].'</td>'.PHP_EOL;
 	    $outstr .= '       <td class="setup">'.$f[1].'</td>'.PHP_EOL;
 	    $outstr .= '       <td class="setup">'.$f[2].'</td>'.PHP_EOL;
-	    if ($busy == 1)
+	    if ($f[3] == 1)
 	    	$outstr .= '       <td class="setup">Busy</td>'.PHP_EOL;
 	    else
 	    	$outstr .= '       <td class="setup"><a href="profiles.php?action=edit&amp;UUID='.$f[0].'">Edit</a></td>'.PHP_EOL;
@@ -145,6 +345,24 @@
 
     $outstr .= '     </table>'.PHP_EOL;
     $outstr .= '    </div> <!-- etable -->'.PHP_EOL;
+    $outstr .= '    <div id="atable">'.PHP_EOL;
+
+    $outstr .= '     <form method="POST" action="profiles.php">'.PHP_EOL;
+    $outstr .= '      <table class="editor">'.PHP_EOL;
+    $outstr .= '       <tr class="editor">'.PHP_EOL;
+    $outstr .= '        <td class="editname">Name</td>'.PHP_EOL;
+    $outstr .= '        <td class="editfield"><input type="text" name="Name" size="50" value="'.$Name.'"></td>'.PHP_EOL;
+    $outstr .= '       </tr>'.PHP_EOL;
+    $outstr .= '       <tr class="editor">'.PHP_EOL;
+    $outstr .= '        <td class="editname"><input type="submit" value="Add" name="key"></td>'.PHP_EOL;
+    $outstr .= '<input type="hidden" value="testdata" name="action">';
+    $outstr .= '<input type="hidden" value="add" name="command">';
+    $outstr .= '<input type="hidden" value="00000000-0000-0000-0000-000000000000" name="UUID">';
+    $outstr .= '       </tr>'.PHP_EOL;
+    $outstr .= '      </table>'.PHP_EOL;
+    $outstr .= '     </form>'.PHP_EOL;
+
+    $outstr .= '    </div> <!-- atable -->'.PHP_EOL;
     $outstr .= build_footer();
     echo $outstr;
 }
--- a/www-thermferm/utilities.php	Sat Jul 26 13:18:00 2014 +0200
+++ b/www-thermferm/utilities.php	Sat Jul 26 17:30:47 2014 +0200
@@ -46,13 +46,13 @@
 
 
 
-function build_header()
+function build_header($heading)
 {
     $outstr  = '<!DOCTYPE html>'.PHP_EOL;
     $outstr .= '<html>'.PHP_EOL;
     $outstr .= ' <head>'.PHP_EOL;
     $outstr .= '  <meta http-equiv="content-type" content="text/html; charset=utf-8" />'.PHP_EOL;
-    $outstr .= '  <title>ThermFerm monitor</title>'.PHP_EOL;
+    $outstr .= '  <title>'.$heading.'</title>'.PHP_EOL;
     $outstr .= '  <link type="text/css" href="css/style.css" rel="stylesheet" media="all" />'.PHP_EOL;
     $outstr .= ' </head>'.PHP_EOL;
     $outstr .= ' <body class="default">'.PHP_EOL;
@@ -80,3 +80,17 @@
 }
 
 
+
+/*
+ * Goto URL. Works also after headers have been sent.
+ */
+function load($url) {
+    echo'
+    <script>
+	if (top.location != self.location) {
+	    top.location = "'.$url.'"
+	}
+	{location.href="'.$url.'";}
+    </script>';
+}
+

mercurial