<%doc>
Device Interface UI
</%doc>
%
%
<%attr>
title   => 'Interface' 
section => 'Management'
</%attr>
%
%
<%args>
$id
$user => $ui->get_current_user($r)
$view => undef
$edit => undef
</%args>
%
%
<%init>
my $DEBUG = 0;
print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' if $DEBUG;
my $table = 'Interface';

# Check if user can access this page
# We don't want average users seeing this page, its only for admins and operators.
my $manager = $ui->get_permission_manager($r);
unless ( $manager && $manager->can($user, "access_section", 'interface.html') ){
    $m->comp('/generic/error.mhtml', error=>"You don't have permission to access this page");
}

my $o = Interface->retrieve($id);

my $dbh = Netdot::Model->db_Main();
my $fdb_tstamps;
my %linksfrom = Interface->meta_data->get_links_from;
</%init>

<div id="sectiondetail">

<!-- Display main object values -->
<div class="container">
    <div class="containerheadleftoutside"><strong>Interface</strong></div>
    <div class="containerheadrightoutside">
%           if ( $manager && $manager->can($user, "access_admin_section", 'interface.html:new') ){
                <a href="edit.html?table=Interface">[new]</a>
%           }
%           if ( $manager && $manager->can($user, 'delete', $o) ){
                <a href="delete.html?table=Interface&id=<% $id %>">[delete]</a>
%           }
%           if ( $manager && $manager->can($user, 'edit', $o) ){
                <a href="edit.html?table=Interface&id=<% $id %>">[edit]</a>
%           }
            <a href="#" onClick="opentextwindow(jspopoutstring,'js','');">[text]</a>
    </div>
    <div class="containerbodyoutside">
        <& /generic/form.mhtml, table=>"Interface", id=>$id, edit=>0 &>
    </div>
</div>
<!-- End Display main object values -->

<!-- Display has_many possibilities-->

      <div class="container">
	<div class="containerhead">View</div>
	   <div class="containerbody">
%	      if ( $view eq "all" ){
		  [all]
%	      }elsif ( keys %linksfrom > 1 ){
		  <a href="interface.html?id=<% $o %>&view=all">[all]</a>
%             }
%             foreach my $i ( sort keys %linksfrom ){
%               next if $i eq 'neighbors';
%               next if $i =~ /^history_/;
%               if ( $view eq $i ){
                   [<% $i %>]
%               }else{
                   <a href="interface.html?id=<% $o %>&view=<% $i %>">[<% $i %>]</a>
%               }   
%             }
              <a href="interface.html?id=<% $o %>&view=history">[history]</a>
	   </div>
	</div>
      </div>
<!-- End Display has_many possibilities-->

<!-- Display has_many objects -->

%foreach my $i ( keys %linksfrom ){
%    if ( ($view eq 'all' || $view eq $i ) ){
%	# Special cases
%       next if $i eq 'neighbors';
%       next if $i =~ /^history_/;
%	if( $i eq 'vlans' && (my @ivlans = $o->vlans) ){
%           my @vlans = map { $_->vlan } @ivlans;
            <div class="containerhead">VLANs</div>
	        <div class="container">
	           <& /generic/sortresults.mhtml, object => \@vlans &>
                </div>
            </div>
%        }elsif( $i eq 'fwt_entries' ){
<%perl>
            my $sth1 = $dbh->prepare("SELECT   ft.tstamp 
                                      FROM     interface i, fwtable ft, fwtableentry fte 
                                      WHERE    fte.interface=i.id 
                                         AND   fte.fwtable=ft.id 
                                         AND   i.id=?
                                      GROUP BY ft.tstamp
                                      ORDER BY ft.tstamp DESC");
</%perl>
%            $sth1->execute($id);
%            $fdb_tstamps = $sth1->fetchall_arrayref();
%            my $fdb_count = scalar(@$fdb_tstamps);
%            my @headers = ("Timestamp");
%            my @rows;
%            if ( $fdb_count ){
                <div class="containerhead">FWT entries</div>
%               foreach my $row (@$fdb_tstamps){
%	            my ($tstamp) = @$row;
%                   push @rows, ["<a href=\"i_fdb.html?id=$id&tstamp=$tstamp\">$tstamp</a>"];
%               }
%               $m->comp('/generic/data_table.mhtml', field_headers=>\@headers, data=>\@rows);
                </div>
%            }

%        }elsif( $i eq 'arp_entries' ){
<%perl>
             my $sth2 = $dbh->prepare("SELECT   arp.tstamp 
                                        FROM     interface i, arpcache arp, arpcacheentry arpe
                                        WHERE    arpe.interface=i.id 
                                           AND   arpe.arpcache=arp.id 
                                           AND   i.id=?
                                        GROUP BY arp.tstamp
                                        ORDER BY arp.tstamp DESC");
</%perl>
%             $sth2->execute($id);
%             my $arp_tstamps = $sth2->fetchall_arrayref();
%             my $arp_count = scalar(@$arp_tstamps);
%             my @headers = ("Timestamp");
%             my @rows = ();
%             if ( $arp_count ){
                  <div class="containerhead">ARP entries</div>
%                 foreach my $row (@$arp_tstamps){
%	              my ($tstamp) = @$row;
%                     push @rows, ["<a href=\"i_arp.html?id=$id&tstamp=$tstamp\">$tstamp</a>"];
%                 }
%                 $m->comp('/generic/data_table.mhtml', field_headers=>\@headers, data=>\@rows);
                  </div>
%              }

%        }else{
%	    # 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 = $o->$i;
%	    my $dtbl  = $j;
%	    my $ctbl  = $j; 
%	
%	    my %args = ( table=>$dtbl, object=>\@robjs );	
%	    my $num = scalar(@robjs);
%	    my $title = $i;
%	    $title .= " ($num)" if $num;	     
            <div class="container">
                <div class="containerheadleft"><% $title %></div>
	    <div class="containerheadright">
%           if ( $edit eq $i ){	    
                &nbsp;
%           }else{
%	        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 ( $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} = "?table=$table&id=$id&view=$view";
%               $m->comp('/generic/sortresults.mhtml', %args);
%           }
            </div>
%        } #else

%    } # if view
%}# foreach

<!-- End Display has_many objects -->

<!-- Display history objects associated with this object-->
%if ( $view eq 'all' || $view eq 'history' ){
<div class="container">
%    my @ho = $o->get_history;
     <& /generic/sortresults.mhtml, object => \@ho , view => "row", sort => 0 &>
</div>
%} 
<!-- End Display history objects -->

</div>
