<%doc>

-- HOST tasks --

</%doc>

<%attr>
title => 'DNS Record Tasks' 
</%attr>
%
%
%#######################################################################
%#
%# Args section
%#
%#######################################################################
<%args>
$user           => $ui->get_current_user($r)
$view           =>'search'
$action         => undef
$search         => undef
$submit         => undef
$hostname       => undef
$show_tasks     => undef
$showheader     => 1
$ipblock_id     => undef
$ip             => undef
$add_host_block => undef
$ethernet       => undef
$zone_id        => undef
</%args>
%
%
%
%#######################################################################
%#
%# INIT section
%#
%#######################################################################
%
<%init>
my $DEBUG = 0;
print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' if $DEBUG;

$show_tasks = $show_tasks || $user->getAttribute("SHOW_TASKS");
if ( $show_tasks eq "" ) {
    $user->setAttribute($r, "SHOW_TASKS", "show");
    $show_tasks = "show";
}
*print_showtaskslink = $m->comp('SELF:.sub_print_showtaskslink'); 
my $hideheader = 'style="display:none"' if ( !$showheader );

my $IPV4 = Netdot->get_ipv4_regex();
my $IPV6 = Netdot->get_ipv6_regex();
my $MAC  = Netdot->get_mac_regex();
my @list;

my @all_zones = Zone->retrieve_all();

my $manager = $ui->get_permission_manager($r);
</%init>

<div id="sectiontools" <% $hideheader %>>
  <div class="container">
    <div class="containerheadleft">DNS Record Tasks</div>
    <div class="containerheadright">

%   if ( $manager && $manager->can($user, 'access_admin_section', 'host_tasks:new') ){
%       if ( $view eq 'search' ){ 
	    <a href="host_tasks.html?view=new">[new]</a>
%       }elsif ( $view eq 'new' ){
            <a href="host_tasks.html?view=search">[search]</a>
%       }
%   }
%   print_showtaskslink($show_tasks);
    </div>
    <div class="containerbody" id="tasks" style="display:<% ($show_tasks eq "show"?"block":"none") %>">
        <table><tr><td>

%    if ( $view eq 'search' ){
        <fieldset class="medium">
            <legend>Search</legend>
            <form action="host_tasks.html" method="POST">
                <p>
                <label for="Find">Name/IP/MAC:</label>
                <input type="hidden" name="action" value="search">
                <input type="text" name="search" class="longtxt" value="<% $search %>"> 
                </p>
                <p>
                <label for="Select">Names within:</label>
                <select name="zone_id">
		<option value="0">All Zones</option>
%               foreach my $zone ( sort { $a->name cmp $b->name } @all_zones ){
%                   if ( $zone->id eq $zone_id ){
                        <option value="<% $zone->id %>" SELECTED><% $zone->get_label %></option>
%                   }else{
                        <option value="<% $zone->id %>"><% $zone->get_label %></option>
%                   }
%               }
                </select>
                <input name="submit" value="Find" class="btn" type="submit">
                </p>
            </form>
        </fieldset>
%     }elsif ( $view eq 'new' ){
        <fieldset class="large">
            <legend>Add New Host</legend>
            <form action="host_tasks.html" method="POST">
                <p>
                <label for="Name">Name:</label>
                <input name="hostname" type="text" class="longtxt" value="">
                </p>

                <p>
                <label for="Name">Ethernet:</label>
                <input name="ethernet" type="text" class="txt" value=""> (optional)
                </p>

                <p>
                <label for="Subnet">Subnet:</label>
                <select name="add_host_block">
                <option value="0">- Select Block -</option>
%           my @subnets = grep { $_->forward_zone } Ipblock->search(status=>"Subnet");
%           foreach my $o ( sort { $a->address_numeric cmp $b->address_numeric } @subnets ){
%               my $subnet_label = $o->get_label();
%               if ( $o->description ){
%                   $subnet_label .= " (".$o->description.")"; 
%               }
                <option value="<% $o %>"><% $subnet_label %></option>
%           }
                </select>
                </p>

                <p>
                <input name="submit" value="Add" class="btn" type="submit">
                </p>
                <input type="hidden" name="action" value="new">
            </form>
        </fieldset>
%   }
        </td></tr></table>
    </div> <!-- close containerbody -->
  </div> <!-- close container -->
</div> <!-- close sectiontools -->




<%perl>
#######################################################################################
# Search
#
#######################################################################################

if ( $action eq "search" && $submit ){
    # Remove trailing and leading spaces
    my %idx;
    if ( $search ){
	# Remove trailing and leading spaces
	$search = $ui->rem_lt_sp($search);
	if ( $search =~ /$MAC/ ){
	    @list = PhysAddr->search(address=>$search);
	}elsif ( $search =~ /^($IPV4)$|^($IPV6)$/ ){
	    @list = Ipblock->search(address=>$search);
	}else{
	    my %args = (name=>$search);
	    $args{zone} = $zone_id if ( $zone_id );
	    @list = RR->search_like(%args);
	}
	# Display search results
	if ( @list ){
	    if ( scalar @list == 1 ){
		my $obj = $list[0];
		my $class = $obj->short_class;
		if ( $class eq 'RR' ){
		    $m->comp('/management/host.html', id=>$obj->id);
		}elsif ( $class eq 'Ipblock' ){
		    $m->comp('/management/ip.html', id=>$obj->id);
		}elsif ( $class eq 'PhysAddr' ){
		    $m->comp('/management/mac.html', id=>$obj->id);
		}
	    }else{
		print '<div class="container">';
		print '<div class="containerhead">';
		print "Query <em>$search</em> returned: ".scalar(@list)." matches";
		print "</div>";
		print '<div class="containerbody">';
		$m->comp('/generic/sortresults.mhtml', object=>\@list, withedit=>1);
		print "</div>\n</div>";
	    }
	} else {
	    $m->comp('/generic/no_search_results.html', search=>$search);
        }
    } else {
	$m->comp('/generic/no_search_criteria.html');
    }

#######################################################################################
# Edit single IP
#
#######################################################################################

} elsif ( $action eq "edit" && $ipblock_id) {
    $m->comp('/management/host.html', ipblock=>$ipblock_id, edit=>1);


#######################################################################################
# New host
#
#######################################################################################

} elsif ( $action eq "new" ) {
    if ( !defined $add_host_block || $add_host_block == 0 ) {
	$m->comp('/generic/error.mhtml', error=>"You need to specify a subnet");
    }
    if ( !(defined $hostname) ) {
	$m->comp('/generic/error.mhtml', error=>"You need to specify a hostname");
    }
    my $block = Ipblock->retrieve($add_host_block);
    unless ( $block ){
	$m->comp('/generic/error.mhtml', error=>"Could not retrieve Ipblock: $add_host_block");
    }
    
    my $ip_strategy = Netdot->config->get("IP_ALLOCATION_STRATEGY");
    my $address;
    unless ( $address = $block->get_next_free(strategy=>$ip_strategy) ){
	$m->comp('/generic/error.mhtml', error=>"Could not get an available address from ".$block->get_label);
    }


    my %args;
    $args{block}    = $block;
    unless ( $args{zone} = $block->forward_zone() ){
	$m->comp("/generic/error.mhtml", error=>"Subnet ".$block->get_label." is not associated with a zone.");
    }
    
    $args{address}  = $address;
    $args{hostname} = $hostname;
    $args{ethernet} = $ethernet;
    
    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);
    }

}


</%perl>

