######################################################################
#
#  EPrints Author Home
#
######################################################################
#
#  This file is part of GNU EPrints 2.
#  
#  Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 2 is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  EPrints 2 is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with EPrints 2; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

use EPrints;

use strict;


######################################################################
#
# Session initialisation
#
######################################################################

my $session = new EPrints::Session;
exit( 0 ) unless( defined $session );

# Any user can view this page, but most BE a user.
if( !$session->auth_check )
{
	$session->terminate();
	exit( 0 );
}

# If we were sent here by change_user then we have an icky url
# eg .../home/username rather than .../home 
# let's clean it up...
if( $ENV{PATH_INFO} ne "" )
{
	$session->redirect( $session->get_repository->get_conf( "userhome" ) );
	$session->terminate();
	exit;
}
	
my $user = $session->current_user;

my $fullname = $user->render_description();

my $probs = $user->validate();
my $valid_user = ( scalar @{$probs} == 0 );

# If not a valid user then redirect them to the user-info page.
# This'll give us info like "name".
unless( $valid_user )
{	
	$session->redirect( "record" );
	$session->terminate();
	exit;
}

######################################################################
#
# Render page
#
######################################################################

my( $page, $dt, $dd, $a );
my( $table, $tr, $td );



my $changeuserpath = "/change_user/".($user->get_value( "username" ));
my $securehost = $session->get_repository->get_conf( "securehost" );
my $securepath = $session->get_repository->get_conf( "securepath" );
if( EPrints::Utils::is_set( $securehost ) )
{
	$changeuserpath = $securepath.$changeuserpath;
}

my $tools = [
	### Create New Item
	{ 
		location=>"create",
		code=>"new_item",
		page=>"submit?_action_new=1",
		priv=>"deposit"
	},
	### Change User Info
	{ 
		location=>"user",
		code=>"change",
		page=>"record",
		invpriv=>"no_edit_own_record"
	},
	### Change subscription options
	{ 
		location=>"user",
		code=>"subscribe",
		page=>"subscribe",
		priv=>"subscription"
	},
	### View the repository status page
	{ 
		location=>"staff",
		code=>"status",
		page=>"status",
		priv=>"view-status"
	},
	### Review items we've got in live repository
	{
		location=>"review",
		code=>"review",
		page=>"review",
		priv=>"deposit"
	},
	### View the Editor submission buffer
	{
		location=>"staff",
		code=>"staff_buffer",
		page=>"staff/buffer",
		priv=>"editor"
	},
	### Staff EPrints Search
	{
		location=>"staff",
		code=>"staff_eprint_search",
		page=>"search",
		priv=>"staff-view"
	},
	### Staff User Search
	{
		location=>"staff",
		code=>"staff_user_search",
		page=>"search/users",
		priv=>"staff-view"
	},
	### Add New User
	{
		location=>"staff",
		code=>"staff_add_user",
		page=>"staff/add_user",
		priv=>"edit-user"
	},
	### Change Email
	{
		location=>"user",
		code=>"change_email",
		page=>"change_email",
		priv=>"change-email"
	},
	### Use the Subject Edit Tool
	{
		location=>"staff",
		code=>"staff_edit_subject",
		page=>"staff/edit_subject",
		priv=>"edit-subject"
	},
	### Change the current login user
	{
		location=>"user",
		code=>"change_user",
		page=>$changeuserpath,
		priv=>"change-user"
	}
];

my $extras = $session->get_repository->get_conf( "userhome_extras" );
if( defined $extras )
{
	push @{$tools},@{$extras};
}

