<%doc>

-- Config tasks --

</%doc>
%
<%attr>
title => 'Config Tasks' 
</%attr>
%
%
%#######################################################################
%#
%# Args section
%#
%#######################################################################
<%args>
@config_types   => undef
$user           => $ui->get_current_user($r)
$submit         => undef
$showheader     => 1
$hideheader     => undef
@zones          => undef
$bind_force     => undef
$dhcpd_force    => undef
$bind_no_priv   => undef
@scopes         => undef
</%args>
%
%
%
%#######################################################################
%#
%# INIT section
%#
%#######################################################################
%
<%init>
use Netdot::Exporter;
my $DEBUG = 0;
print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' if $DEBUG;
my @types = qw /Nagios Sysmon Rancid Smokeping BIND DHCPD/;
my $logstr;
my $manager = $ui->get_permission_manager($r);
</%init>


% unless ( $manager && $manager->can($user, 'access_admin_section', 'Export:Configurations') ){
    $m->comp('/generic/error.mhtml', error=>"You don't  have permission to access this section");
% }
<form action="config_tasks.html" method="POST">
<div id="sectiontools" <% $hideheader %>>
<div class="container">
    <div class="containerhead">
        Export third party configurations from Netdot
    </div>
    <div class="containerbody" id="tasks">
    <p><br>
    <table>
     <tr>
      <td>
        <fieldset class="small">
            <legend>Export Classes</legend>
                <p>
                <label>Select one or more of:</label>
                <select name="config_types" size="6" MULTIPLE>
%               foreach my $type ( @types ){
        	    <option value="<% $type %>"><% $type %></option> 
%               } 
                </select>
                </p>
        </fieldset>
        <fieldset class="small">
            <legend>BIND Options</legend>
                <p>
                <label>Select specific zones</strong> (default is ALL):</label>
                <select name="zones" size="6" MULTIPLE>
%               foreach my $zone ( Zone->retrieve_all ){
        	    <option value="<% $zone %>"><% $zone->name %></option> 
%               } 
                </select>
                </p>
                <p>
                   <input type="checkbox" name="bind_force">
                   <label for="bind_force">Force export even if no pending changes</label>
                </p>
                <p>
                   <input type="checkbox" name="bind_no_priv" CHECKED>
                   <label for="bind_no_priv">Exclude Private Data (HINFO and TXT records)</label> 
                </p>
        </fieldset>
        <fieldset class="small">
            <legend>DHCPD Options</legend>
                <p>
                <label>Select specific scopes</strong> (default is ALL):</label>
                <select name="scopes" size="6" MULTIPLE>
%               foreach my $scope ( DhcpScope->search(type=>'global') ){
        	    <option value="<% $scope->name %>"><% $scope->name %></option> 
%               } 
                </select>
                </p>
                <p>
                   <input type="checkbox" name="dhcpd_force">
                   <label for="dhcpd_force">Force export even if no pending changes</label>
                </p>
        </fieldset>
     </td>
    </tr>
    </table>
    <input type="submit" name="submit" class="btn" value="Export" >
</form>

<%perl>
if ( $submit ){
    unless ( $manager && $manager->can($user, 'access_admin_section', 'Export:Submit_Configuration') ){
	$m->comp('/generic/error.mhtml', error=>"You don't  have permission to perform this operation")
    }

    $m->comp('/generic/error.mhtml', error=>"Please select an Export type")
	unless ( $ARGS{config_types} );

    my $logger = Netdot->log->get_logger('Netdot::Exporter');
    if ( $logstr = Log::Log4perl::appender_by_name('config_tasks.html') ){
	Log::Log4perl->eradicate_appender('config_tasks.html');
    }
    $logstr = Netdot::Util::Log->new_appender('String', name=>'config_tasks.html');
    $logger->add_appender($logstr);

    my $dhcp_logger = Netdot->log->get_logger('Netdot::Model::DHCP');
    $dhcp_logger->add_appender($logstr);

    foreach my $type ( @config_types ){
	my %args;
	if ( $type eq 'BIND' ){
	    $args{zone_ids} = \@zones if ( scalar @zones && $zones[0] ne "" );
	    $args{force}    = 1 if ($bind_force);
	    $args{nopriv}   = 1 if ($bind_no_priv);
	}elsif ( $type eq 'DHCPD' ){
	    $args{force}  = 1 if ($dhcpd_force);
	    $args{scopes} = \@scopes if @scopes;	    
	}
	eval {
	    my $exporter = Netdot::Exporter->new(type=>$type);
	    $exporter->generate_configs(%args);
	};
	if ( my $e = $@ ){
	    $m->comp('/generic/error.mhtml', error=>$e);
	}
    }

    my $log = $logstr->string() ;
    if ( $log ){
	print "<p><br>";
	print "<strong>Exporter Output:</strong> <br>";
	print "<p><pre>";
	print "$log";
	print "</pre> <br />";
    }
}
</%perl>
 
    </div> <!-- close containerbody -->
</div> <!-- close container -->
</div> <!-- close sectiontools -->


<%cleanup>
    if ( defined $logstr ){
        Log::Log4perl->eradicate_appender('config_tasks.html')
    }
</%cleanup>
