<%doc>

-- Add Host form --

</%doc>
%
<%attr>
title  => 'Add Host' 
</%attr>
%
%
%#######################################################################
%#
%# Args section
%#
%#######################################################################
<%args>
$add_host_block
$address       => undef # Can be passed optionally
$user          => $ui->get_current_user($r)
$zone_id       => undef
$name          => undef
$aliases       => undef
$expiration    => undef
$cpu           => undef
$other_cpu     => undef
$os            => undef
$other_os      => undef
$site_id       => undef
$site_name     => undef
$room_id       => undef
$room_number   => undef
$ethernet      => undef
$duid          => undef
$submit        => undef
$contact_name  => undef
$contact_email => undef
$contact_phone => undef
$v6_assign     => undef
</%args>
%
%
%
%#######################################################################
%#
%# INIT section
%#
%#######################################################################
%
<%init>
my $DEBUG = 0;
print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' if $DEBUG;
my $manager = $ui->get_permission_manager($r);

my $block = Ipblock->retrieve($add_host_block);
unless ( $block ){
    $m->comp('/generic/error.mhtml', error=>"Could not retrieve Ipblock: $add_host_block");
}

unless ( $manager && $manager->can($user, "edit", $block) ){
    $m->comp('/generic/error.mhtml', error=>"You don't have permission to edit ".$block->get_label);
}

my $zone;
if ( $zone_id ){
    $zone = Zone->retrieve($zone_id);
}

if ( $submit ){
    unless ( $zone ){
	$m->comp('/generic/error.mhtml', error=>"A DNS Domain (Zone) is required");
    }
    unless ( $name ){
	$m->comp('/generic/error.mhtml', error=>"A hostname is required");
    }
    
    my %args; # For RR->add_host()
    foreach my $key ( qw(name expiration ethernet duid aliases) ){
	$args{$key} = $ARGS{$key};
    }

    # Hostname validation
    if ( $args{name} ) {
	eval {
	    RR->validate_name($args{name});
	};
	if ( my $e = $@ ){
	    $m->comp('/generic/error.mhtml', error=>"$e");
	}
    }

    my $person = $ui->get_user_person($user);
    $args{person}  = $person;
    $args{zone}    = $zone;
    $args{subnet}  = $block;
    if ( $address ){
	$args{address} = $address;
    }else{
	if ( $block->version == 6 && $v6_assign eq 'manual' ){
	    if ( $ui->config->get('IPV6_ADDRESS_USER_ENTRY_HOST_ONLY') ){
		# We need to construct the address
		my %parts;
		foreach my $key ( %ARGS ){
		    if ( $key =~ /^v6_(\d+)$/o ){
			my $val = $ARGS{$key};
			if ( defined $val && $val ne "" ){
			    $parts{$1} = $val;
			}else{
			    $m->comp('/generic/error.mhtml', error=>"You must fill all the address parts");
			}
		    }
		}
		my $addr = $block->address;
		$addr =~ s/:$//;
		$addr .= join(':', map { $parts{$_} } sort { $a <=> $b } keys %parts);
		$args{address} = $addr;
	    }
	}
    }

    # Hardware Info
    $args{cpu} = $cpu || $other_cpu;
    $args{os}  = $os  || $other_os;

    # Lines to go in the info field
    my @info;

    # Location
    if ( $room_id || $room_number || $site_id || $site_name ){
	my $info;
	if ( my $room = Room->retrieve($room_id) ){
	    $info = "LOC: ".$room->get_label;
	    # the room label includes the site name
	}elsif ( $room_number ){
	    $info = "LOC: ".$room_number;
	    if ( my $site = Site->retrieve($site_id) ){
		$info .=  " ".$site->get_label;
	    }elsif ( $site_name ){
		$info .=  " ".$site_name;
	    }
	}else{
	    if ( my $site = Site->retrieve($site_id) ){
		$info =  $site->get_label;
	    }elsif ( $site_name ){
		$info =  $site_name;
	    }
	}
	push @info, $info if ( $info );
    }
    
    # Contacts
    if ( $person ){
	# Add the current user as a contact
	my $info = "CON: ".$person->get_label;
	$info .= " (".$person->email.")" if $person->email; 
	$info .= ", ".$person->office if $person->office;
	push @info, $info if ( $info );
    }
    
    # Add additional contact info
    if ( $contact_name ){
	my $info = "";
	$info =  "CON: ".$contact_name;
	$info .= " (".$contact_email.")" if $contact_email; 
	$info .= ", ".$contact_phone if $contact_phone; 
	push @info, $info if ( $info );
    }
    my $info_text = join "\n", @info;
    $args{info} = $info_text if ( $info_text );
    
    my $rr;
    eval {
	$rr = RR->add_host(%args);
    };
    if ( my $e = $@ ){
	$m->comp("/generic/error.mhtml", error=>$e);
    }else{
	$m->comp("../management/host.html", rr=>$rr);
    }
}

</%init>

