<%doc>

-- Add Host form --

</%doc>
%
<%attr>
title  => 'Add Host' 
</%attr>
%
%
%#######################################################################
%#
%# Args section
%#
%#######################################################################
<%args>
$add_host_block
$user          => $ui->get_current_user($r)
$zone_id       => undef
$hostname      => 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
$submit        => undef
$contact_name  => undef
$contact_email => undef
$contact_phone => 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);
}else{
    $zone = $block->forward_zone();
}

if ( $submit ){

    # Obtain next available address
    my $address;
    my $ip_strategy = Netdot->config->get("IP_ALLOCATION_STRATEGY");
    eval {
	$address = $block->get_next_free(strategy=>$ip_strategy);
    };
    if ( my $e = $@ ){
	$m->comp('/generic/error.mhtml', error=>"$e");	
    }
    unless ( $address ){
	$m->comp('/generic/error.mhtml', error=>"Could not get an available address from ".$block->get_label);
    }

    unless ( $zone ){
	$m->comp('/generic/error.mhtml', error=>"A DNS Domain (Zone) is required");
    }
    
    unless ( $hostname ){
	$m->comp('/generic/error.mhtml', error=>"A hostname is required");
    }
    
    eval {
	RR->validate_name($hostname);
    };
    if ( my $e = $@ ){
	$m->comp('/generic/error.mhtml', error=>$e);
    }

    if ( my $h = RR->search(name=>$hostname, zone=>$zone)->first ){
	$m->comp('/generic/error.mhtml', error=>$h->get_label." is already taken");
    }

    # We do not want two host scopes with the same physical address in the same subnet
    if ( $ethernet ){
	if ( my $physaddr = PhysAddr->search(address=>$ethernet)->first ){
	    if ( my @host_scopes = $physaddr->dhcp_hosts ){
		foreach my $host ( @host_scopes ){
		    if ( $host->ipblock->parent && $host->ipblock->parent->id == $block->id ){
			my $hname = $host->ipblock->address;
			my $ipid  = $host->ipblock->id;
			$m->comp('/generic/error.mhtml', error=>"Host <strong><a href=\"../management/host.html?ipblock=$ipid\">$hname</a></strong> is already registered with Ethernet: $ethernet in this subnet");
		    }
		}
	    }
	}
    }
    my $person = $ui->get_user_person($user);

    my %args;
    if ( $aliases ){
	my @aliases = split ',', $aliases;
	foreach my $alias ( @aliases ){
	    $alias =~ s/\s+//g;
	    # In case they included the domain part in the alias
	    my $domain = $zone->name;
	    $alias =~ s/\.$domain$//;
	    if ( my $h = RR->search(name=>$alias, zone=>$zone)->first ){
		$m->comp('/generic/error.mhtml', error=>$h->get_label." from your aliases list is already taken");
	    }
	    eval {
		RR->validate_name($alias);
	    };
	    if ( my $e = $@ ){
		$m->comp('/generic/error.mhtml', error=>"Invalid Alias $alias: $e");
	    }
	    push @{$args{aliases}}, $alias;
	}
    }
    
    foreach my $key qw(hostname expiration ethernet) {
	$args{$key} = $ARGS{$key};
    }
    $args{zone}    = $zone;
    $args{person}  = $person;
    $args{block}   = $block;
    $args{address} = $address;

    my @text_records;

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

    # Location
    if ( $room_id || $site_id || $site_name ){
	my $txtdata;
	if ( my $room = Room->retrieve($room_id) ){
	    $txtdata = "LOC: ".$room->get_label if $room;
	    # the room label includes the site name
	}elsif ( $room_number ){
	    $txtdata = "LOC: ".$room_number;
	    if ( my $site = Site->retrieve($site_id) ){
		$txtdata .=  " ".$site->get_label;
	    }elsif ( $site_name ){
		$txtdata .=  " ".$site_name;
	    }
	}else{
	    if ( my $site = Site->retrieve($site_id) ){
		$txtdata =  $site->get_label;
	    }elsif ( $site_name ){
		$txtdata =  $site_name;
	    }
	}
	push @text_records, $txtdata if ( $txtdata );
    }
    
    # Contacts
    if ( $person ){
	# Add the current user as a contact
	my $txtdata = "CON: ".$person->get_label;
	$txtdata .= " (".$person->email.")" if $person->email; 
	$txtdata .= ", ".$person->office if $person->office;
	push @text_records, $txtdata if ( $txtdata );
    }
    
    # Add additional contact info
    if ( $contact_name ){
	my $txtdata = "";
	$txtdata =  "CON: ".$contact_name;
	$txtdata .= " (".$contact_email.")" if $contact_email; 
	$txtdata .= ", ".$contact_phone if $contact_phone; 
	push @text_records, $txtdata if ( $txtdata );
    }
    $args{text_records} = \@text_records if @text_records;
    
    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');

<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">
    <script language="javascript" src="../java_script/datechooser.js"></script>
    <input type="hidden" name="add_host_block" value="<% $add_host_block %>">
    <table><tr><td>
    <p>Required fields are marked with an asterisk (<font color="red">*</font>).<br>
    <p>Help is available for some fields by clicking on the field name.<br>
    <p><br>
    <fieldset class="large">
        <legend>Host Identification</legend>
        <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_domain = Netdot->config->get('DEFAULT_DNSDOMAIN');
%           my $default_zone = (Zone->search(name=>$default_domain))[0];
%	    # 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) ){
%	                push @zones, $zone;
%	            }
%               }
%           }
            <option value="0">-- Select --</option>
%           foreach my $z ( sort { $a->get_label cmp $b->get_label } @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="hostname" class="longtxt" value="">
        </p>
        <p>
        <label for="ethernet"><% $ui->help_link('../user_help/hosts.html#Ethernet', '', '', 'Ethernet Address') %></label>
        <input type="text" name="ethernet" class="ethertxt" 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="0">-- 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="0">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="0">-- 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="0">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 @all_sites = Site->retrieve_all();
        <option value="0">-- Select --</option>
%       foreach my $site ( sort { $a->get_label cmp $b->get_label } @all_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="0">-- 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 -->

% }
