Index: trunk/ippMonitor/generate
===================================================================
--- trunk/ippMonitor/generate	(revision 9003)
+++ trunk/ippMonitor/generate	(revision 9008)
@@ -9,4 +9,9 @@
 $template = $ARGV[1];
 $output   = $ARGV[2];
+
+# global variables:
+# @key, @value                         : paired list of keys and values
+# @field, @name, @show, @link, @extras : properties of each table column
+# @headcode, @tailcode                 : additional HTML or PHP code before or after table
 
 &parse_schema;
@@ -31,7 +36,7 @@
 	($key, $value) = split (" ", $line, 2);
 	
-	# strip white space from the following
-	if ($key eq "TABLE") { ($value) = $value =~ m|\s*(\S+)\s*|; }
-	if ($key eq "TYPE")  { ($value) = $value =~ m|\s*(\S+)\s*|; }
+	# strip leading and trailing white space from the following
+	if ($key eq "TABLE") { ($value) = $value =~ m|^\s*(.+)\s*$|; }
+	if ($key eq "TYPE")  { ($value) = $value =~ m|^\s*(.+)\s*$|; }
 
 	&set_keypair ($key, $value);
@@ -52,20 +57,19 @@
 
 	# list of the table fields
- 	if ($key eq "BODY.HTML") {
-	    push @bodyhtml, $value;
+ 	if ($key eq "HEAD") {
+	    push @headcode, $value;
 	}
 
 	# list of the table fields
- 	if ($key eq "TAIL.HTML") {
-	    push @tailhtml, $value;
+ 	if ($key eq "TAIL") {
+	    push @tailcode, $value;
 	}
 
 	# list of the table restrictions
- 	if ($key eq "RESTRICT") {
+	# replace this with a flexible where statement?
+ 	if ($key eq "WHERE") {
 	    ($field) = split (/,\s+/, $value, 1);
-	    # ($field) = $field =~ m|\s*(\S+)\s*|;
-
 	    if ($VERBOSE) { printf "%-20s\n", $field; }
-	    push @restrict,   $field;
+	    push @where,   $field;
 	}
     }
@@ -83,6 +87,8 @@
     }
 
-    # define query string, add to keypairs
-    &define_query_fields;
+    # define FIELDS and WHERE strings, add to keypairs
+    &define_fields_count;
+    &define_fields_string (@field);
+    &define_where_string (@where);
 
     if (0) {
@@ -139,12 +145,12 @@
 	}
 
-	# fill in body HTML
-	if ($line =~ m|// \*\* BODY HTML \*\*|) {
-	    &write_body_html;
-	}
-
-	# fill in tail HTML
-	if ($line =~ m|// \*\* TAIL HTML \*\*|) {
-	    &write_tail_html;
+	# fill in head HTML or PHP code
+	if ($line =~ m|// \*\* HEAD CODE \*\*|) {
+	    &write_inline_code (@headcode);
+	}
+
+	# fill in tail HTML or PHP code
+	if ($line =~ m|// \*\* TAIL CODE \*\*|) {
+	    &write_inline_code (@tailcode);
 	}
     }
@@ -152,28 +158,20 @@
 }
 
-sub write_body_html {
-    my ($i);
-
-    if (@bodyhtml == 0) { return; }
-    printf FILE "?>\n";
+sub write_inline_code {
+    my (@code) = @_;
+    my ($i, $code, $type);
     
-    for ($i = 0; $i < @bodyhtml; $i++) {
-	printf FILE "$bodyhtml[$i]\n";
-    }
-    printf FILE "<?\n";
-}
-
-sub write_tail_html {
-    my ($i);
-
-    if (@tailhtml == 0) { return; }
-    printf FILE "?>\n";
-    
-    for ($i = 0; $i < @tailhtml; $i++) {
-	printf FILE "$tailhtml[$i]\n";
-    }
-    printf FILE "<?\n";
-}
-
+    for ($i = 0; $i < @code; $i++) {
+	($type, $line) = split (" ", $code[$i], 2);
+	if ($type eq "PHP") {
+	    printf FILE "$line\n";
+	}
+	if ($type eq "HTML") {
+	    printf FILE "?> $line <?\n";
+	}
+    }
+}
+
+# generate a table header cell (<th> </th>) for each field (show != none)
 sub write_table_header {
 
@@ -184,25 +182,24 @@
 }
 