my $sb = $session->get_repository->get_conf( "skip_buffer" );	
my $dls = {};
my $tool;
foreach	$tool (@{$tools})
{
	# disable buffer option if skip_buffer is in effect:
	if( $tool->{code} eq 'staff_buffer' && defined $sb && $sb == 1 )
	{
		next;
	}


	if( defined $tool->{priv} )
	{
		next unless $user->has_priv( $tool->{priv} );
	}
	if( defined $tool->{invpriv} )
	{
		next if $user->has_priv( $tool->{invpriv} );
	}
	if( !defined $dls->{$tool->{location}} )
	{
		$dls->{$tool->{location}} = $session->make_element( "dl" );
	}
	my $dl = $dls->{$tool->{location}};
	$dt = $session->make_element( "dt" );
	$a = $session->render_link( $tool->{page} );
	$a->appendChild( $session->html_phrase( "cgi/users/home:".$tool->{code}."_link" ) );
	$dt->appendChild( $a );
	$dl->appendChild( $dt );

	$dd = $session->make_element( "dd" );
	$dd->appendChild( $session->html_phrase( "cgi/users/home:".$tool->{code}."_info" ) );
	$dl->appendChild( $dd );
}








my %pins;

if( $user->has_priv( "deposit" ) )
{
	my %dpins;
	$dpins{create} = $dls->{create};
	$dpins{workspace} = &render_workspace( $session, $user );
	$dpins{pending} = &render_pending( $session, $user );
	$dpins{recent} = &render_recent( $session, $user, 10, $dls->{review} );

	$pins{layout_deposit} = $session->html_phrase( "cgi/users/home:layout_deposit", %dpins );
}
else
{
	$pins{layout_deposit} = $session->make_doc_fragment;
}

if( defined $dls->{user} )
{
	$pins{user} = $dls->{user};
}
else
{
	$pins{user} = $session->make_doc_fragment;
}


if( defined $dls->{staff} )
{
	$pins{layout_staff} = $session->html_phrase( "cgi/users/home:layout_staff", options => $dls->{staff} );
}
else
{
	$pins{layout_staff} = $session->make_doc_fragment;
}


$page= $session->html_phrase( "cgi/users/home:layout", %pins );

$session->build_page( 
		$session->html_phrase( 
			"cgi/users/home:user_home", 
			name => $fullname ),
		$page,
		"user_home" );
$session->send_page();

$session->terminate();
exit;



######################################################################
#
# render_workspace( $session, $user )
#
#  This puts all of the user's items in the inbox table in a form.
#  The buttons will POST stuff to the submit script, with the possible
#  actions:
#
#   New     (Create a new EPrint)
#   Edit    (Edit a selected EPrint)
#   Clone   (Clone the selected EPrint)
#   Delete  (Delete the selected EPrint)
#   Deposit (Submit the selected EPrint to the respository)
#
#
#  If the user has no items in the inbox, a single button is drawn,
#  Allowing them to start the upload process.
#
######################################################################

sub render_workspace
{
	my( $session, $user ) = @_;

	### Get the actual items in the workspace
	my $ds = $session->get_repository->get_dataset( "inbox" );
	my $list = $user->get_owned_eprints( $ds );

	my( $html, $form, $div );

	$html = $session->make_doc_fragment();

	if( $list->count == 0 )
	{
		# No items, oh well.
		return $html;
	}

	# One or more items in workspace.
	# Render a form to let the user fiddle with them.

	my( $table, $tr, $td );

	$table = $session->make_element( "table",
			cellpadding=>3,
			cellspacing=>0,
			border=>0 );
	my $e;
	my $cnt = 0;
	my $row = 0;
		
	$list->map( sub {
		my( $session, $dataset, $e ) = @_;
	
		$tr = $session->make_element( "tr", class=>"row_".($row?"b":"a") );
		$table->appendChild( $tr );	
		$td = $session->make_element( "td", class=>'first_col userpage_table_emph' );
		$tr->appendChild( $td );
		$td->appendChild( $e->render_description() );
		$td = $session->make_element( "td", class=>'last_col' );
		$tr->appendChild( $td );
	#	$td->appendChild( $session->html_phrase( 
	#		"cgi/users/home:accepted_at",
	#		time=>$e->render_value( "datestamp" ) ) );
		my $a;

		$a = $session->render_link( "submit?eprintid=".$e->get_id."&_action_edit=1" );
		$a->appendChild( $session->html_phrase("cgi/users/home:action_edit") );
		$td->appendChild( $a );

		$td->appendChild( $session->make_text( ", " ) );

		$a = $session->render_link( "submit?eprintid=".$e->get_id."&_action_submit=1" );
		$a->appendChild( $session->html_phrase("cgi/users/home:action_submit") );
		$td->appendChild( $a );

		$td->appendChild( $session->make_text( ", " ) );

		$a = $session->render_link( "submit?eprintid=".$e->get_id."&_action_copy=1" );
		$a->appendChild( $session->html_phrase("cgi/users/home:action_copy") );
		$td->appendChild( $a );

		$td->appendChild( $session->make_text( ", " ) );

		$a = $session->render_link( "submit?eprintid=".$e->get_id."&_action_delete=1" );
		$a->appendChild( $session->html_phrase("cgi/users/home:action_delete") );
		$td->appendChild( $a );
		$row=!$row;
	} );

	$html->appendChild( $session->html_phrase( "cgi/users/home:depositing" ) );
	$html->appendChild( $table );

	return $html;
}



