######################################################################
#
#  EPrints Submission Buffer Overview
#
######################################################################
#
#  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;

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

# Check we have privs
if( !$session->auth_check( "editor" ) )
{
	$session->terminate();
	exit( 0 );
}
#cjg does not check that item is editable...

my $user = $session->current_user();

my( $page, $p, $table, $tr, $td, $th, $link );

$page = $session->make_doc_fragment();

# Get EPrints in the submission buffer
my $list = $user->get_editable_eprints();

if( $list->count == 0 )
{
	# No EPrints
	$page->appendChild( $session->html_phrase( 
		"cgi/users/buffer:no_entries",
		scope=>$user->render_value( "editperms" ) ) );
}
else
{
	$page->appendChild( $session->html_phrase( 
		"cgi/users/buffer:buffer_blurb",
		scope=>$user->render_value( "editperms" ) ) );

	$table = $session->make_element( "table", border=>0, cellpadding=>4, cellspacing=>0 );
	$page->appendChild( $table );
	$tr = $session->make_element( "tr", class=>"header_plain" );
	$table->appendChild( $tr );
	
	$th = $session->make_element( "th" );
	$th->appendChild( $session->html_phrase( "cgi/users/buffer:title" ) );
	$tr->appendChild( $th );

	$th = $session->make_element( "th" );
	$th->appendChild( $session->html_phrase( "cgi/users/buffer:sub_by" ) );
	$tr->appendChild( $th );

	$th = $session->make_element( "th" );
	$th->appendChild( $session->html_phrase( "cgi/users/buffer:sub_date" ) );
	$tr->appendChild( $th );

	my $info = {row => 1};

	$list->map( sub {
		my( $session, $dataset, $e, $info ) = @_;

		$tr = $session->make_element( "tr", class=>"row_".($info->{row}%2?"b":"a") );
		$table->appendChild( $tr );

		# Title
		$td = $session->make_element( "td", class=>"first_col" );
		$tr->appendChild( $td );
		$link = $session->render_link( "edit_eprint?dataset=buffer&eprintid=".$e->get_value("eprintid") );
		$link->appendChild( $e->render_description() );
		$td->appendChild( $link );
		
		# Link to user
		my $user = new EPrints::User( $session, $e->get_value( "userid" ) );
		
		$td = $session->make_element( "td", class=>"middle_col" );
		$tr->appendChild( $td );
		if( defined $user )
		{
#cjg Has view-user priv?
			$td->appendChild( $user->render_citation_link( undef, 1 ) );
		}
		else
		{
			$td->appendChild( $session->html_phrase( "cgi/users/buffer:invalid" ) );
		}
	
		my $buffds = $session->get_repository->get_dataset( "buffer" );	
		
		$td = $session->make_element( "td", class=>"last_col" );
		$tr->appendChild( $td );
		$td->appendChild( $buffds->get_field( "datestamp" )->render_value( $session, $e->get_value( "datestamp" ) ) );
		++$info->{row};
	}, $info );
}

$page->appendChild( $session->html_phrase( "general:userhome_link" ) );

$session->build_page(
	$session->html_phrase( "cgi/users/buffer:overview_title" ), 
	$page,
	"buffer" );
$session->send_page();

$session->terminate();
