Index: branches/htmlform/cgi-bin/search.cgi
===================================================================
--- branches/htmlform/cgi-bin/search.cgi	(revision 480)
+++ branches/htmlform/cgi-bin/search.cgi	(revision 481)
@@ -54,6 +54,8 @@
 # Global variables
 my $RESULTS_PER_PAGE = 25;
-##fixme - need to autofill this somehow
-$ENV{HTML_TEMPLATE_ROOT} = '/home/kdeugau/dev/ipdb/trunk/templates';
+
+# anyone got a better name?  :P
+my $thingroot = $ENV{SCRIPT_FILENAME};
+$thingroot =~ s|cgi-bin/search.cgi||;
 
 # Set up the CGI object...
@@ -70,6 +72,13 @@
 }
 
+# Set up some globals
+$ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
+
+my $page;
 if (!defined($webvar{stype})) {
   $webvar{stype} = "<NULL>";   #shuts up the warnings.
+  $page = HTML::Template->new(filename => "search/compsearch.tmpl");
+} else {
+  $page = HTML::Template->new(filename => "search/sresults.tmpl");
 }
 
@@ -212,6 +221,6 @@
 
   if ($count == 0) {
-    printError "No matches found.  Try eliminating one of the criteria,".
-	" or making one or more criteria more general.";
+    $page->param(errmsg => "No matches found.  Try eliminating one of the criteria,".
+	" or making one or more criteria more general.");
   } else {
     # Add the limit/offset clauses
@@ -236,5 +245,6 @@
 
   if ($count == 0) {
-    printError "No customers currently listed as connected through this node.";
+    $page->param(errmsg => "No customers currently listed as connected through this node.");
+##fixme:  still get the results table header
   } else {
     # Add the limit/offset clauses
@@ -248,53 +258,40 @@
 } else { # how script was called.  General case is to show the search criteria page.
 
-  # Display search page.  We have to do this here, because otherwise
-  # we can't retrieve data from the database for the types and cities.  >:(
-  my $html;
-  open HTML,"<../compsearch.html";
-  $html = join('',<HTML>);
-  close HTML;
-
 # Generate table of types
-  my $typetable = "<table class=regular cellspacing=0>\n<tr>";
   $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ".
 	"order by listorder");
   $sth->execute;
   my $i=0;
-  while (my @data = $sth->fetchrow_array) {
-    $typetable .= "<td><input type=checkbox name=type[$data[0]]>$data[1]</td>";
-    $i++;
-    $typetable .= "</tr>\n<tr>"
-	if ($i % 4 == 0);
-  }
-  if ($i %4 == 0) {
-    $typetable =~ s/<tr>$//;
-  } else {
-    $typetable .= "</tr>\n";
-  }
-  $typetable .= "</table>\n";
+  my @typelist;
+  while (my ($type,$dispname) = $sth->fetchrow_array) {
+    my %row = (
+	newrow => ($i % 4 == 0),
+	type => $type,
+	dispname => $dispname,
+	endrow => ($i++ % 4 == 3)
+	);
+    push @typelist, \%row;
+  }
+  $page->param(typelist => \@typelist);
 
 # Generate table of cities
-  my $citytable = "<table class=regular cellspacing=0>\n<tr>";
   $sth = $ip_dbh->prepare("select id,city from cities order by city");
   $sth->execute;
-  my $i=0;
-  while (my @data = $sth->fetchrow_array) {
-    $citytable .= "<td><input type=checkbox name=city[$data[0]]>$data[1]</td>";
-    $i++;
-    $citytable .= "</tr>\n<tr>"
-	if ($i % 5 == 0);
-  }
-  if ($i %5 == 0) {
-    $citytable =~ s/<tr>$//;
-  } else {
-    $citytable .= "</tr>\n";
-  }
-  $citytable .= "</table>\n";
-
-  $html =~ s/\$\$TYPELIST\$\$/$typetable/;
-  $html =~ s/\$\$CITYLIST\$\$/$citytable/;
-
-  print $html;
+  $i=0;
+  my @citylist;
+  while (my ($id, $city) = $sth->fetchrow_array) {
+    my %row = (
+	newrow => ($i % 4 == 0),
+	id => $id,
+	city => $city,
+	endrow => ($i++ % 4 == 3)
+	);	
+    push @citylist, \%row;
+  }
+  $page->param(citylist => \@citylist);
+
 }
