<%doc>

-- DNS Zone tasks --

</%doc>
%
<%attr>
title  => 'Zone Tasks' 
</%attr>
%
%
%#######################################################################
%#
%# Args section
%#
%#######################################################################
<%args>
$user           => $ui->get_current_user($r)
$action         => undef
$search         => undef
$zone_id        => undef
$zone_name      => undef
$template_zone  => undef
$submit         => undef
$show_tasks     => undef
$showheader     => 1
$view           => 'search'
</%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 @all_zones = sort { $a->name cmp $b->name } Zone->retrieve_all();

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

<div id="sectiontools" <% $hideheader %>>
  <div class="container">
    <div class="containerheadleft">
        DNS Zone Tasks
    </div>
    <div class="containerheadright">
%   if ( $manager && $manager->can($user, 'access_admin_section', 'zone_tasks:new') ){
%       if ( $view eq 'search' ){
            <a href="zone_tasks.html?view=new">[new]</a>
%       }elsif ( $view eq "new" ){
            <a href="zone_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>Find</legend>
            <form name="zone_select_form" action="zone_tasks.html" method="POST">
                <label for="Select">Zone:</label>
                <select name="zone_id" onChange="document.zone_select_form.submit();">
                <option value="">-- Select --</option>
%               foreach my $zone ( @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>
            </form>
            <p>

            <form name="zone_search_form" action="zone_tasks.html" method="POST">
                <label for="Search">Zone:</label>
                <input type="hidden" name="action" value="search">
                <input type="text" name="search" class="longtxt" value="<% $search %>"> 
                <input name="submit" value="Find" class="btn" type="submit">
            </form>
        </fieldset>
%    }elsif ( $view eq 'new' ){
%       unless ( $manager && $manager->can($user, 'access_admin_section', 'zone_tasks.html:new') ){
%	    $m->comp('/generic/error.mhtml', error => "You do not have permission to add new zones");    
%       }
        <fieldset class="medium">
            <legend>Add New Zone</legend>
            <form name="zone_add_form" action="zone_tasks.html" method="POST">
                <input type="hidden" name="action" value="new">
                <p>
                <label for="Name">Name:</label>
                <input name="zone_name" type="text" class="longtxt" value="">
		</p>
                <p>
                <label for="Template">Use template (optional):</label>
	        <select name="template_zone">
                <option value="">-- Select --</option>
%	        foreach my $zone ( @all_zones ){
                    <option value="<% $zone->id %>"><% $zone->get_label %></option>
%	        }
                </select>
		</p>
		<p>
                <input name="submit" value="Add" class="btn" type="submit">
		</p>
            </form>
        </fieldset>
%    } 

        </td></tr></table>
    </div> <!-- close containerbody -->
  </div> <!-- close container -->
</div> <!-- close sectiontools -->

<%perl>


if ( $zone_id ){
    $m->comp("zone.html", id=>$zone_id);
}

#######################################################################################
# Search
#
#######################################################################################
elsif ( $action eq "search" ) {
    if (!(defined $search)) {
	$m->comp('/generic/error.mhtml', error=>"You need to specify a zone name");
    }
    my @zones = Zone->search_like(name=>$search);
    print '<div class="container">';
    print '<div class="containerhead"><b>Zones</b></div>';
    print '<div class="containerbody">';
    if ( @zones && $zones[0]!="" ){
	$m->comp('/generic/sortresults.mhtml', object=>\@zones, withedit=>1);
    }else{
	print "No matches";
    }
    print '</div>';
    print '</div>';
}

#######################################################################################
# New zone
#
#######################################################################################
elsif ( $action eq "new" ) {
    if (!(defined $zone_name)) {
	$m->comp('/generic/error.mhtml', error=>"You need to specify a zone name");
    }

    my $zone;
    my %args = (name=>$zone_name);
    $args{template} = $template_zone if $template_zone;
    eval {
	$zone = Zone->insert(\%args);
    };
    if ( my $e = $@ ){
	$m->comp("/generic/error.mhtml", error=>$e);
    }else{
	$m->comp("zone.html", id=>$zone->id);
    }
}


</%perl>

