package ISPMan::DNSMan::Arecords;

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

require Exporter;

@ISA = qw(Exporter AutoLoader);
@EXPORT = qw(
editarecords
modifyarecords
addarecord
);
$VERSION = '0.01';

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



sub editarecords {
   my $self=shift;
   my $r=shift;
   my $dn=$r->param("dn");
   
   print  $r->param('domain'), "::A records<br><hr noshade>";
   print "<table border=0 cellpadding=0 cellspacing=0><tr><th>Hostname </th><th>IP Address</th></tr>\n";


   my $entry=$self->{'ldap'}->getEntry($dn);

   my @records=$entry->get_value("record");
   if (scalar(@records)){
      print qq|

         <form name="aModifyForm" method="POST">
         <input type=hidden name=dn value="$dn">
         <input type=hidden name=mode value="modifyarecords">
      |;

      for (@records) {
           my ($host, $ip)=split(/\s*,\s*/, $_);
           print qq|
           <tr>
            <td><input type=text name=host value='$host'></td>
              <td><input  type=text name=ip value='$ip'></td>
         </tr>
         |;
      }
      print qq|
      <tr>
         <td>
            <a href="javascript:void(document.forms['aModifyForm'].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['aModifyForm'].submit())"><img name="applyChanges" border=0 alt="Apply Changes" src="/gfx/but/but_apply_chg_0.gif"></a>
         </td>
      <tr>
      </form>
      |;
   }
   
   
   print qq|
   <form name="aAddForm" method=POST>
      <tr><td colspan=2><hr noshade></td></tr>

      <tr>
         <input type=hidden name=dn value="$dn">
         <input type=hidden name=mode value="addarecord">
         <td><input type=text name=host value=''></td>
         <td><input  type=text name=ip></td>
      </tr>
      <tr>
         <td>&nbsp;</td>
         <td>         <a href="javascript:void(document.forms['aAddForm'].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 modifyarecords {
   my $self=shift;
   my $r=shift;
   
   my (@ips, @hosts);
   for ($r->param("host")) {
      push @hosts, $_  if /\S/;
   }
   for ($r->param("ip")) {
      push @ips, $_  if  /\S/;
   }
   
   my @records=map{"$hosts[$_], $ips[$_]"}0..$#hosts;
   
   
   $r->param("record", \@records);
   $self->{'ldap'}->updateEntry($r);

   $self->editarecords($r);
}

sub addarecord {
   my $self=shift;
   my $r=shift;
   my $record=join ',', ($r->param("host"), $r->param("ip"));
   $r->param("record", $record);

   if ($r->param("host")){
      $self->{'ldap'}->addEntry($r);
   }
   $self->editarecords($r);
}

1;
__END__

