<%doc>
 Contacts Management
</%doc>

<%args>
$user => $ui->get_current_user($r);
$showheader => 1
</%args>

<%attr>
title   => 'Contacts'
# This is used by the toplevel autohandler to determine the section.
# Since attributes aren't inherited (but shouldn't they be ...) we have
# to also specify this in the dhandler
section => "Contacts" # $SECTION
</%attr>

<%shared>
my $PAGE_ATTRIBUTE = "CONTACTS_PAGE";
my $SECTION = "Contacts";
</%shared>

<%init>
my $DEBUG = 0;
my $manager = $ui->get_permission_manager($r);
print '%ARGS is  <pre>', Dumper(%ARGS), '</pre><br>' if $DEBUG;
print "Request component name: ", $m->request_comp->name if $DEBUG;

my $page;

if ( $manager && $manager->can($user, "access_section", 'contacts') ){

    my $caller_args = $m->caller_args(-1);
    if (exists($caller_args->{page})){
	$user->setAttribute($r, "$PAGE_ATTRIBUTE", $caller_args->{page});
    }
    $page = $user->getAttribute("$PAGE_ATTRIBUTE");  
    
    if (exists($caller_args->{tasks})){
	$user->setAttribute($r, "SHOW_TASKS", $caller_args->{tasks});
    }
    my $show_tasks = $user->getAttribute("SHOW_TASKS");
    if ($show_tasks eq "") {
	$user->setAttribute($r, "SHOW_TASKS", "show");
	$show_tasks = "show";
    }
    *print_showtaskslink = $m->comp('SELF:.sub_print_showtaskslink'); 
    
    my $hideheader;
    if( !$showheader ) {
	$hideheader = 'style="display:none"';
    }

    if ( $page eq "ENTITIES" && $m->request_comp->name ne "entities.html" ) {	
	$m->comp('entities.html', %ARGS);
    }elsif ( ($page eq "PEOPLE" || $page eq "") && $m->request_comp->name ne "people.html" ) {
	$m->comp('people.html', %ARGS);
    }elsif ( ($page eq "CLISTS" || $page eq "") && $m->request_comp->name ne "clists.html" ) {
	my %args = (showheader=>$showheader);
	$args{view} = $ARGS{view} if defined $ARGS{view};
	$m->comp('clists.html', %args);
    }
}
</%init>

<%perl>
if ( my $next = $m->fetch_next ){
    if ( $page ){
	$m->call_next(page => $page);
    }else{
	$m->call_next();
    }
}
</%perl>


%# Methods can be called externally.
<%method .section_meta_data>
    <%doc>
        Returns the meta data used to generate this section\'s header in section1.mhtml
    </%doc>
    <%init>
        return {  
	    section      => $SECTION,
	    page         => 'contacts/', 
	    title        => 'Contacts',
	    attribute    => $PAGE_ATTRIBUTE,
	    sub_sections => [
		{ section => 'PEOPLE',   title => 'People'  },
		{ section => 'ENTITIES', title => 'Entities'},
		{ section => 'CLISTS',   title => 'Contact Lists'},
	    ]
        };
    </%init>
</%method>


