<%doc>

Specific page for DhcpScope objects

</%doc>

<%args>
$id               => undef
$user             => $ui->get_current_user($r)
$view             => undef
$edit             => undef
$submit           => undef
$bulk_import_data => undef
$import_overwrite => undef
@scope_type_box   => () #used for narrowing down types of scopes you're intrested in in contained_scopes
$scope_text_filter => undef #used to filter scope types based on a keyword.
</%args>

<%attr>
title   => 'View Object' 
</%attr>

<%init>
my $table = 'DhcpScope';
my $DEBUG = 0;
my($o, $prevobj );
my %ftables;

if(! $id){
    return;
}

my %explanation_hash = ( 
    'templates'        => 'When using a template, the scope will inherit all attributes of the template',
    'contained_scopes' => 'Scopes that inherit attributes from this scope.'
    );

unless ( $o = $table->retrieve($id) ){
   $m->comp('error.mhtml', error => "Nonexistent record: $id");
}
# Check if user can view this object
my $manager = $ui->get_permission_manager($r);
unless ( $manager && $manager->can($user, "view", $o) ){
    $m->comp('/generic/error.mhtml', error=>"You don't have permission to view this object");
}

my $labelstr  = $o->get_label;
my $type      = $o->type->name;
my %linksfrom = $table->meta_data->get_links_from;

# Override tags from metadata in certain cases
my %tags;
if ( $type eq 'subnet' ){
    $tags{ipblock} = 'Subnet';
}elsif($type eq 'host'){
    $tags{ipblock} = 'IP Address';
}

if ( $DEBUG ){
    print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>';
    print '%linksfrom is  <pre>', Dumper(%linksfrom), '</pre><br>';
    print '$type is '."$type <br>";
}

if ( $submit ){
    if ($edit eq 'bulk_import'){
	unless ( $manager && $manager->can($user, 'access_admin_section', 'scope:bulk_import') ){
	    $m->comp('/generic/error.mhtml', error=>"You don't have permission to perform this operation");
	}

	unless ( $bulk_import_data ){
	    $m->comp('/generic/error.mhtml', error=>"Empty import data");
	}
	# Do this atomically
	eval {
	    Netdot::Model->do_transaction(
		sub{
		    $o->import_hosts(text=>$bulk_import_data, overwrite=>$import_overwrite);
		});
	};
	if ( my $e = $@ ){
	    $m->comp('/generic/error.mhtml', error=>$e);
	}else{
	    $submit = 0; # So we display stuff below
	}
    }
}
</%init>

<div id="sectiondetail">

<!-- Display main object values -->
<div class="container">
    <div class="containerheadleft"><strong><% $table %></strong></div>
    <div class="containerheadright">
%       if ( ! $table->meta_data->is_history() ){
%           if ( $manager && $manager->can($user, 'access_admin_section', 'scope:new') ){
                <a href="edit.html?table=<% $table %>">[new]</a>
%           }
%	    if ( $manager && $manager->can($user, 'edit', $o) ){
		<a href="edit.html?table=<% $table %>&id=<% $o %>&view=<% $type %>">[edit]</a>
%	    }
%	}
%	if ( $manager && $manager->can($user, 'delete', $o) ){
            <a href="delete.html?table=<% $table %>&id=<% $o %>">[delete]</a>
%       }
        <a href="#" onClick="opentextwindow(jspopoutstring,'js','');">[text]</a>
    </div>
    <div class="containerbodyoutside">
        <& "/generic/form.mhtml", table=>$table, id=>$id, edit=>0, view=>$type, tags=>\%tags  &>
    </div>
</div>
<!-- End Display main object values -->
<!-- Display has_many possibilities-->
%  my $has_history_table = 1 if ( $table->meta_data->get_history_table_name() );
%  if ( ! $table->meta_data->is_history() && ( keys %linksfrom ) ){
  <div class="container">
	<div class="containerhead">View</div>
	   <div class="containerbody">
%	      if ( $view eq "all" ){
		  [all]
%	      }elsif ( keys %linksfrom > 1 ){
		  <a href="scope.html?id=<% $o %>&view=all">[all]</a>
%             }
%             foreach my $i ( sort keys %linksfrom ){
%               next if ($i eq 'derived_scopes' && $type ne 'template');
%               next if ($i eq 'contained_scopes' && ($type eq 'host' || $type eq 'subnet' || $type eq 'template'));
%               next if ($i eq 'audit_records' && $type ne 'global');
%               next if ($i eq 'templates' && $type eq 'template');
%               if ( $view eq $i ){
                   [<% $i %>]
%               }else{
                   <a href="scope.html?id=<% $o %>&view=<% $i %>">[<% $i %>]</a>
%               }   
%             }
%             if ( $type eq 'global' || $type eq 'group' ){
%                 if ( $manager && $manager->can($user, 'access_admin_section', 'scope:bulk_import') ){
                      <a href="scope.html?id=<% $o %>&view=bulk_import">[bulk import]</a>
%                 }
%             }
	   </div>
	</div>
  </div>
%  }
<!-- End Display has_many possibilities-->

<!-- Display has_many objects -->
<%perl>

my @all_types = DhcpScopeType->retrieve_all();  