+# generate a WHERE test for each field (field != *)
 sub write_table_restrict {
 
-    for ($i = 0; $i < @restrict; $i++) {
-	$value = $restrict[$i];
-	printf FILE "\$value = \$_GET['$value'];\n";
-	printf FILE "if (\$value) {\n";
-	printf FILE "  if (\$WHERE) {\n";
-	printf FILE "    \$WHERE = \$WHERE . \" AND $value = '\$value'\";\n";
-	printf FILE "  } else {\n";
-	printf FILE "    \$WHERE = \"$value = '\$value'\";\n";
-	printf FILE "  }\n";
-	printf FILE "}\n";
+    if ($WHERE) {
+	printf FILE "\$WHERE = \"$WHERE\";\n";
+    }	
+    for ($i = 0; $i < @field; $i++) {
+	$value = $field[$i];
+	if ($value eq "*") { next; }
+	printf FILE "\$WHERE = check_restrict ('$value', \$WHERE);\n";
     }
 }
 
 sub write_table_data {
-    my ($i);
+    my ($i, $Nrow);
     my ($label, $value, $string);
 
     for ($i = 0; $i < @field; $i++) {
 	($label, $value, $string) = &parse_label ($show[$i]);
+	$Nrow = $count[$i];
 
 	# create the link variable if this entry should be linked
@@ -211,5 +208,5 @@
 	if (($label eq "value") && !$link[$i]) { 
 	    # print value without link or image
-	    if (! $value) { $value = "\$row[$i]"; }
+	    if (! $value && ($field[$i] ne "*")) { $value = "\$row[$Nrow]"; }
 	    print FILE "  echo \"<td class=\\\"list\\\"> $value </td>\\n\";\n";
 	    next;
@@ -222,5 +219,5 @@
 	    if ($extras[$i]) { &parse_extras ($extras[$i]); } 
 	    # print the actual table cell line with the link...
-	    if (! $value) { $value = "\$row[$i]"; }
+	    if (! $value && ($field[$i] ne "*")) { $value = "\$row[$Nrow]"; }
 	    printf FILE "  echo \"<td class=\\\"list\\\"><a href=\\\"\$link\\\"> $value </a></td>\\n\";\n";
 	    next;
@@ -246,19 +243,58 @@
 
 sub write_table_query {
-
+    my ($i);
     for ($i = 0; $i < @field; $i++) {
 	if ($show[$i] eq "none")  { next; }
-	printf FILE "echo \"<td class=\\\"list\\\"> <input type=\\\"text\\\" name=\\\"$field[$i]\\\"> </td>\\n\";\n";
-    }
-}
-
-sub define_query_fields {
-
-    $FIELDS = "$field[0]";
-    for ($i = 1; $i < @field; $i++) {
-	$FIELDS = "$FIELDS,$field[$i]";
-    }
- 
+	if ($field[$i] eq "*")  { 
+	    # * fields create an empty cell 
+	    printf FILE "echo \"<td> &nbsp; </td>\\n\";\n";
+	} else {
+	    printf FILE "write_query_row ('$field[$i]');\n";
+	}
+    }
+}
+
+sub define_fields_string {
+    my (@array) = @_;
+    my ($i, $FIELDS);
+
+    $FIELDS = "$array[0]";
+    for ($i = 1; $i < @array; $i++) {
+	# skip * fields (not a valid query element
+	if ($array[$i] eq "*") { next; }
+	$FIELDS = "$FIELDS,$array[$i]";
+    }
     set_keypair ("FIELDS", $FIELDS);
+}
+
+sub define_fields_count {
+    my ($i, $Nrow);
+
+    @count = ();
+
+    $Nrow = 0;
+    for ($i = 0; $i < @field; $i++) {
+	# skip * fields (not a valid query element
+	if ($field[$i] eq "*") { 
+	    $count[$i] = -1;
+	} else {
+	    $count[$i] = $Nrow;
+	    $Nrow ++;
+	}
+    }
+}
+
+sub define_where_string {
+    my (@array) = @_;
+    my ($i, $FIELDS);
+
+    if (@array == 0) { return; }
+
+    $WHERE = "$array[0]";
+    for ($i = 1; $i < @array; $i++) {
+	$WHERE = "$WHERE AND $array[$i]";
+    }
+    
+    set_keypair ("WHERE", $WHERE);
 }
 
@@ -283,5 +319,5 @@
     my ($string) = $_[0];
     my ($label, $value, $outline);
-    my ($N, $i);
+    my ($Nrow, $i);
 
     # unless we replace the value with the db row element, return the original string
@@ -297,6 +333,8 @@
 	  MATCH_LABEL:
 	    for ($i = 0; $i < @field; $i++) {
+		if ($field[$i] eq "*") { next; }
 		if ($value =~ m|^\$$field[$i]|) {
-		    $value = "\$row[$i]";
+		    $Nrow = $count[$i];
+		    $value = "\$row[$Nrow]";
 		    last MATCH_LABEL;
 		}
