<%doc>

  Generic object view
  Uses metadata to display all fields with their values
  and lists of each group of 'has_many' related objects

  Required Arguments:
    table name and either id or position

</%doc>

<%args>
$table
$id
$user    => $ui->get_current_user($r)
$view    => 'all'
$edit    => undef
</%args>

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

<%init>
my $DEBUG = 0;
my($obj, $prevobj );
my %ftables;


unless ( $obj = $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", $obj) ){
    $m->comp('/generic/error.mhtml', error=>"You don't have permission to view this object");
}

my $labelstr  = $obj->get_label;
my %linksfrom = $table->meta_data->get_links_from;
my %order     = $table->meta_data->get_column_order;

if ( $DEBUG ){
    print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>';
    print '%linksfrom is  <pre>', Dumper(%linksfrom), '</pre><br>';
    print '%order is  <pre>', Dumper(%order), '</pre><br>';
}
</%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', 'view.html:new') ){
                <a href="edit.html?table=<% $table %>">[new]</a>
%           }
%	    if ( $manager && $manager->can($user, "edit", $obj) ){
		<a href="edit.html?table=<% $table %>&id=<% $obj %>">[edit]</a>
%	    }
%	}
%	if ( $manager && $manager->can($user, "delete", $obj) ){
            <a href="delete.html?table=<% $table %>&id=<% $obj %>">[delete]</a>
%       }
        <a href="#" onClick="opentextwindow(jspopoutstring,'js','');">[text]</a>
    </div>
    <div class="containerbodyoutside">
        <& form.mhtml, table=>$table, id=>$id, edit=>0 &>
    </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="view.html?table=<% $table %>&id=<% $obj %>&view=all">[all]</a>
%             }
%             foreach my $i ( sort keys %linksfrom ){
%               next if $i =~ /^history_/;
%               if ( $view eq $i ){
                   [<% $i %>]
%               }else{
                   <a href="view.html?table=<% $table %>&id=<% $obj %>&view=<% $i %>">[<% $i %>]</a>
%               }   
%             }
%           if ( $has_history_table ){
              <a href="view.html?table=<% $table %>&id=<% $obj %>&view=history">[history]</a>
%	    }
	   </div>
	</div>
      </div>
%  }
<!-- End Display has_many possibilities-->

<!-- Display has_many objects -->
<%perl>
foreach my $i ( keys %linksfrom ){
    next if $i =~ /^history_/;
    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 = $obj->$i;
    my $dtbl  = $j;
    my $ctbl  = $j; 

    my $num = scalar(@robjs);
</%perl>

      <div class="container">
%     my $title = $i;
%     $title .= " ($num)" if $num;	     
          <div class="containerheadleft"><% $title %></div>
	  <div class="containerheadright">
%         if ( $edit eq $i ){	    
              &nbsp;
%         }else{
%	      if ( $num ){
%	          if ( $manager && $manager->can($user, 'edit', $obj) ){
                      <a href="view.html?table=<% $table %>&id=<% $id %>&view=<% $view %>&edit=<% $i %>">[edit]</a>
%                 }
%	      }
%	      if ( $manager && $manager->can($user, 'access_admin_section', 'view.html:add') ){
	          <a href="edit.html?table=<% $ctbl %>&<% $ffield %>=<% $obj %>">[add]</a>
%             }else{
                  &nbsp;
%             }
%         }
	  </div>
%         my %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} = "?table=$table&id=$id&view=$view";
%             $m->comp('sortresults.mhtml', %args);
%         }
     </div>
%    }
%}
<!-- End Display has_many objects -->



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


</div>