% if ( !$submit ){
%    my $subnet_description = $block->get_label;
%    $subnet_description .= " (".$block->description.")" if $block->description;
%
%    my $cpu_defaults = $ui->config->get('DEFAULT_HINFO_CPU_VALUES');
%    my $os_defaults  = $ui->config->get('DEFAULT_HINFO_OS_VALUES');


<%doc>
Opens a new window showing subnet addresses to choose from
</%doc>
<script type="text/javascript">
<!--
function openChooseIP(selectObj1, selectId2, subnet) {
   var idx = selectObj1.selectedIndex; 
   var selection = selectObj1.options[idx].value;
   if ( selection == 'manual' ){
       openwindow('../management/choose_ip.html?select_id='+selectId2+'&subnet='+subnet);
   }
}

function showIPv6Input(selectObj, Layer){
   var idx = selectObj.selectedIndex; 
   var selection = selectObj.options[idx].value;
   var style = document.getElementById(Layer).style;
   if ( selection == 'manual' ){
       style.display = 'block';
   }else{
       style.display = 'none';
   }
} 
-->
</script>


<div id="sectiontools">
  <div class="container">
    <div class="containerhead">
        Add New Host in <% $subnet_description %>
    </div>
    <div class="containerbody" id="tasks">

    <form name="add_host_form" action="add_host.html" method="POST">
    <input type="hidden" name="add_host_block" value="<% $add_host_block %>">
%   if ( $address ){
         <input type="hidden" name="address" value="<% $address %>">
%   }
    <table><tr><td>
    <p>(<font color="red">*</font>) Field is required<br>
    <p>Help is available for some fields by clicking on the field name.<br>
    <p><br>
    <fieldset class="large">
        <legend>Host Identification</legend>
%       if ( $address ){
          <p>
          <label for="address">IP Address</label>
          <% $address %>
          </p>
%       }else{
%           if ( $manager && $manager->can($user, 'choose_ip', $block) ){
%               if ( $block->version == 6 ){
                  <p>
                      <label for="address">IPv6 Address</label>
                      <select id="v6_assign" name="v6_assign" onChange="showIPv6Input(this, 'ipv6_input');">
   		        <option value="automatic" SELECTED>Automatic Assignment</option>
		        <option value="manual">Manual Assignment</option>
		      </select>
                  </p>
                  <div id="ipv6_input" style="display:none">
                  <p>
                      <label for="address">&nbsp;</label>
%		      if ( $ui->config->get('IPV6_ADDRESS_USER_ENTRY_HOST_ONLY') ){
%                        my $blockaddr = $block->address;
%                        my $prefix = $block->prefix;
%			 my $num = (128 - $prefix) / 16;
%                        my @txt;
%			 for (my $i=1; $i <= $num; $i++){
%			     push @txt, '<input type="text" name="v6_'.$i.'" maxlength="4" style="width:50px" value="">';
%			 }
%                        my $txt = join ':', @txt;
%			 $blockaddr =~ s/:$//;
%                        print $blockaddr . $txt;
%                     }else{
                          <input type="text" name="address" class="longtxt" value=""> 
%                     }
                  </p>
                  </div>
%               }else{
                  <p>
                    <label for="address">IP Address</label>
                    <select id="v6_assign" name="v6_assign" onChange="showIPv6Input(this, 'ipv6_input');openChooseIP(this, 'address', '<% $block %>');">
  		      <option value="automatic" SELECTED>Automatic Assignment</option>
		      <option value="manual">Manual Assignment</option>
		    </select>
                  </p>
                  <div id="ipv6_input" style="display:none">
                  <p>
                        <label for="address">&nbsp;</label>
                        <select id="address" name="address"></select>
                  </p>
                  </div>
%               }
%	    }else{
                <p>
                    <label for="address">IP Address</label>
                    An available address will be automatically assigned.
                </p>
%           }
%       }
        <p>
        <label for="domain">Domain <font color="red">*</font></label>
%       if ( $zone ){
            <input type="hidden" name="zone" value="<% $zone->id %>">
            <% $zone->get_label %>
%       }else{
            <select name="zone_id" id="zone">
%           my $default_zone;
%	    # Show only zones that this user has access to
%           my $ao = $ui->get_allowed_objects($r, $user);
%           my %zones;
%           if ( exists $ao->{Zone} ){
%               foreach my $id ( keys %{$ao->{Zone}} ){
%	            if ( my $zone = Zone->retrieve($id) ){
%	                $zones{$id} = $zone;
%	            }
%               }
%           }
%	    if ( my $fz = $block->forward_zone ){
%               $zones{$fz->id} = $fz;
%               $default_zone = $fz;
%	    }else{
%               my $default_domain = Netdot->config->get('DEFAULT_DNSDOMAIN');
%               $default_zone = (Zone->search(name=>$default_domain))[0];
%           }
            <option value="">-- Select --</option>
%           foreach my $z ( sort { $a->get_label cmp $b->get_label } values %zones ){
%               if ( $default_zone && $default_zone->id == $z->id ){
                    <option value="<% $z->id %>" SELECTED><% $z->get_label %></option>
%               }else{
                    <option value="<% $z->id %>"><% $z->get_label %></option>
%		}
%           }
            </select>
%       }
        </p>
        <p>
        <label for="name"><% $ui->help_link('../user_help/hosts.html#Hostname', '', '', 'Host Name') %> <font color="red">*</font></label>
        <input type="text" name="name" class="longtxt" value="">
        </p>
%	if ( $block->version == 4 || 
%	     ($block->version == 6 && Netdot->config->get('DHCPD_ALLOW_ETHERNET_FOR_IPV6_HOST_DECL')) ){
            <p>
            <label for="ethernet"><% $ui->help_link('../user_help/hosts.html#Ethernet', '', '', 'Ethernet Address') %></label>
            <input type="text" name="ethernet" class="ethertxt" value="">
            </p>
%       }elsif ( $block->version == 6 ){
            <p>
            <label for="duid"><% $ui->help_link('../user_help/hosts.html#DUID', '', '', 'DUID') %></label>
            <input type="text" name="duid" class="longtxt" value="">
            </p>
%       }
        <p>
        <label for="aliases"><% $ui->help_link('../user_help/hosts.html#Cnames', '', '', 'Aliases') %></label>
        <input type="text" name="aliases" class="longtxt" value="">
        </p>
        <p>
        <label for="expiration"><% $ui->help_link('../user_help/hosts.html#Expiration', '', '', 'Expiraton Date') %></label>
	<% $ui->date_field(table=>"RR", column=>"expiration", edit=>1, shortFieldName=>1, returnAsVar=>1) %>
        </p>
    </fieldset>

    <p>
    <fieldset class="large">
        <legend>Hardware Information</legend>
        <p>
        <label for="cpu"><% $ui->help_link('../user_help/hosts.html#CPU', '', '', 'CPU') %></label>
        <select name="cpu" id="cpu">
            <option value="">-- Select --</option>
%	foreach my $key ( sort { $cpu_defaults->{$a} cmp $cpu_defaults->{$b} } keys %$cpu_defaults ){
%           my $value = $cpu_defaults->{$key};
            <option value="<% $key %>"><% $value %></option>
%       }
            <option value="">Other</option>
        </select>
        </p>
        <p>
        <label for="other_cpu">&nbsp;</label>
	<input type="text" name="other_cpu" class="txt" value=""> (if not listed)
        </p>
        <p>
        <label for="os"><% $ui->help_link('../user_help/hosts.html#OS', '', '', 'Operating System') %></label>
        <select name="os" id="os">
            <option value="">-- Select --</option>
%	foreach my $key ( sort { $os_defaults->{$a} cmp $os_defaults->{$b} } keys %$os_defaults ){
%           my $value = $os_defaults->{$key};
            <option value="<% $key %>"><% $value %></option>
%       }
            <option value="">Other</option>
        </select>
        </p>
        <p>
        <label for="other_os">&nbsp;</label>
	<input type="text" name="other_os" class="txt" value="">  (if not listed)
        </p>
    </fieldset>

    <p>
    <fieldset class="large">
        <legend>Location</legend>
        <p>
        <label for="site_id">Site</label>
        <select name="site_id" id="site_id" onChange="jsrsSendqueryNM('Room', 'room_id', this.options[selectedIndex].text);">
%	my @sites;
%       if ( $block->sites ){
%            @sites = map { $_->site } $block->sites;
%       }else{
%           @sites = Site->retrieve_all();
%       }
        <option value="">-- Select --</option>
%       foreach my $site ( sort { $a->get_label cmp $b->get_label } @sites ){
            <option value="<% $site->id %>"><% $site->get_label %></option>
%       }
        </select>
        </p>
        <p>
        <label for="site_name">&nbsp;</label>
        <input type="text" name="site_name" class="longtxt" value=""> (if not listed)
        </p>
        <p>
        <label for="room_id">Room</label>
        <select name="room_id" id="room_id">
            <option value="">-- Select --</option>
        </select>
        </p>
        <p>
        <label for="room_number">&nbsp;</label>
        <input type="text" name="room_number" class="shorttxt" value=""> (if not listed)
        </p>
    </fieldset>

    <p>
    <fieldset class="large">
        <legend>Contact (other than you)</legend>
        <p>
        <label for="contact_name">Name</label>
        <input type="text" name="contact_name" class="longtxt" value="">
        </p>
        <p>
        <label for="contact_email">Email</label>
        <input type="text" name="contact_email" class="longtxt" value="">
        </p>
        <p>
        <label for="contat_phone">Phone</label>
        <input type="text" name="contact_phone" class="txt" value="">
    </fieldset>

    <p>
    <input type="button" name="cancel_button" value="Cancel" onClick="history.go(-1);">
    <input type="submit" name="submit" value="Submit">

    </td></tr></table>
    </form>

    </div> <!-- close containerbody -->
  </div> <!-- close container -->
</div> <!-- close sectiontools -->

% }
