Changeset 481


Ignore:
Timestamp:
09/20/10 14:16:51 (14 years ago)
Author:
Kris Deugau
Message:

/branches/htmlform

Update search.cgi to use a few templates (see #3 - note search.cgi NOT completely
updated for HTML only in templates), remove search.cgi references to printError
from CommonWeb.pm (see #15, #26).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/htmlform/cgi-bin/search.cgi

    r478 r481  
    5454# Global variables
    5555my $RESULTS_PER_PAGE = 25;
    56 ##fixme - need to autofill this somehow
    57 $ENV{HTML_TEMPLATE_ROOT} = '/home/kdeugau/dev/ipdb/trunk/templates';
     56
     57# anyone got a better name?  :P
     58my $thingroot = $ENV{SCRIPT_FILENAME};
     59$thingroot =~ s|cgi-bin/search.cgi||;
    5860
    5961# Set up the CGI object...
     
    7072}
    7173
     74# Set up some globals
     75$ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
     76
     77my $page;
    7278if (!defined($webvar{stype})) {
    7379  $webvar{stype} = "<NULL>";   #shuts up the warnings.
     80  $page = HTML::Template->new(filename => "search/compsearch.tmpl");
     81} else {
     82  $page = HTML::Template->new(filename => "search/sresults.tmpl");
    7483}
    7584
     
    212221
    213222  if ($count == 0) {
    214     printError "No matches found.  Try eliminating one of the criteria,".
    215         " or making one or more criteria more general.";
     223    $page->param(errmsg => "No matches found.  Try eliminating one of the criteria,".
     224        " or making one or more criteria more general.");
    216225  } else {
    217226    # Add the limit/offset clauses
     
    236245
    237246  if ($count == 0) {
    238     printError "No customers currently listed as connected through this node.";
     247    $page->param(errmsg => "No customers currently listed as connected through this node.");
     248##fixme:  still get the results table header
    239249  } else {
    240250    # Add the limit/offset clauses
     
    248258} else { # how script was called.  General case is to show the search criteria page.
    249259
    250   # Display search page.  We have to do this here, because otherwise
    251   # we can't retrieve data from the database for the types and cities.  >:(
    252   my $html;
    253   open HTML,"<../compsearch.html";
    254   $html = join('',<HTML>);
    255   close HTML;
    256 
    257260# Generate table of types
    258   my $typetable = "<table class=regular cellspacing=0>\n<tr>";
    259261  $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ".
    260262        "order by listorder");
    261263  $sth->execute;
    262264  my $i=0;
    263   while (my @data = $sth->fetchrow_array) {
    264     $typetable .= "<td><input type=checkbox name=type[$data[0]]>$data[1]</td>";
    265     $i++;
    266     $typetable .= "</tr>\n<tr>"
    267         if ($i % 4 == 0);
    268   }
    269   if ($i %4 == 0) {
    270     $typetable =~ s/<tr>$//;
    271   } else {
    272     $typetable .= "</tr>\n";
    273   }
    274   $typetable .= "</table>\n";
     265  my @typelist;
     266  while (my ($type,$dispname) = $sth->fetchrow_array) {
     267    my %row = (
     268        newrow => ($i % 4 == 0),
     269        type => $type,
     270        dispname => $dispname,
     271        endrow => ($i++ % 4 == 3)
     272        );
     273    push @typelist, \%row;
     274  }
     275  $page->param(typelist => \@typelist);
    275276
    276277# Generate table of cities
    277   my $citytable = "<table class=regular cellspacing=0>\n<tr>";
    278278  $sth = $ip_dbh->prepare("select id,city from cities order by city");
    279279  $sth->execute;
    280   my $i=0;
    281   while (my @data = $sth->fetchrow_array) {
    282     $citytable .= "<td><input type=checkbox name=city[$data[0]]>$data[1]</td>";
    283     $i++;
    284     $citytable .= "</tr>\n<tr>"
    285         if ($i % 5 == 0);
    286   }
    287   if ($i %5 == 0) {
    288     $citytable =~ s/<tr>$//;
    289   } else {
    290     $citytable .= "</tr>\n";
    291   }
    292   $citytable .= "</table>\n";
    293 
    294   $html =~ s/\$\$TYPELIST\$\$/$typetable/;
    295   $html =~ s/\$\$CITYLIST\$\$/$citytable/;
    296 
    297   print $html;
     280  $i=0;
     281  my @citylist;
     282  while (my ($id, $city) = $sth->fetchrow_array) {
     283    my %row = (
     284        newrow => ($i % 4 == 0),
     285        id => $id,
     286        city => $city,
     287        endrow => ($i++ % 4 == 3)
     288        );     
     289    push @citylist, \%row;
     290  }
     291  $page->param(citylist => \@citylist);
     292
    298293}
     294
     295print $page->output;
    299296
    300297# Shut down and clean up.
     
    340337  if ($category eq 'all') {
    341338
    342     print qq(<div class="heading">Showing all netblock and static-IP allocations</div><br>\n);
    343339    $sql = "select $cols from searchme";
    344340    my $count = countRows($sql);
     
    348344  } elsif ($category eq 'cust') {
    349345
     346##fixme:  this and other quick-search areas;  fix up page heading title similar to first grouping above
    350347    print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);
    351348
     
    411408    } else {
    412409      # This shouldn't happen, but if it does, whoever gets it deserves what they get...
    413       printError("Invalid query.");
     410      $page->param(errmsg => "Invalid query.");
    414411    }
    415412  } else {
    416413    # This shouldn't happen, but if it does, whoever gets it deserves what they get...
    417     printError("Invalid searchfor.");
     414    $page->param(errmsg => "Invalid searchfor.");
    418415  }
    419416} # viewBy
     
    455452  $sth->execute();
    456453
    457   startTable('Allocation','CustID','Type','City','Description/Name');
     454  $page->param(searchtitle => "Showing all netblock and static-IP allocations");
     455
    458456  my $count = 0;
    459 
    460   while (my @data = $sth->fetchrow_array) {
    461 
    462     # cidr,custid,type,city,description,notes
    463     # Another bit of HairyPerl(TM) to prefix subblocks with "Sub"
    464     my @row = (($data[2] =~ /^.r$/ ? 'Sub ' : '').
    465         qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),
    466         $data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]);
    467     # Allow listing of pool if desired/required.
    468     if ($data[2] =~ /^.[pd]$/) {
    469       $row[0] .= ' &nbsp; <a href="/ip/cgi-bin/main.cgi?action=listpool'.
    470         "&pool=$data[0]\">List IPs</a>";
    471     }
    472     printRow(\@row, "row".($count++ % 2), 1);
    473   }
     457  my @sresults;
     458  while (my ($block, $custid, $type, $city, $desc) = $sth->fetchrow_array) {
     459    my %row = (
     460        rowclass => $count++ % 2,
     461        issub => ($type =~ /^.r$/ ? 1 : 0),
     462        block => $block,
     463        ispool => ($type =~ /^.[pd]$/ ? 1 : 0),
     464        custid => $custid,
     465        disptype => $disp_alloctypes{$type},
     466        city => $city,
     467        desc => $desc
     468        );
     469    push @sresults, \%row;
     470  }
     471  $page->param(sresults => \@sresults);
    474472
    475473  # Have to think on this call, it's primarily to clean up unfetched rows from a select.
     
    478476
    479477  my $upper = $offset+$count;
    480   print "<tr><td colspan=10 bgcolor=white class=regular>Records found: $rowCount<br><i>Displaying: ".($offset+1)." - $upper</i></td></tr>\n";
    481   print "</table></center>\n";
     478
     479  $page->param(resfound => $rowCount);
     480  $page->param(resstart => $offset+1);
     481  $page->param(resstop => $upper);
    482482
    483483  # print the page thing..
    484484  if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) {
     485    $page->param(multipage => 1);
    485486    my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
    486     print qq(<div class="center"> Page: );
     487    my @pagelist;
    487488    for (my $i = 1; $i <= $pages; $i++) {
     489      my %row;
     490      $row{pgnum} = $i;
    488491      if ($i == $pageNo) {
    489         print "<b>$i&nbsp;</b>\n";
     492        $row{thispage} = 1;
    490493      } else {
    491         print qq(<a href="/ip/cgi-bin/search.cgi?page=$i&stype=$webvar{stype}&);
     494        $row{stype} = $webvar{stype};
    492495        if ($webvar{stype} eq 'c') {
    493           print "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
     496          $row{extraopts} = "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
    494497                "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&".
    495498                "allcities=$webvar{allcities}&";
    496499          foreach my $key (keys %webvar) {
    497500            if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) {
    498               print "$key=$webvar{$key}&";
     501              $row{extraopts} .= "$key=$webvar{$key}&";
    499502            }
    500503          }
    501504        } else {
    502           print "input=$webvar{input}&";
     505          $row{extraopts} = "input=$webvar{input}&";
    503506        }
    504         print qq(">$i</a>&nbsp;\n);
    505507      }
    506     }
    507     print "</div>";
    508   }
     508      push @pagelist, \%row;
     509    }
     510    $page->param(pgnums => \@pagelist);
     511  }
     512
    509513} # queryResults
    510514
Note: See TracChangeset for help on using the changeset viewer.