Changeset 487 for branches/htmlform


Ignore:
Timestamp:
09/23/10 01:10:39 (14 years ago)
Author:
Kris Deugau
Message:

/branches/htmlform

Finish IP pool tweaking
Complete email notice configuration
Finish most of block allocation
Add generic error page
Fix admin header with reference to Javascript bits
See #3, #15, #26

Location:
branches/htmlform
Files:
12 added
3 edited

Legend:

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

    r484 r487  
    8484}
    8585
    86 #print "Content-type: text/html\n\n".$header->output;
    87 my $page = HTML::Template->new(filename => "admin/$webvar{action}.tmpl");
     86my $page;
     87if (-e "$ENV{HTML_TEMPLATE_ROOT}/admin/$webvar{action}.tmpl") {
     88  $page = HTML::Template->new(filename => "admin/$webvar{action}.tmpl");
     89} else {
     90  $page = HTML::Template->new(filename => "admin/dunno.tmpl");
     91}
    8892
    8993if ($webvar{action} eq 'main') {
     
    180184  $sth->finish;
    181185
    182   my $cities = '';
     186  my @cities;
    183187  foreach my $city (@citylist) {
    184     $cities .= "<option>$city</option>\n";
    185   }
    186 
    187   print qq(<table class=regular>
    188 <form method=POST action=admin.cgi>
    189 <tr class=color1>
    190 <td>Allocating:</td>
    191 <td>$cidr<input type=hidden name=cidr value="$cidr"></td>
    192 </tr><tr class=color2>
    193 <td>Type:</td><td>$disp_alloctypes{$webvar{alloctype}}
    194 <input type=hidden name=alloctype value="$webvar{alloctype}"></td>
    195 </tr><tr class=color1>
    196 <td>Allocated from:</td>
    197 <td>$alloc_from<input type=hidden name=alloc_from value="$alloc_from"></td>
    198 </tr><tr class="color2">
    199 <td>Customer ID:</td><td>$custid<input type=hidden name=custid value="$custid"></td>
    200 </tr><tr class=color1>
    201 <td>Customer location:</td><td>
    202 <select name="city"><option selected>-</option>
    203 $cities
    204 </select>
    205 &nbsp;<a href="javascript:popNotes('/ip/cgi-bin/newcity.cgi')">Add new location</a>
    206 </td>
    207 </tr>
    208 <tr class="color2">
    209 <td>Circuit ID:</td><td><input name=circid size=40></td>
    210 </tr><tr class="color1">
    211 <td>Description/Name:</td><td><input name="desc" size=40></td>
    212 </tr><tr class="color2">
    213 <td>Notes:</td><td><textarea name="notes" rows="3" cols="40"></textarea></td>
    214 </tr><tr class="warning">
    215 <td colspan=2><center>WARNING:  This will IMMEDIATELY assign this block!!</center></td>
    216 </tr><tr class="color2">
    217 <td class="center" colspan="2"><input type="submit" value="  Assign  "></td>
    218 <input type="hidden" name="action" value="confirm">
    219 </form>
    220 </tr>
    221 </table>
    222 );
    223 
     188     my %row = (city => $city);
     189     push @cities, \%row;
     190  }
     191  $page->param(
     192        cidr => $cidr,
     193        disptype => $disp_alloctypes{$webvar{alloctype}},
     194        type => $webvar{alloctype},
     195        alloc_from => $alloc_from,
     196        custid => $custid,
     197        citylist => \@cities
     198        );
    224199
    225200} elsif ($webvar{action} eq 'confirm') {
    226201
    227   print "Assigning $webvar{cidr} to $webvar{custid} (\"$webvar{desc}\") as ".
    228         "$disp_alloctypes{$webvar{alloctype}}...<br>\n";
     202  $page->param(
     203        cidr => $webvar{cidr},
     204        custid => $webvar{custid},
     205        desc => $webvar{desc},
     206        disptype => $disp_alloctypes{$webvar{alloctype}}
     207        );
    229208  # Only need to check city here.
    230209  if ($webvar{city} eq '-') {
     
    237216      $sth->execute;
    238217      if ($sth->err) {
    239         print "Allocation failed!  DBI said:\n".$sth->errstr."\n";
     218        $page->param(errmsg => $sth->errstr);
    240219        syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
    241220                "'$webvar{alloctype}' failed: '".$sth->errstr."'";
    242221      } else {
    243         print "Allocation OK!\n";
    244222        syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
    245223                "'$webvar{alloctype}'";
     
    253231        $webvar{circid});
    254232      if ($retcode eq 'OK') {
    255         print "Allocation OK!\n";
    256233        syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
    257234                "'$webvar{alloctype}'";
    258235      } else {
    259         print "Allocation failed!  IPDB::allocateBlock said:\n$msg\n";
     236        $page->param(errmsg => $msg);
    260237        syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
    261238                "'$webvar{alloctype}' failed: '$msg'";
     
    379356
    380357} elsif ($webvar{action} eq 'tweakpool') {
     358
    381359  showPool($webvar{pool});
     360
    382361} elsif ($webvar{action} eq 'updatepool') {
    383362
     
    388367        "where ip='$webvar{ip}'");
    389368  $sth->execute($webvar{city},$webvar{notes},$webvar{desc});
     369  $page->param(ip => $webvar{ip});
    390370  if ($sth->err) {
    391     print "Error updating pool IP $webvar{ip}: $@<hr>\n";
    392     syslog "err", "$authuser could not update pool IP $webvar{ip}: $@";
    393   } else { 
    394     $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
    395     $sth->execute;
    396     my @data = $sth->fetchrow_array;
    397     print "$webvar{ip} in $data[0] updated\n<hr>\n";
     371    $page->param(errmsg => $sth->errstr);
     372    syslog "err", "$authuser could not update pool IP $webvar{ip}: ".$sth->errstr;
     373  } else {
    398374    syslog "notice", "$authuser updated pool IP $webvar{ip}";
    399375  }
     376  $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
     377  $sth->execute;
     378  my @data = $sth->fetchrow_array;
     379  $page->param(pool => $data[0]);
    400380
    401381} elsif ($webvar{action} eq 'showusers') {
     
    455435
    456436} elsif ($webvar{action} eq 'emailnotice') {
    457   print "<h4>Email notice management:</h4>\nClick the email addresses to edit that list.";
     437
    458438  $sth = $ip_dbh->prepare("SELECT action,reciplist FROM notify");
    459439  $sth->execute;
    460 
    461   print "<table border=1>\n";
     440  my @spamlist;
    462441  while (my ($notice_code,$reciplist) = $sth->fetchrow_array() ) {
    463442##fixme: hairy mess, only a few things call mailNotify() anyway, so many possible notices won't work.
    464443    my $action_out = dispNoticeCode($notice_code);
    465     print "<tr><td>$action_out</td>".
    466         qq(<td><a href="admin.cgi?action=ednotice&code=$notice_code">$reciplist</a></td>).
    467         qq(<td><a href="admin.cgi?action=delnotice&code=$notice_code">Delete</a></tr>\n);
    468   }
    469   print qq(<tr><td colspan=2>Known "special" codes:<br>
    470 <ul style="margin-top: 0px; margin-bottom: 0px;">
    471         <li>swi: Notify if block being updated has SWIP flag set</li>
    472 </ul></td></tr>
    473 </table>
    474 );
    475 
    476 # add new entries from this tangle:
    477   print "<h4>Add new notification:</h4>\n".
    478         "Note:  Failure notices on most conditions are not yet supported.\n";
    479 
    480   print qq(<table border=1><form action=admin.cgi method="POST">
    481 <input type=hidden name=action value=addnotice>
    482 <tr>
    483 <td>Recipients</td><td colspan=3><textarea name=reciplist cols=50 rows=5></textarea></td></tr>
    484 <tr><td>Action</td><td>
    485         <table><tr>
    486                 <td><input type=radio name=msgaction value=a>Add &nbsp;
    487                 <input type=radio name=msgaction value=u>Update &nbsp;
    488                 <input type=radio name=msgaction value=d>Delete &nbsp;
    489                 <input type=radio name=msgaction value=n>New listitem</td>
    490         </tr><tr>
    491                 <td>
    492                 <input type=radio name=msgaction value=s:>Special: <input name=special>(requires code changes)
    493         </td></tr></table>
    494 </td>
    495 <td>Failure?</td><td><input type=checkbox name=onfail></td></tr>
    496 <tr><td>Event/Allocation type:</td><td colspan=3>
    497         <table>
    498         <tr>
    499                 <td><input type=radio name=alloctype value=a>All allocations</td>
    500                 <td><input type=radio name=alloctype value=.i>All static IPs</td>
    501                 <td><input type=radio name=alloctype value=ci>New city</td>
    502                 <td><input type=radio name=alloctype value=no>New node</td>
    503         </tr>
    504         <tr>
    505 );
     444    my %row = (
     445        action => $action_out,
     446        code => $notice_code,
     447        recips => $reciplist
     448        );
     449    push @spamlist, \%row;
     450  }
     451  $page->param(spamlist => \@spamlist);
    506452
    507453  $sth = $ip_dbh->prepare("SELECT type,dispname FROM alloctypes WHERE listorder < 500 ".
     
    509455  $sth->execute;
    510456  my $i=0;
     457  my @typelist;
    511458  while (my ($type,$disp) = $sth->fetchrow_array) {
    512     print "             <td><input type=radio name=alloctype value=$type>$disp</td>";
    513     $i++;
    514     print "     </tr>\n\t<tr>"
    515         if ($i % 4 == 0);
    516   }
    517 
    518   print qq(     </tr>
    519         </table>
    520 </tr>
    521 <tr><td colspan=4 align=center><input type=submit value="Add notice"></td></tr>
    522 </table>
    523 </form>
    524 );
    525   ## done spitting out add-new-spam-me-now table
     459    my %row = (
     460        type => $type,
     461        disptype => $disp,
     462# ahh, off-by-one counts, how we do love thee...  NOT!
     463        newrow => ($i+2 > $sth->rows ? 1 : (++$i % 4)),
     464        );
     465    push @typelist, \%row;
     466  }
     467  $page->param(typelist => \@typelist);
    526468
    527469} elsif ($webvar{action} eq 'addnotice') {
     470
    528471  $webvar{alloctype} = $webvar{special} if $webvar{msgaction} eq 's:';
    529472  if ($webvar{msgaction} && $webvar{alloctype} && $webvar{reciplist}) {
     473    $page->param(cantry => 1);
    530474    $webvar{reciplist} =~ s/[\r\n]+/,/g;
    531475    $webvar{msgaction} = "f:$webvar{msgaction}" if $webvar{onfail};
    532     print "Adding notice to $webvar{reciplist} for ".dispNoticeCode($webvar{msgaction}.$webvar{alloctype}).":\n";
     476    $page->param(reciplist => $webvar{reciplist});
     477    $page->param(dispnotice => dispNoticeCode($webvar{msgaction}.$webvar{alloctype}));
    533478    $sth = $ip_dbh->prepare("INSERT INTO notify (action, reciplist) VALUES (?,?)");
    534479##fixme:  automagically merge reciplists iff action already exists
    535480    $sth->execute($webvar{msgaction}.$webvar{alloctype}, $webvar{reciplist});
    536     if ($sth->err) {
    537       print "Failed:  DB error: ".$sth->errstr."\n";
    538     } else {
    539       print "OK!<br>\n"
    540     }
    541   } else {
    542     print "Need to specify at least one recipient, an action, and an allocation type. ".
    543         qq{("Special" content is considered an allocation type).  Hit the Back button and try again.<br>\n};
    544   }
    545   print qq(<a href="admin.cgi?action=emailnotice">Back to email notice list</a>\n);
     481    $page->param(addfailed => $sth->errstr) if $sth->err;
     482  }
    546483
    547484} elsif ($webvar{action} eq 'delnotice') {
    548   print "Deleting notices on ".dispNoticeCode($webvar{code}.$webvar{alloctype}).":\n";
     485
     486  $page->param(dispnotice => dispNoticeCode($webvar{code}.$webvar{alloctype}));
    549487  $sth = $ip_dbh->prepare("DELETE FROM notify WHERE action=?");
    550488  $sth->execute($webvar{code});
    551   if ($sth->err) {
    552     print "Failed:  DB error: ".$sth->errstr."\n";
    553   } else {
    554     print "OK!<br>\n"
    555   }
    556   print qq(<a href="admin.cgi?action=emailnotice">Back to email notice list</a>\n);
     489  $page->param(delfailed => $sth->errstr) if $sth->err;
    557490
    558491} elsif ($webvar{action} eq 'ednotice') {
    559   print "<h4>Editing recipient list for '".dispNoticeCode($webvar{code})."':</h4>\n";
     492
     493  $page->param(dispnotice => dispNoticeCode($webvar{code}));
     494  $page->param(code => $webvar{code});
    560495  $sth = $ip_dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
    561496  $sth->execute($webvar{code});
    562497  my ($reciplist) = $sth->fetchrow_array;
    563498  $reciplist =~ s/,/\n/g;
    564   print qq(<form action=admin.cgi method=POST><input type=hidden name=code value="$webvar{code}">\n).
    565         qq(<input type=hidden name=action value="updnotice"><table border=1><tr><td>).
    566         qq(<textarea cols="40" rows="5" name=reciplist>$reciplist</textarea></td><td><input type=submit value="Update">\n).
    567         "</td></tr></table></form>\n";
     499  $page->param(reciplist => $reciplist);
     500
    568501} elsif ($webvar{action} eq 'updnotice') {
    569   print "<h4>Updating recipient list for '".dispNoticeCode($webvar{code})."':</h4>\n";
     502
     503  $page->param(dispnotice => dispNoticeCode($webvar{code}));
    570504  $sth = $ip_dbh->prepare("UPDATE notify SET reciplist=? WHERE action=?");
    571505  $webvar{reciplist} =~ s/[\r\n]+/,/g;
    572506  $sth->execute($webvar{reciplist}, $webvar{code});
    573   if ($sth->err) {
    574     print "Failed:  DB error: ".$sth->errstr."\n";
    575   } else {
    576     print "OK!<br>\n"
    577   }
    578   print qq(<a href="admin.cgi?action=emailnotice">Back to email notice list</a>\n);
     507  $page->param(updfailed => $sth->errstr) if $sth->err;
     508
    579509} elsif ($webvar{action} ne '<NULL>') {
    580   print "webvar{action} check failed: Don't know how to $webvar{action}";
     510  $page->param(dunno => $webvar{action});
    581511}
    582512
     
    645575
    646576# Show allocations to allow editing.
    647 sub showAllocs($) {
    648   my $cidr = new NetAddr::IP $_[0];
    649   print "Edit custID, allocation type, city for allocations in ".
    650         "$cidr:\n<table border=1>";
    651   $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr");
    652   $sth->execute;
    653   while (my @data = $sth->fetchrow_array) {
    654     print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n".
    655         qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n).
    656         qq(<td><input name=custid value="$data[1]"></td>\n).
    657         "<td><select name=alloctype>";
    658 
     577sub showAllocs {
     578
     579  my $within = new NetAddr::IP $_[0];
     580  $page->param(within => $within);
     581
     582  $sth = $ip_dbh->prepare("select cidr,custid,type,city,description from allocations where cidr <<= '$within' order by cidr");
     583  $sth->execute;
     584  my @blocklist;
     585  while (my ($cidr,$custid,$type,$city,$desc) = $sth->fetchrow_array) {
     586    my %row = (
     587        cidr => $cidr,
     588        custid => $custid,
     589        city => $city,
     590        desc => $desc,
     591        );
     592
     593##fixme:  don't wanna retrieve the whole type list *every time around the outer loop*
    659594    my $sth2 = $ip_dbh->prepare("select type,listname from alloctypes".
    660595        " where listorder < 500 and not (type like '_i') order by listorder");
    661596    $sth2->execute;
    662     while (my @types = $sth2->fetchrow_array) {
    663       print "<option". (($data[2] eq $types[0]) ? ' selected' : '') .
    664         " value='$types[0]'>$types[1]</option>\n";
     597    my @typelist;
     598    while (my ($listtype,$dispname) = $sth2->fetchrow_array) {
     599      my %subrow = (
     600        type => $listtype,
     601        dispname => $dispname,
     602        selected => ($listtype eq $type)
     603        );
     604      push @typelist, \%subrow;
    665605    }
    666 
    667     print qq(<td><input name=city value="$data[3]"></td>\n).
    668         "<td>$data[4]</td><td>$data[5]</td>".
    669         qq(<td><input type=submit value="Update"></td></form></tr>\n);
    670   }
    671   print "</table>\n";
    672 
    673   # notes
    674   print "<hr><b>Notes:</b>\n".
    675         "<ul>\n<li>Use the main interface to update description and notes fields\n".
    676         "<li>Changing the allocation type here will NOT affect IP pool data.\n".
    677         "</ul>\n";
    678 }
     606    $row{typelist} = \@typelist;
     607    push @blocklist, \%row;
     608  }
     609  $page->param(blocklist => \@blocklist);
     610} # end showAllocs()
    679611
    680612
    681613# Stuff updates into DB
    682614sub update {
    683   eval {
    684     # Relatively simple SQL transaction here.  Note that we're deliberately NOT
    685     # updating notes/desc here as it's available through the main interface.
    686     $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
    687         "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'");
    688     $sth->execute;
    689     $ip_dbh->commit;
    690   };
    691   if ($@) {
    692     carp "Transaction aborted because $@";
    693     eval { $ip_dbh->rollback; };
    694     syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
     615  # Relatively simple SQL transaction here.  Note that we're deliberately NOT
     616  # updating notes/desc here as it's available through the main interface.
     617  $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
     618        "city=?,type='$webvar{alloctype}' where cidr='$webvar{block}'");
     619  $sth->execute($webvar{city});
     620
     621  $page->param(block => $webvar{block});
     622  if ($sth->err) {
     623    $page->param(updfailed => $sth->errstr);
     624    syslog "err", "$authuser could not update block '$webvar{block}': '".$sth->errstr."'";
    695625  } else {
    696     # If we get here, the operation succeeded.
    697626    syslog "notice", "$authuser updated $webvar{block}";
    698     print "Allocation $webvar{block} updated<hr>\n";
    699627  }
    700628  # need to get /24 that block is part of
     
    702630  $bits[3] = "0/24";
    703631  showAllocs((join ".", @bits));
    704 }
     632} # end update()
    705633
    706634
     
    710638sub showPool($) {
    711639  my $pool = new NetAddr::IP $_[0];
    712   print qq(Listing pool $pool:\n<table border=1>
    713 <form action=admin.cgi method=POST>
    714 <input type=hidden name=action value=updatepool>
    715 <tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
    716 <tr><td align=right>Customer location:</td><td><input name=city></td></tr>
    717 <tr><td align=right>Type:</td><td><select name=type><option selected>-</option>\n);
    718640
    719641  $sth = $ip_dbh->prepare("select type,listname from alloctypes where type like '_i' order by listorder");
    720642  $sth->execute;
    721   while (my @data = $sth->fetchrow_array) {
    722     print "<option value='$data[0]'>$data[1]</option>\n";
    723   }
    724 
    725   print qq(</select></td></tr>
    726 <tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
    727 <tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr>
    728 <tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr>
    729 <tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
    730 ).
    731         "</table>Update the following record:<table border=1>\n";
    732   $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
    733   $sth->execute;
    734   while (my @data = $sth->fetchrow_array) {
    735     print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>).
    736         "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>".
    737         "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n";
    738   }
    739   print "</form></table>\n";
    740 }
     643  my @typelist;
     644  while (my ($type,$dispname) = $sth->fetchrow_array) {
     645    my %row = (
     646        type => $type,
     647        dispname => $dispname
     648        );
     649    push @typelist, \%row;
     650  }
     651  $page->param(typelist => \@typelist);
     652
     653  $sth = $ip_dbh->prepare("select ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
     654  $sth->execute;
     655  my @iplist;
     656  while (my ($ip,$custid,$city,$type,$avail,$desc,$notes) = $sth->fetchrow_array) {
     657    my %row = (
     658        ip => $ip,
     659        custid => $custid,
     660        city => $city,
     661        type => $type,
     662        avail => $avail,
     663        desc => $desc,
     664        notes => $notes
     665        );
     666    push @iplist, \%row;
     667  }
     668  $page->param(iplist => \@iplist);
     669} # end showPool()
    741670
    742671
  • branches/htmlform/templates/admin/header.tmpl

    r483 r487  
    55        <link rel="stylesheet" type="text/css" href="/ip/ipdb.css">
    66        <link rel="stylesheet" type="text/css" href="/ip/local.css">
     7        <script src="/ip/templates/widgets.js" type="text/javascript"></script>
    78</head>
    89<body>
  • branches/htmlform/templates/admin/tweakpool.tmpl

    r484 r487  
     1Listing pool <TMPL_VAR NAME=pool>:
     2<form action="admin.cgi" method="POST">
     3<fieldset><legend></legend>
     4<input type="hidden" name="action" value="updatepool">
     5<table border="1">
     6<tr><td align="right">Customer ID:</td><td><input name="custid"></td></tr>
     7<tr><td align="right">Customer location:</td><td><input name="city"></td></tr>
     8<tr><td align="right">Type:</td><td><select name="type"><option selected>-</option>
     9<TMPL_LOOP name=typelist><option value="<TMPL_VAR NAME=type>"><TMPL_VAR NAME=dispname></option>
     10</TMPL_LOOP>
     11</select></td></tr>
     12<tr><td align="right">Available?</td><td><input type="checkbox" value="y"></td></tr>
     13<tr><td align="right">Description/name:</td><td><input name="desc" size="40"></td></tr>
     14<tr><td align="right">Notes:</td><td><textarea name="notes" rows="3" cols="40"></textarea></td></tr>
     15<tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
     16</table>
     17Update the following record:
     18<table border=1>
     19<TMPL_LOOP NAME=iplist>
     20<tr><td><input type="radio" name="ip" value="<TMPL_VAR NAME=ip>"><TMPL_VAR NAME=ip></td>
     21<td><TMPL_VAR NAME=custid></td><td><TMPL_VAR NAME=city></td><td><TMPL_VAR NAME=type></td>
     22<td><TMPL_VAR NAME=avail></td><td><TMPL_VAR NAME=desc></td><td><TMPL_VAR NAME=notes></td></tr>
     23</TMPL_LOOP>
     24</table>
     25</fieldset></form>
Note: See TracChangeset for help on using the changeset viewer.