<%doc>

 HTML form for access rights (special case of edit)

</%doc>

<%args>
$table        => undef
$id           => undef
$select_id    => undef
$selected     => undef
$showheader   => undef
$dowindow     => 0
$object_class => undef
@object_id    => undef
$person       => undef
$contactlist  => undef
@access       => undef
$submit       => undef
$return_comp  => undef
$return_id    => undef
$return_view  => undef
%args         => undef
</%args>

<%init>
my $DEBUG = 0;

# Consolidate args variables
foreach my $arg ( %args ){
    $ARGS{$arg} = $args{$arg};
}

my %anames = $ui->get_access_names();

my $title = ($table)? "$table " . $ui->table_descr_link($table, "[?]") : 'Add';

print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' if $DEBUG;
if ( $submit ){
    if ( !defined($access[0]) ){
        $m->comp('error.mhtml', error=>"No rights were selected, please select one or more rights to grant");
    }

    if( !$object_id[0] ){
        $m->comp('error.mhtml', error=>"No objects were selected, please select objects to grant rights on");        
    }

    if( !$object_class ){
        $m->comp('error.mhtml', error=>"No object class was selected, please select an object class");
    }
    eval {
	if ( $object_class && $object_id[0] ){

	    foreach my $object_id ( @object_id ){
		foreach my $access ( @access ){
            	    my $accessright = AccessRight->find_or_create({object_class => $object_class, 
								   object_id    => $object_id, 
								   access       => $access});
		    if ( $table eq 'Person' ){
			UserRight->find_or_create({accessright=>$accessright, person=>$id});
		    }elsif ( $table eq 'ContactList' ){
			GroupRight->find_or_create({accessright=>$accessright, contactlist=>$id});
		    }
		}
	    }
	}
    };
    if ( my $e= $@ ){
        $m->comp('error.mhtml', error=>$e);
    }
}

</%init>

