Ignore:
Timestamp:
10/18/12 16:53:10 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Finally merge conversion to HTML::Template from /branches/htmlform

  • Node "hack" showed conflict due to having been added to all branches in parallel
  • editDisplay.html was apparently changed enough that the merged delete caused an irrelevant conflict

Closes #3.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/cgi-bin/search.cgi

    r455 r517  
    1414use warnings;   
    1515use CGI::Carp qw(fatalsToBrowser);
     16use CGI::Simple;
     17use HTML::Template;
    1618use DBI;
    17 use CommonWeb qw(:ALL);
    1819use POSIX qw(ceil);
    1920use NetAddr::IP;
     
    3839}
    3940
     41# Global variables
     42my $RESULTS_PER_PAGE = 25;
     43
     44# anyone got a better name?  :P
     45my $thingroot = $ENV{SCRIPT_FILENAME};
     46$thingroot =~ s|cgi-bin/search.cgi||;
     47
     48# Set up the CGI object...
     49my $q = new CGI::Simple;
     50# ... and get query-string params as well as POST params if necessary
     51$q->parse_query_string;
     52
     53# Convenience;  saves changing all references to %webvar
     54##fixme:  tweak for handling <select multiple='y' size=3> (list with multiple selection)
     55my %webvar = $q->Vars;
     56
     57if (defined($webvar{rpp})) {
     58  ($RESULTS_PER_PAGE) = ($webvar{rpp} =~ /(\d+)/);
     59}
     60
    4061# Why not a global DB handle?  (And a global statement handle, as well...)
    4162# Use the connectDB function, otherwise we end up confusing ourselves
     
    4465my $errstr;
    4566($ip_dbh,$errstr) = connectDB_My;
    46 if (!$ip_dbh) {
    47   printAndExit("Failed to connect to database: $errstr\n");
     67if ($ip_dbh) {
     68  checkDBSanity($ip_dbh);
     69  initIPDBGlobals($ip_dbh);
    4870}
    49 checkDBSanity($ip_dbh);
    50 initIPDBGlobals($ip_dbh);
    51 
    52 # Global variables
    53 my $RESULTS_PER_PAGE = 25;
    54 my %webvar = parse_post();
    55 cleanInput(\%webvar);
    56 
    57 if (defined($webvar{rpp})) {
    58   ($RESULTS_PER_PAGE) = ($webvar{rpp} =~ /(\d+)/);
    59 }
    60 
     71
     72# Set up some globals
     73$ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
     74
     75my $page;
    6176if (!defined($webvar{stype})) {
    6277  $webvar{stype} = "<NULL>";   #shuts up the warnings.
     78  $page = HTML::Template->new(filename => "search/compsearch.tmpl");
     79} else {
     80  $page = HTML::Template->new(filename => "search/sresults.tmpl");
    6381}
    6482
    65 # Headerize!  Make sure we replace the $$EXTRA0$$ bit as needed.
    66 printHeader('', ($IPDBacl{$authuser} =~ /a/ ?
    67         '<td align=right><a href="/ip/cgi-bin/main.cgi?action=assign">Add new assignment</a></td>' : ''
    68         ));
    69 
    70 if ($webvar{stype} eq 'q') {
     83my $header = HTML::Template->new(filename => "header.tmpl");
     84$header->param(version => $IPDB::VERSION);
     85$header->param(addperm => $IPDBacl{$authuser} =~ /a/);
     86print "Content-type: text/html\n\n", $header->output;
     87
     88# Handle the DB error first
     89if (!$ip_dbh) {
     90  $page = HTML::Template->new(filename => "dberr.tmpl");
     91  $page->param(errmsg => $errstr);
     92} elsif ($webvar{stype} eq 'q') {
    7193  # Quick search.
    7294
     
    111133    $sqlconcat = "UNION";
    112134  } else {
    113     # We can't get here.  PTHBTT!
    114     printAndExit "PTHBTT!!  Your search has been rejected due to Microsoft excuse #4432: ".
    115         "Not enough mana";
     135    # sum-buddy tryn'a game the system.  Match "all"
     136    $sqlconcat = "INTERSECT";
    116137  }
    117138
     
    190211        "text(cidr) like '$webvar{cidr}%')";
    191212  } else {
    192     # This shouldn't happen, but if it does, whoever gets it deserves what they get...
    193     printAndExit("Invalid netblock query.");
     213    # do nothing.
     214    ##fixme  we'll ignore this to clear out the references to legacy code.
    194215  } # done with CIDR query options.
    195216
     
    201222
    202223  if ($count == 0) {
    203     printError "No matches found.  Try eliminating one of the criteria,".
    204         " or making one or more criteria more general.";
     224    $page->param(errmsg => "No matches found.  Try eliminating one of the criteria,".
     225        " or making one or more criteria more general.");
    205226  } else {
    206227    # Add the limit/offset clauses
     
    225246
    226247  if ($count == 0) {
    227     printError "No customers currently listed as connected through this node.";
     248    $page->param(errmsg => "No customers currently listed as connected through this node.");
     249##fixme:  still get the results table header
    228250  } else {
    229251    # Add the limit/offset clauses
     
    237259} else { # how script was called.  General case is to show the search criteria page.
    238260
    239   # Display search page.  We have to do this here, because otherwise
    240   # we can't retrieve data from the database for the types and cities.  >:(
    241   my $html;
    242   open HTML,"<../compsearch.html";
    243   $html = join('',<HTML>);
    244   close HTML;
    245 
    246261# Generate table of types
    247   my $typetable = "<table class=regular cellspacing=0>\n<tr>";
    248262  $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ".
    249263        "order by listorder");
    250264  $sth->execute;
    251265  my $i=0;
    252   while (my @data = $sth->fetchrow_array) {
    253     $typetable .= "<td><input type=checkbox name=type[$data[0]]>$data[1]</td>";
    254     $i++;
    255     $typetable .= "</tr>\n<tr>"
    256         if ($i % 4 == 0);
    257   }
    258   if ($i %4 == 0) {
    259     $typetable =~ s/<tr>$//;
    260   } else {
    261     $typetable .= "</tr>\n";
    262   }
    263   $typetable .= "</table>\n";
     266  my @typelist;
     267  while (my ($type,$dispname) = $sth->fetchrow_array) {
     268    my %row = (
     269        newrow => ($i % 4 == 0),
     270        type => $type,
     271        dispname => $dispname,
     272        endrow => ($i++ % 4 == 3)
     273        );
     274    push @typelist, \%row;
     275  }
     276  $page->param(typelist => \@typelist);
    264277
    265278# Generate table of cities
    266   my $citytable = "<table class=regular cellspacing=0>\n<tr>";
    267279  $sth = $ip_dbh->prepare("select id,city from cities order by city");
    268280  $sth->execute;
    269   my $i=0;
    270   while (my @data = $sth->fetchrow_array) {
    271     $citytable .= "<td><input type=checkbox name=city[$data[0]]>$data[1]</td>";
    272     $i++;
    273     $citytable .= "</tr>\n<tr>"
    274         if ($i % 5 == 0);
    275   }
    276   if ($i %5 == 0) {
    277     $citytable =~ s/<tr>$//;
    278   } else {
    279     $citytable .= "</tr>\n";
    280   }
    281   $citytable .= "</table>\n";
    282 
    283   $html =~ s/\$\$TYPELIST\$\$/$typetable/;
    284   $html =~ s/\$\$CITYLIST\$\$/$citytable/;
    285 
    286   print $html;
     281  $i=0;
     282  my @citylist;
     283  while (my ($id, $city) = $sth->fetchrow_array) {
     284    my %row = (
     285        newrow => ($i % 4 == 0),
     286        id => $id,
     287        city => $city,
     288        endrow => ($i++ % 4 == 3)
     289        );     
     290    push @citylist, \%row;
     291  }
     292  $page->param(citylist => \@citylist);
     293
    287294}
     295
     296print $page->output;
    288297
    289298# Shut down and clean up.
    290299finish($ip_dbh);
    291 printFooter;
     300
     301# We print the footer here, so we don't have to do it elsewhere.
     302my $footer = HTML::Template->new(filename => "footer.tmpl");
     303# include the admin tools link in the output?
     304$footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
     305
     306print $footer->output;
     307
    292308# We shouldn't need to directly execute any code below here;  it's all subroutines.
    293309exit 0;
     
    322338  if ($category eq 'all') {
    323339
    324     print qq(<div class="heading">Showing all netblock and static-IP allocations</div><br>\n);
    325340    $sql = "select $cols from searchme";
    326341    my $count = countRows($sql);
     
    330345  } elsif ($category eq 'cust') {
    331346
     347##fixme:  this and other quick-search areas;  fix up page heading title similar to first grouping above
    332348    print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);
    333349
     
    393409    } else {
    394410      # This shouldn't happen, but if it does, whoever gets it deserves what they get...
    395       printError("Invalid query.");
     411      $page->param(errmsg => "Invalid query.");
    396412    }
    397413  } else {
    398414    # This shouldn't happen, but if it does, whoever gets it deserves what they get...
    399     printError("Invalid searchfor.");
     415    $page->param(errmsg => "Invalid searchfor.");
    400416  }
    401417} # viewBy
     
    437453  $sth->execute();
    438454
    439   startTable('Allocation','CustID','Type','City','Description/Name');
     455  $page->param(searchtitle => "Showing all netblock and static-IP allocations");
     456
    440457  my $count = 0;
    441 
    442   while (my @data = $sth->fetchrow_array) {
    443 
    444     # cidr,custid,type,city,description,notes
    445     # Another bit of HairyPerl(TM) to prefix subblocks with "Sub"
    446     my @row = (($data[2] =~ /^.r$/ ? 'Sub ' : '').
    447         qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),
    448         $data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]);
    449     # Allow listing of pool if desired/required.
    450     if ($data[2] =~ /^.[pd]$/) {
    451       $row[0] .= ' &nbsp; <a href="/ip/cgi-bin/main.cgi?action=listpool'.
    452         "&pool=$data[0]\">List IPs</a>";
    453     }
    454     printRow(\@row, 'color1', 1) if ($count%2==0);
    455     printRow(\@row, 'color2', 1) if ($count%2!=0);
    456     $count++;
    457   }
     458  my @sresults;
     459  while (my ($block, $custid, $type, $city, $desc) = $sth->fetchrow_array) {
     460    my %row = (
     461        rowclass => $count++ % 2,
     462        issub => ($type =~ /^.r$/ ? 1 : 0),
     463        block => $block,
     464        ispool => ($type =~ /^.[pd]$/ ? 1 : 0),
     465        custid => $custid,
     466        disptype => $disp_alloctypes{$type},
     467        city => $city,
     468        desc => $desc
     469        );
     470    push @sresults, \%row;
     471  }
     472  $page->param(sresults => \@sresults);
    458473
    459474  # Have to think on this call, it's primarily to clean up unfetched rows from a select.
     
    462477
    463478  my $upper = $offset+$count;
    464   print "<tr><td colspan=10 bgcolor=white class=regular>Records found: $rowCount<br><i>Displaying: ".($offset+1)." - $upper</i></td></tr>\n";
    465   print "</table></center>\n";
     479
     480  $page->param(resfound => $rowCount);
     481  $page->param(resstart => $offset+1);
     482  $page->param(resstop => $upper);
    466483
    467484  # print the page thing..
    468485  if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) {
     486    $page->param(multipage => 1);
    469487    my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
    470     print qq(<div class="center"> Page: );
     488    my @pagelist;
    471489    for (my $i = 1; $i <= $pages; $i++) {
     490      my %row;
     491      $row{pgnum} = $i;
    472492      if ($i == $pageNo) {
    473         print "<b>$i&nbsp;</b>\n";
     493        $row{thispage} = 1;
    474494      } else {
    475         print qq(<a href="/ip/cgi-bin/search.cgi?page=$i&stype=$webvar{stype}&);
     495        $row{stype} = $webvar{stype};
    476496        if ($webvar{stype} eq 'c') {
    477           print "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
     497          $row{extraopts} = "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
    478498                "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&".
    479499                "allcities=$webvar{allcities}&";
    480500          foreach my $key (keys %webvar) {
    481501            if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) {
    482               print "$key=$webvar{$key}&";
     502              $row{extraopts} .= "$key=$webvar{$key}&";
    483503            }
    484504          }
    485505        } else {
    486           print "input=$webvar{input}&";
     506          $row{extraopts} = "input=$webvar{input}&";
    487507        }
    488         print qq(">$i</a>&nbsp;\n);
    489508      }
    490     }
    491     print "</div>";
    492   }
     509      push @pagelist, \%row;
     510    }
     511    $page->param(pgnums => \@pagelist);
     512  }
     513
    493514} # queryResults
    494515
Note: See TracChangeset for help on using the changeset viewer.