package ISPMan::DNSMan::CNAMES;

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

require Exporter;

@ISA = qw(Exporter AutoLoader);
@EXPORT = qw(
editcnames
addcname
modifycnames
);
$VERSION = '0.01';

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


 sub editcnames {
    my $self=shift;
    my $r=shift;
    my $dn=$r->param("dn");
    my ($entry)=$self->{'ldap'}->getEntry($dn);
    

    print  $r->param('domain'), "::CNAMES<br><hr noshade>";
    print qq|
    <table border=0 cellpadding=0 cellspacing=0>
    <tr>
       <th><font face="Helvetica, Aria" size=3>
          Alias 
       </th>
       <th><font face="Helvetica, Aria" size=3>
          Hostname 
       </th>
    </tr>\n
    |;
    
    my @records=$entry->get_value("record");
    if (@records) {
       print qq|
          <form name="cnamesModifyForm" method="POST">
          <input type=hidden name=dn value="$dn">
          <input type=hidden name=mode value="modifycnames">
       |;

       for (@records) {
            my ($alias, $host)=split(/\s*,\s*/, $_);
            print qq|
            <tr>
                <td><input  type=text name=alias value='$alias'></td>
                <td><input type=text name=host value='$host'></td>
          </tr>
          |;
       }
          print qq|
          <tr>
             <td>
                <a href="javascript:void(document.forms['cnamesModifyForm'].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['cnamesModifyForm'].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="cnamesAddForm" method="POST">
      <tr><td colspan=2><hr noshade></td></tr>

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

}


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






1;
__END__