foreach my $i ( keys %linksfrom ){
    next if ($i eq "derived_scopes" && $type ne "template");
    next if ($i eq 'contained_scopes' && ($type eq 'host' || $type eq 'subnet' || $type eq 'template'));
    next if ($i eq 'audit_records' && $type ne 'global');
    next if ($i eq 'templates' && $type eq 'template');

    if ( ($view eq 'all' || $view eq $i ) ){
        
        # Table that points to us
        my $j = (keys %{ $linksfrom{$i} })[0];
        
        # Column in the other table that points to this table
        my $ffield = $linksfrom{$i}{$j};
        # Determine if the has_many method returns ordered data from the db
        my $ffattrs = $j->meta_data->get_column($ffield)->links_to_attrs();
        my $ffield_order = $ffattrs->{order_by} if ( defined $ffattrs && exists $ffattrs->{order_by} );
        
        my @robjs;
        if($i ne 'contained_scopes' || $scope_text_filter || @scope_type_box){
            @robjs = $o->$i;
        }
        else{
           @robjs = (); #no need to preform this expensvie query if the user hasn't selected filters
        } 
            
        my $dtbl  = $j;
        my $ctbl  = $j; 
        my %args;
       

        #since the amount of returned values in contained_scopes is sometimes big, 
        #lets filter down our result list here.  Performance is better and the results
        #are more readable, but might need to figure out how to move this filtering to db
        if($i eq 'contained_scopes' && ($scope_text_filter || @scope_type_box)){
            my @tmp_row_objects;   
            foreach $r (@robjs){
                if (!@scope_type_box || grep{$_ eq $r->type->name} @scope_type_box){
                    if(!$scope_text_filter || $r->name =~ /$scope_text_filter/){
                        push(@tmp_row_objects, $r);
                    }
                }
            }
            
            @robjs = sort {$a->name cmp $b->name} @tmp_row_objects;    
        }

        my $num = scalar(@robjs);

</%perl>

        <div class="container">
%       my $title = $i;
%       $title .= " ($num)" if $num;	 
%       $title .= "&nbsp;&nbsp;&nbsp;".$explanation_hash{$i};
            <div class="containerheadleft"><% $title %></div>
	    <div class="containerheadright">
        &nbsp;
%       if ( $edit ne $i ){	    
%           if($i eq "templates"){ #we have a special edit page for templates.
%	        if ( $num ){
%                   my @temps = DhcpScopeUse->search(scope=>$o);
%                   my $space_seperated_list = "";
%                   for my $s (@temps){
%                       $space_seperated_list.=$s." "
%                   }
%	            if ( $manager && $manager->can($user, 'edit', $o) ){
                       <a href="template_edit.html?id=<% $id %>&selectall_ids=<% $space_seperated_list %>">[edit]</a>
%                   }
%	        }
%               if ( $manager && $manager->can($user, 'edit', $o) ){
                    <a href="template_edit.html?id=<% $o %>">[add]</a>
%               }
%           }else{ #for everything BUT templates:
%               if ( $manager && $manager->can($user, 'edit', $o) ){
%	            if ( $num ){
                        <a href="view.html?table=<% $table %>&id=<% $id %>&view=<% $view %>&edit=<% $i %>">[edit]</a>
%                   }
                    <a href="edit.html?table=<% $ctbl %>&<% $ffield %>=<% $o %>">[add]</a>
%               }
%           }
%       }
	    </div>



%       if($i eq 'contained_scopes'){
%           # present a form to the user allowing them to only display certian types of scopes
              <p>
              <form name = "scope_options" action="scope.html" method="POST" >
              Select Scope type(s)
              <select name = "scope_type_box">
%             foreach my $type (@all_types){                   
                  <option value="<% $type->name %>"><% $type->name %></option>
%             }
               </select>
            keyword:  <input type="text" name="scope_text_filter"></input></td></tr>
            <input type="submit" /></td></tr>
              <input type="hidden" name="id" value="<% $id %>" />
              <input type="hidden" name="view" value="<% $view %>">
              </form>
            </table>
%       }

%       %args = ( table=>$dtbl, object=>\@robjs );
%       if ( $edit eq $i ){
%           $args{withedit} = 1;
%	}
%       else{
%       # If data came ordered from the db, tell sortresults not to bother sorting
%           $args{sort} = 0 if ( $ffield_order );
%       }
%	if ($num){
%           $args{return_args} = "?id=$id&view=$view";
%           $m->comp('/generic/sortresults.mhtml', %args);
%       }
%    
    </div>
%  }
%}

<!-- End Display has_many objects -->

<!-- Display bulk import -->
% if ( $view eq 'bulk_import' ){
   <div class="container">
      <div class="containerhead">Bulk Import</div>
      <div class="containerbody">
%     if ( $o->type->name ne 'global' && $o->type->name ne 'group' ){
	 <p>At this time, only host scopes can be imported, which can only be contained in global or group scopes
%     }else{
      <p>
      <div align="center">
      <p>
      Create fixed-address host scopes in bulk by adding lines containing ethernet and IP address, separated by spaces.
      <br> For example:
      <p>00:23:45:67:89:AB 192.168.0.1
      <br>00:23:45:67:89:CD 192.168.0.2
      <p>
      <form action="scope.html" method="POST">
	  <input type="hidden" name="id" value="<% $id %>" />
	  <input type="hidden" name="edit" value="bulk_import">
	  <textarea name="bulk_import_data" cols="80" rows="20"></textarea>
	  <p>
	  Overwrite existing records <input type="checkbox" name="import_overwrite" CHECKED>
	  <p>
	  <input type="submit" name="submit" value="Import">
      </form>
      </div>
%   }
     </div>
    </div>
% }
<!-- End Display bulk import -->


</div>
