package ISPMan::DNSMan::MXrecords;

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

require Exporter;

@ISA = qw(Exporter AutoLoader);
@EXPORT = qw(
editmxrecords
addmxrecord
modifymxrecords
);
$VERSION = '0.01';

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


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

   my @records=$entry->get_value("record");
   print  $r->param('domain'), "::MX Records<br><hr noshade>";
   print qq|
      <form method=POST name="mxModifyForm">
      <input type=hidden name=dn value="$dn">
      <input type=hidden name=mode value="modifymxrecords">
   |;

   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>Host</b></font>
      </th>
      <th>
         <font face="Helvetica, Arial" color="#666699" size=3><b>Priority</b></font>
      </th>
   </tr>
   |;
   for (@records) {
        my ($origion, $pref, $host)=split(/\s*,\s*/, $_);
        print qq|
        <tr>
         <td><font face="Helvetica, Arial" color="#666699" size=3><input type=text name=origion value='$origion'></font></td>
           <td><font face="Helvetica, Arial" color="#666699" size=3><input type=text name=host value='$host'></font></td>
         <td><font face="Helvetica, Arial" color="#666699" size=3><input size=5 type=text name=pref value='$pref'></font></td>
      </tr>
      |;
   }
   
   print qq|
   <tr>
      <td>
      <a href="javascript:void(document.forms['mxModifyForm'].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['mxModifyForm'].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="mxAddForm" method="POST">
      <tr>
         <td colspan=2>
            <font face="Helvetica, Arial" color="#666699" size=3>Add new record
         </td>
   </tr>

      <tr>
         
      <input type=hidden name=dn value="$dn">
      <input type=hidden name=mode value="addmxrecord">
   
         <td><font face="Helvetica, Arial" color="#666699" size=3><input type=text name=origion value=''></font></td>
         <td><font face="Helvetica, Arial" color="#666699" size=3><input type=text name=host value=''></font></td>
         <td><font face="Helvetica, Arial" color="#666699" size=3><input size=5 type=text name=pref value=''></font></td>
      </tr>
      <tr>
         <td>&nbsp;</td>
            <td align=right>
            <a href="javascript:void(document.forms['mxAddForm'].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 addmxrecord {
   my $self=shift;
   my $r=shift;
   my $dn=$r->param("dn");
   
   my $record=join ',', ($r->param("origion"), $r->param("pref"), $r->param("host"));
   $r->param("record", $record);
   $self->{'ldap'}->addEntry($r);
   $self->editmxrecords($r);
}



sub modifymxrecords {
   my $self=shift;
   my $r=shift;
   my $dn=$r->param("dn");
   
   my (@hosts, @prefs, @origions);

   for ($r->param("host")) {
      push @hosts, $_  if $_;
   }
   
   for ($r->param("origion")) {
      $_||='@';
      push @origions, $_ ;
   }
   for ($r->param("pref")) {
      $_||=10;
      push @prefs, $_  if $_;
   }
   

   my @records=map{"$origions[$_], $prefs[$_], $hosts[$_]"}0..$#hosts;
   $r->param("record", \@records);
   $self->{'ldap'}->updateEntry($r);
   $self->editmxrecords($r);

}



1;
__END__