+
+print $page->output;
 
 # Shut down and clean up.
@@ -340,5 +337,4 @@
   if ($category eq 'all') {
 
-    print qq(<div class="heading">Showing all netblock and static-IP allocations</div><br>\n);
     $sql = "select $cols from searchme";
     my $count = countRows($sql);
@@ -348,4 +344,5 @@
   } elsif ($category eq 'cust') {
 
+##fixme:  this and other quick-search areas;  fix up page heading title similar to first grouping above
     print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);
 
@@ -411,9 +408,9 @@
     } else {
       # This shouldn't happen, but if it does, whoever gets it deserves what they get...
-      printError("Invalid query.");
+      $page->param(errmsg => "Invalid query.");
     }
   } else {
     # This shouldn't happen, but if it does, whoever gets it deserves what they get...
-    printError("Invalid searchfor.");
+    $page->param(errmsg => "Invalid searchfor.");
   }
 } # viewBy
@@ -455,21 +452,22 @@
   $sth->execute();
 
-  startTable('Allocation','CustID','Type','City','Description/Name');
+  $page->param(searchtitle => "Showing all netblock and static-IP allocations");
+
   my $count = 0;
-
-  while (my @data = $sth->fetchrow_array) {
-
-    # cidr,custid,type,city,description,notes
-    # Another bit of HairyPerl(TM) to prefix subblocks with "Sub"
-    my @row = (($data[2] =~ /^.r$/ ? 'Sub ' : '').
-	qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),
-	$data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]);
-    # Allow listing of pool if desired/required.
-    if ($data[2] =~ /^.[pd]$/) {
-      $row[0] .= ' &nbsp; <a href="/ip/cgi-bin/main.cgi?action=listpool'.
-	"&pool=$data[0]\">List IPs</a>";
-    }
-    printRow(\@row, "row".($count++ % 2), 1);
-  }
+  my @sresults;
+  while (my ($block, $custid, $type, $city, $desc) = $sth->fetchrow_array) {
+    my %row = (
+	rowclass => $count++ % 2,
+	issub => ($type =~ /^.r$/ ? 1 : 0),
+	block => $block,
+	ispool => ($type =~ /^.[pd]$/ ? 1 : 0),
+	custid => $custid,
+	disptype => $disp_alloctypes{$type},
+	city => $city,
+	desc => $desc
+	);
+    push @sresults, \%row;
+  }
+  $page->param(sresults => \@sresults);
 
   # Have to think on this call, it's primarily to clean up unfetched rows from a select.
@@ -478,33 +476,39 @@
 
   my $upper = $offset+$count;
-  print "<tr><td colspan=10 bgcolor=white class=regular>Records found: $rowCount<br><i>Displaying: ".($offset+1)." - $upper</i></td></tr>\n";
-  print "</table></center>\n";
+
+  $page->param(resfound => $rowCount);
+  $page->param(resstart => $offset+1);
+  $page->param(resstop => $upper);
 
   # print the page thing..
   if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) {
+    $page->param(multipage => 1);
     my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
-    print qq(<div class="center"> Page: );
+    my @pagelist;
     for (my $i = 1; $i <= $pages; $i++) {
+      my %row;
+      $row{pgnum} = $i;
       if ($i == $pageNo) {
-	print "<b>$i&nbsp;</b>\n";
+	$row{thispage} = 1;
       } else {
-	print qq(<a href="/ip/cgi-bin/search.cgi?page=$i&stype=$webvar{stype}&);
+	$row{stype} = $webvar{stype};
 	if ($webvar{stype} eq 'c') {
-	  print "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
+	  $row{extraopts} = "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
 		"notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&".
 		"allcities=$webvar{allcities}&";
 	  foreach my $key (keys %webvar) {
 	    if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) {
-	      print "$key=$webvar{$key}&";
+	      $row{extraopts} .= "$key=$webvar{$key}&";
 	    }
 	  }
 	} else {
-	  print "input=$webvar{input}&";
+	  $row{extraopts} = "input=$webvar{input}&";
 	}
-	print qq(">$i</a>&nbsp;\n);
       }
-    }
-    print "</div>";
-  }
+      push @pagelist, \%row;
+    }
+    $page->param(pgnums => \@pagelist);
+  }
+
 } # queryResults
 
