package ISPMan::DNSMan::NSrecords;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $Config);

require Exporter;

@ISA = qw(Exporter AutoLoader);
@EXPORT = qw(
editnsrecords
addnsrecord
modifynsrecords
getns
);
$VERSION = '0.01';

use ISPMan::Config;
$Config=ISPMan::Config->new();


sub editnsrecords {
    my $self=shift;
    my $r=shift;
    my $dn=$r->param("dn");
    my ($entry)=$self->{'ldap'}->getEntry($dn);
    
    print  $r->param('domain'), "::NS Records<br><hr noshade>";
   
    print qq|
    <table border=0>
    <tr>
       <th>
          <font face="Helvetica, Arial" color="#666699" size=3><b>Origion</b></font>
       </th>
       <th>
          <font face="Helvetica, Arial" color="#666699" size=3><b>Name Server</b></font>
       </th>
    </tr>
    |;
   
   my @records=$entry->get_value("record");
   if (@records) {
   print qq|
      <form name=nsModifyForm method=POST>
      <input type=hidden name=dn value="$dn">
      <input type=hidden name=mode value="modifynsrecords">
   |;

   for (@records) {
        my ($origion, $host)=split(/\s*,\s*/, $_);
        print qq|
        <tr>
        <td><font face="Helvetica, Arial" color="#666699" size=3><b><input  type=text name=origion value='$origion'></b></font></td>
         <td><font face="Helvetica, Arial" color="#666699" size=3><b><input type=text name=host value='$host'></b></font></td>
      </tr>
      |;
   }
   print qq|
   <tr>
      <td>
      <a href="javascript:void(document.forms['nsModifyForm'].reset())"><img name="resetForm" border=0 alt="Discard Changes" src="/gfx/but/but_discard_chg_0.gif"></a>
      </td>
      <td align=right>
      <a href="javascript:void(document.forms['nsModifyForm'].submit())"><img name="applyChanges" border=0 alt="Apply Changes" src="/gfx/but/but_apply_chg_0.gif"></a>
      </td>
   </tr>

   <tr>
   </form>
   |;
   }

   print qq|
   <form name=nsAddForm method="POST">
      <tr><td colspan=2><hr noshade></hr></td></tr>

      <tr>
         <input type=hidden name=dn value="$dn">
         <input type=hidden name=mode value="addnsrecord">
   
         <td><font face="Helvetica, Arial" color="#666699" size=3><b><input  type=text name=origion></b></font></td>
         <td><font face="Helvetica, Arial" color="#666699" size=3><b><input type=text name=host value=''></b></font></td>
      </tr>
      <tr>
         <td>&nbsp;</td>
            
         <td align=right>
         <a href="javascript:void(document.forms['nsAddForm'].submit())"><img name="saveNewEntry" border=0 alt="Save new Entry" src="/gfx/but/but_new_entry_0.gif"></a>
         </td>
      </form>
      </tr>

   |;

   print "</table>";
}





sub modifynsrecords {
   my $self=shift;
   my $r=shift;
   my $dn=$r->param("dn");
   
   my (@hosts, @origions);
   for ($r->param("host")) {
      push @hosts, $_  if $_;
   }
   for ($r->param("origion")) {
      push @origions, $_  if $_;
   }
   my @records=map{"$origions[$_], $hosts[$_]"}0..$#hosts;
   if (@records) {
      $r->param("record", \@records);
      $self->{'ldap'}->updateEntry($r);
   }
   $self->editnsrecords($r);
}

sub addnsrecord {
   my $self=shift;
   my $r=shift;
   if ($r->param("host")){
      my $record=join ',', ($r->param("origion"), $r->param("host"));
      $r->param("record", $record);
      $self->{'ldap'}->addEntry($r);
   }
   $self->editnsrecords($r);
}





1;
__END__

