<%doc>
In a separate window, allow user to modify one of the fields in Ipblock recursively (or not)
Some restrictions apply regarding
which fields can be updated in a recursive way.
</%doc>
%
<%flags>
inherit => undef 
</%flags>
%
<%args>
$id
$field
$recursive => 0
$submit    => undef
$cancel    => undef
</%args>
%
<%init>
my %state;
my %reserved = (submit => "", cancel => "");
my $msg;
my (@field_headers, @cell_data) = ();

my $mtable   = $ui->meta->get_table('Ipblock');    
my %linksto  = $mtable->get_links_to();
my $mcol     = $mtable->get_column($field);
my $fieldtag = $mcol->tag();

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

if ( $submit ){
    foreach my $arg ( keys %ARGS ){
	next if ( exists $reserved{$arg} );
	if ($arg =~ /^(\w+)__(\w+)__(\w+)$/){
	    $state{$3} = $ARGS{$arg};
	}
    }
    $state{recursive} = $recursive;
    eval {
	$o->update(\%state);
    };
    if ( my $e = $@ ){
	$m->comp('/generic/error.mhtml', error=>$e);
    }else{
	$msg = 'OK.';
 	if ( $recursive ){
 	    $msg .= ' All blocks updated successfully.';
 	}
	$msg .= ' You might need to refresh the parent window to see the new values.'; 
    }

}else{
    push( @field_headers, "$fieldtag:" );
    push( @cell_data, $ui->form_field(object=>$o, column=>$field, edit=>1, returnValOnly=>1) );

    push( @field_headers, "Options:" );
    push( @cell_data, 'Apply to all descendants <input type="checkbox" name="recursive"'.($recursive?' CHECKED':'').'>' );
}
my $netdot_path = $r->dir_config('NetdotPath');
</%init>

<html>
<head>
    <link rel="stylesheet" href="<% $netdot_path %>css/style.css" type="text/css">
    <title>Edit IP block Recursively</title>
</head> 
<script language="JavaScript" src="<% $netdot_path %>java_script/select.js"></script>
<script language="JavaScript" src="<% $netdot_path %>java_script/insertentry.js"></script>
<script language="JavaScript" src="<% $netdot_path %>java_script/jsrsClient.js"></script>

<body class="bodytag">

<div class="container">

%if ( $submit ){
  <div class="containerhead">
      <strong>Message</strong>   
  </div>

  <div class="containerbody">
      <% $msg %>
      <p><a href="#" onClick="window.close();">[close]</a><br>
    
%}else{

  <div class="containerhead">Edit <% $fieldtag %></div>

  <div class="containerbody">
    <form action="ip-rec.html" method="POST">
      <input type="hidden" name="id" value="<% $id %>">
      <input type="hidden" name="field" value="<% $field %>">

    <& /generic/attribute_table.mhtml, field_headers=>\@field_headers, data=>\@cell_data &>
    <p>
    <center>
    <input type="submit" name="submit" value="Submit">
    <input type="button" value="Cancel" onClick="window.close()">
    </center>
   </form>

% } #else

    </div> <!-- close containerbody -->
</div> <!-- close container -->
 
</body>
</html>