<%perl>
if( $id ) {
    if ( $return_comp && $return_id ){
	$m->comp($return_comp, id=>$return_id, view=>$return_view); 
    }else{
	if ( $table eq 'Person' ){
	    $m->comp('view.html', table=>$table, id=>$id);
	}else{
	    $m->comp('contactlist.html', id=>$id);
	}
    }
}else{
    if ( !$showheader ){
	$m->clear_buffer;
</%perl>
<html>
  <head>
      <link rel="stylesheet" href="<% $r->dir_config('NetdotPath') %>css/style.css" type="text/css">
  </head>      
% my $js_prefix = $r->dir_config('NetdotPath') . "java_script";
% for my $script ( qw(select jsrsClient) ){
  <script language="JavaScript" src="<% $js_prefix %>/<% $script %>.js"></script>
% }
  <body class="bodytag">
%}

<script language="javascript">
<!--
/* 
 * Set the rights list based on the value of Object Class
 * 
*/
function setPerms(){
    classSel = document.getElementById('object_class');
    permsSel = document.getElementById('access');
    permsSel.options.length = 0;
    if ( classSel.value == 'Ipblock' ){
	permsSel.options[0] = new Option('View',      'view');
	permsSel.options[1] = new Option('Edit',      'edit');
	permsSel.options[2] = new Option('Delete',    'delete');
	permsSel.options[3] = new Option('Choose IP', 'choose_ip');
	permsSel.options[4] = new Option('None',      'none');
    }else{
	permsSel.options[0] = new Option('View',      'view');
	permsSel.options[1] = new Option('Edit',      'edit');
	permsSel.options[2] = new Option('Delete',    'delete');
	permsSel.options[3] = new Option('None',      'none');
    }
}

-->
</script>

<div class="container">
    <div class="containerhead">
        <% $title %>
    </div>
    <div class="containerbody">
   
      <form name="netdotform" action="access_right_form.html" method="POST" enctype="multipart/form-data">
      <input type="hidden" name="showheader" value="<% $showheader %>"> 
      <input type="hidden" name="select_id" value="<% $select_id %>">
      <input type="hidden" name="selected" value="<% $selected %>">
      <input type="hidden" name="dowindow" value="<% $dowindow %>">
      <input type="hidden" name="return_comp" value="<% $return_comp %>">
      <input type="hidden" name="return_id" value="<% $return_id %>">
      <input type="hidden" name="return_view" value="<% $return_view %>">

<%perl>

my (@field_headers, @cell_data);
if ( $ARGS{person} ){
    my $person = Person->retrieve($ARGS{person});
    push @field_headers, 'User:';
    push @cell_data, $person->get_label;
    print "<input type=\"hidden\" name=\"table\" value=\"Person\">";
    print "<input type=\"hidden\" name=\"id\" value=\"$person\">";

}elsif ( $ARGS{contactlist} ){
    my $cl = ContactList->retrieve($ARGS{contactlist});
    push @field_headers, 'Contact List:';
    push @cell_data, $cl->get_label;
    print "<input type=\"hidden\" name=\"table\" value=\"ContactList\">";
    print "<input type=\"hidden\" name=\"id\" value=\"$cl\">";

}else{
    push @field_headers, 'Subject Type:';
    push @cell_data, ('<select name="table" id="table" '.
		      'onChange="jsrsSendqueryRA(this.options[selectedIndex].value, \'id\')">'.
		      '<option value="0" SELECTED>-- Select --</option>'.
		      '<option value="Person">Person</option>'.
		      '<option value="ContactList">Contact List</option>'.
		      '</select>');
    push @field_headers, 'Name:';
    push @cell_data, ('<select name="id" id="id">'.
		      '<option value="0" SELECTED>-- Select Type --</option>'.
		      '</select>'.
		      '<input type="hidden" name="keywords" id="keywords" value="Keywords" '.
		      'onFocus="if (this.value == \'Keywords\') {this.value = \'\';} return true;">'.
		      '<input type="hidden" name="button" id="button" value="Search" '.
		      'onClick="jsrsSendquery(document.getElementById(\'table\').value, \'id\', '.
		      'document.getElementById(\'keywords\').value)">'
    );
}

push @field_headers, 'Object Class:';
if ( $object_class ){
    push @cell_data, 
    &{sub{
	my $out = $object_class;
	$out .=  "<input type=\"hidden\" name=\"object_class\" value=\"$object_class\">";
      }};
}else{
    my %classes = AccessRight->get_classes();
    push @cell_data, 
    &{sub{
	my $out;
	$out .= "<select name=\"object_class\" id=\"object_class\" ".
	    "onChange=\"jsrsSendqueryRA(this.options[selectedIndex].text, 'object_id');setPerms()\">";
	$out .= "<option value=\"0\">-- Select --</option SELECTED>";
	foreach my $class ( sort keys %classes ){
	    $out .= "<option name=\"$class\" >$class</option>";
	}
	$out .= "</select>";
      }};
}
push @field_headers, 'Object(s):';
if ( $object_id[0] ){
    push @cell_data, 
    &{sub{
	my $out = join(', ', map { $object_class->retrieve($_)->get_label } @object_id);
	foreach my $oid ( @object_id ){
	    $out .=  "<input type=\"hidden\" name=\"object_id\" value=\"$oid\">";
	}
	$out;
      }};
}else{
    push @cell_data,
    &{sub{
	my $out;
	$out .= "<select name=\"object_id\" id=\"object_id\" MULTIPLE>";
	$out .= "<option value=\"0\">Select Class</option></select><br>";
	$out .= "<input type=\"hidden\" name=\"keywords\" id=\"keywords\" value=\"Keywords\" ";
        $out .= "onFocus=\"if (this.value == 'Keywords') {this.value = '';} return true;\">";
	$out .= "<input type=\"hidden\" name=\"button\" id=\"button\" value=\"Search\" ";
	$out .= "onClick=\"jsrsSendquery(document.getElementById('object_class').value, ";
	$out .= "'object_id', document.getElementById('keywords').value);\" >";
      }};
    
}
push @field_headers, 'Rights:';
my $txt = '<select id="access" name="access" MULTIPLE>';
if ( $object_class ){
    # The js code will not work in this case, so we just generate the 
    # select options
    my @values = ('view', 'edit', 'delete');
    if ( $object_class eq 'Ipblock' ){
	push @values, 'choose_ip';
    }
    push @values, 'none';
    foreach my $v ( @values ){
	$txt .= '<option value="'.$v.'">'.$anames{$v}.'</option>';
    }
}
$txt .= '</select>';
push @cell_data, $txt;

</%perl>
	  <& /generic/attribute_table.mhtml, field_headers=>\@field_headers, data=>\@cell_data, 
 	                                     width=>"1", headercolwidth=>"15%", datacolwidth=>"35%" &>

      <br>
      <input name="submit" type="submit" value="Insert">

%    if ( $dowindow ){
	<input name="cancel" value="Cancel" onClick="window.close()" type="button">
%    }else{
    	<input name="cancel" value="Cancel" onClick="history.go(-1)" type="button">
%    }
      </form>
    </div>
</div>

%     if ( $showheader ){
         </div>
%     }else{
          </body>
          </html>
%     }
% } 


