######################################################################
#
#  EPrints Author Home
#
######################################################################
#
#  This file is part of EPrints 2.
#  
#  Copyright (c) 2000,2001,2002 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::User;
use EPrints::Database;
use EPrints::Session;
use EPrints::EPrint;

use strict;


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

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

# Any user can view this page, but most BE a user.
if( !$session->auth_check )
{
	$session->terminate();
	Apache::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_archive()->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 );

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

my( $page, $dl, $dt, $dd, $a );

$page = $session->make_doc_fragment();

### Welcome author blurb
$page->appendChild( $session->html_phrase( "cgi/users/home:intro" ) );

if( $valid_user && $user->has_priv( "deposit" ) )
{
	### Workspace
	$page->appendChild(
		&render_workspace( $session, $user ) );

	### Pending
	$page->appendChild(
		&render_pending( $session, $user ) );

	$page->appendChild( $session->render_ruler() );
}

if( !$valid_user )
{
	### User needs to update their record
	$page->appendChild( $session->html_phrase( "cgi/users/home:before" ) );
}

$dl = $session->make_element( "dl" );

### Change author record

unless( $user->has_priv( "no_edit_own_record" ) )
{
	$dt = $session->make_element( "dt" );
	$a = $session->render_link( "record" );
	$a->appendChild( $session->html_phrase( "cgi/users/home:change_link" ) );
	$dt->appendChild( $a );
	$dl->appendChild( $dt );
	$dd = $session->make_element( "dd" );
	$dd->appendChild( $session->html_phrase( "cgi/users/home:change_info" ) );
	$dl->appendChild( $dd );
}

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

my $tools = [
	### Change subscription options
	{ 
		code=>"subscribe",
		page=>"subscribe",
		priv=>"subscription"
	},
	### View the archive status page
	{ 
		code=>"status",
		page=>"status",
		priv=>"view-status"
	},
	### Review items we've got in archive
	{
		code=>"review",
		page=>"review",
		priv=>"deposit"
	},
	### View the Editor submission buffer
	{
		code=>"staff_buffer",
		page=>"staff/buffer",
		priv=>"editor"
	},
	### Staff EPrints Search
	{
		code=>"staff_eprint_search",
		page=>"staff/eprint_search",
		priv=>"staff-view"
	},
	### Staff User Search
	{
		code=>"staff_user_search",
		page=>"staff/user_search",
		priv=>"staff-view"
	},
	### Add New User
	{
		code=>"staff_add_user",
		page=>"staff/add_user",
		priv=>"edit-user"
	},
	### Change Email
	{
		code=>"change_email",
		page=>"change_email",
		priv=>"change-email"
	},
	### Use the Subject Edit Tool
	{
		code=>"staff_edit_subject",
		page=>"staff/edit_subject",
		priv=>"edit-subject"
	},
	### Change the current login user
	{
		code=>"change_user",
		page=>$changeuserpath,
		priv=>"change-user"
	}
];

if( $valid_user )
{
	my $tool;
	foreach	$tool (@{$tools})
	{
		next unless $user->has_priv( $tool->{priv} );
	
		$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 );
	}
}

$page->appendChild( $dl );

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

$session->terminate();



######################################################################
#
# 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 archive)
#
#
#  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_archive()->get_dataset( "inbox" );
	my @eprints = $user->get_eprints( $ds );


	if( scalar @eprints == 0)
	{
		# No items, oh well.
		my( $form );
		$form = $session->render_form( "post", "submit" );	

		$form->appendChild(
			$session->render_action_buttons( 	
				new => $session->phrase( "cgi/users/home:start_upload" ) ) );
		return $form;
	}

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

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

	$html = $session->make_doc_fragment();

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

	my @eprintids;
	my %eprint_titles;
	my $e;

	foreach $e (@eprints)
	{
		my $id = $e->get_value( "eprintid" );
		push @eprintids, $id;
		$eprint_titles{$id} = EPrints::Utils::tree_to_utf8(
					$e->render_description() );
	}

	### Now put them in a form
	$form = $session->render_form( "post", "submit#t" );

	$div = $session->make_element( "div", class => "formfieldinput" );
	$form->appendChild( $div );
	$div->appendChild( $session->render_option_list(
		name => "eprintid",
		height => 6,
		values => \@eprintids,
		labels => \%eprint_titles,
		default => ( scalar @eprints==1 ? $eprintids[0] : undef ) 
	) );

	$form->appendChild( $session->render_action_buttons(
		new => 
		  $session->phrase("cgi/users/home:action_new"),
		edit => 
		  $session->phrase("cgi/users/home:action_edit"),
		clone => 
		  $session->phrase("cgi/users/home:action_clone"),
		delete => 
		  $session->phrase("cgi/users/home:action_delete"),
		submit => 
		  $session->phrase("cgi/users/home:action_submit") 
	) );

	$html->appendChild( $form );

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

	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_archive()->get_dataset( "buffer" );
	my @eprints =  $user->get_eprints( $ds );

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

	if( scalar @eprints == 0 )
	{
		return $html;
	}
	my( $table, $tr, $td );

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

	$table = $session->make_element( "table",
			cellpadding=>3,
			border=>0 );
	$html->appendChild( $table );
	my $e;
	foreach $e (@eprints)
	{
		$tr = $session->make_element( "tr" );
		$table->appendChild( $tr );	
		$td = $session->make_element( "td" );
		$tr->appendChild( $td );
		$td->appendChild( $e->render_description() );
		$td = $session->make_element( "td" );
		$tr->appendChild( $td );
		$td->appendChild( $session->html_phrase( 
			"cgi/users/home:deposited_at",
			time=>$e->render_value( "datestamp" ) ) );
	}
	return $html;
}