######################################################################
#
# render_pending( $session, $user)
#
#  Render any items that are in the submissions buffer, if any
#
######################################################################

sub render_pending
{
	my( $session, $user ) = @_;

	### Get the items in the buffer
	my $ds = $session->get_repository->get_dataset( "buffer" );
	my $list = $user->get_owned_eprints( $ds );

	my $html = $session->make_doc_fragment();

	if( $list->count == 0 )
	{
		$html->appendChild( $session->html_phrase( "cgi/users/home:no_pending" ) );
	}
	else
	{
		my( $table, $tr, $td );

		$table = $session->make_element( "table",
				cellpadding=>3,
				cellspacing=>0,
				border=>0 );
		$html->appendChild( $table );
		my $row = 0;
		$list->map( sub {
			my( $session, $dataset, $e ) = @_; 

			$tr = $session->make_element( "tr", class=>"row_".($row?"b":"a") );
			$table->appendChild( $tr );	
			$td = $session->make_element( "td", class=>"first_col userpage_table_emph" );
			$tr->appendChild( $td );
			$td->appendChild( $e->render_description() );
			$td = $session->make_element( "td", class=>"last_col" );
			$tr->appendChild( $td );
			$td->appendChild( $session->html_phrase( 
				"cgi/users/home:deposited_at",
				time=>$e->render_value( "datestamp" ) ) );
			$row=!$row;
		} );
	}
	return $html;
}
	

######################################################################
#
# render_recent( $session, $user, $max, $review )
#
#  Render any items that have been recently accepted into the main 
#  respository.
#
######################################################################

sub render_recent
{
	my( $session, $user, $max, $review ) = @_;
	
	### Get the items in the buffer
	my $ds = $session->get_repository->get_dataset( "archive" );
	my $list = $user->get_owned_eprints( $ds );

	my $html = $session->make_doc_fragment();

	if( $list->count == 0 )
	{
		$html->appendChild( $session->html_phrase( "cgi/users/home:no_recent" ) );
	}
	else
	{
		my( $table, $tr, $td );
	
		$table = $session->make_element( "table",
				cellpadding=>3,
				cellspacing=>0,
				border=>0 );
		$html->appendChild( $table );
		my $e;
		my $cnt = 0;
		my $row = 0;

		my $size = $list->count;
		my $offset = $list->count - $max;
		my $count = $max;
		$offset = 0 if( $offset < 0 );
			
		foreach $e ( reverse $list->get_records( $offset, $count ) )
#cjg not exactly the right way!
#cjg should really do it by deposit date
		{
			last if( $cnt++ == $max );
			$tr = $session->make_element( "tr", class=>"row_".($row?"b":"a") );
			$table->appendChild( $tr );	
			$td = $session->make_element( "td", class=>"first_col userpage_table_emph" );
			$tr->appendChild( $td );
		        my $a = $session->render_link( $e->get_url() );
			$a->appendChild( $e->render_description() );
			$td->appendChild( $a );
			$td = $session->make_element( "td", class=>"last_col" );
			$tr->appendChild( $td );
			$td->appendChild( $session->html_phrase( 
				"cgi/users/home:accepted_at",
				time=>$e->render_value( "datestamp" ) ) );
			$row=!$row;
		}
	}

	if( defined $review )
	{
		$html->appendChild( $review );
	}
	return $html;
}

