######################################################################
#
#  Review Author's Documents in the Archive
#
######################################################################
#
#  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 = new EPrints::Session;
exit( 0 ) unless( defined $session );

# Check we have privs
if( !$session->auth_check( "deposit" ) )
{
	$session->terminate();
	exit( 0 );
}

my $page;
my $action = $session->get_action_button();
if( !defined $action )
{
	$page = make_form( $session );
}
else
{
	$page = process_form( $session, $action );
}

$session->terminate();

#############################################################

sub make_form
{
	my( $session ) = @_;

	my $user = $session->current_user;

	# cjg. This should maybe extract stuff you can edit
	# not just stuff you deposited.

	my $ds = $session->get_repository->get_dataset( "archive" );	
	my $list = $user->get_owned_eprints( $ds );

	my $user = $session->current_user;
	my $fullname = $user->render_description();
	my $page = $session->make_doc_fragment();
	my $title = $session->html_phrase( "cgi/users/review:title", name=>$fullname );

	if( $list->count == 0 )
	{
		$page->appendChild( $session->html_phrase( 
			"cgi/users/review:no_docs" ) );

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

		$session->build_page( $title, $page, "review_none" );
		$session->send_page();
		return;
	}
	my $r_req = $session->get_repository->get_conf( 
			"allow_user_removal_request" ); 
	my $checkout = $session->get_repository->get_conf( 
			"allow_user_checkout_eprint" ); 

	$page->appendChild( $session->html_phrase( "cgi/users/review:blurb" ) );

	if( $checkout )
	{
		$page->appendChild( $session->html_phrase( 
			"cgi/users/review:checkout_info" ) );
	}
	if( $r_req )
	{
		$page->appendChild( $session->html_phrase( 
			"cgi/users/review:remove_info" ) );
	}



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

	$form = $session->render_form( "post", "review" );
	$page->appendChild( $form );
	$table = $session->make_element( "table", border=>0, cellspacing=>0, cellpadding=>3 );
	$form->appendChild( $table );

	my $row = 0;
	$list->map( sub {
		my( $session, $dataset, $eprint ) = @_;

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

		$td = $session->make_element( "td", class=>"first_col" );
		$tr->appendChild( $td );
		$td->appendChild( $eprint->render_citation_link() );

		$td = $session->make_element( "td", class=>"last_col" );
		$tr->appendChild( $td );


		my $a;

		$a = $session->render_link( "review?_action_clone_".$eprint->get_id."=1" );
		$a->appendChild( $session->html_phrase("cgi/users/review:clone") );
		$td->appendChild( $a );

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

		$a = $session->render_link( "review?_action_copy_".$eprint->get_id."=1" );
		$a->appendChild( $session->html_phrase("cgi/users/review:copy") );
		$td->appendChild( $a );

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

			$a = $session->render_link( "review?_action_checkout_".$eprint->get_id."=1" );
			$a->appendChild( $session->html_phrase("cgi/users/review:checkout") );
			$td->appendChild( $a );
		}

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

			$a = $session->render_link( "review?_action_remove_".$eprint->get_id."=1" );
			$a->appendChild( $session->html_phrase("cgi/users/review:req_remove") );
			$td->appendChild( $a );
		}
		$row=!$row;
	} );
	$session->build_page( $title, $page, "review_list" );
	$session->send_page();
	return;

}

sub process_form
{
	my( $session, $action ) = @_;

	my $user = $session->current_user;
	my $ds = $session->get_repository->get_dataset( "archive" );	

	if( $action =~ m/^(clone|copy|remove|checkout)_(.+)$/ )
	{
		my( $act, $eprintid ) = ( $1, $2 );

		my $r_req = $session->get_repository->get_conf( "allow_user_removal_request" ); 
		my $checkout = $session->get_repository->get_conf( "allow_user_checkout_eprint" ); 
		my $eprint = EPrints::DataObj::EPrint->new( $session, $eprintid, $ds );
		if( !defined $eprint )
		{
			$session->render_error(
				$session->html_phrase( "cgi/users/review:cant_find_it" ) );
			return;
		}		

		if( $act eq "clone" || $act eq "copy" )
		{
			my $inbox_ds = $session->get_repository->get_dataset( "inbox" );
			my $copy;
			if( $act eq "clone" )
			{
 				$copy = $eprint->clone( $inbox_ds, 1 );
			}
			else
			{
				# nofiles, no link
 				$copy = $eprint->clone( $inbox_ds, 0, 1 );
			}
		
			if( defined $copy )
			{
				# Copied OK, redirect to workspace
				$session->redirect( 'submit?eprintid='.$copy->get_id.'&_action_edit=1' );
				return;
			}
		}

		if( $act eq "remove" && $r_req)
		{
			unless( $user->is_owner( $eprint ) )
			{
				$session->render_error(
					$session->html_phrase( "cgi/users/review:cant_remove" ) );
				return;
			}
			&removal_request_form( $session, $eprint );
			return;
		}

		if( $act eq "checkout" && $checkout)
		{
			unless( $user->is_owner( $eprint ) )
			{
				$session->render_error(
					$session->html_phrase( "cgi/users/review:cant_checkout" ) );
				return;
			}
			$eprint->move_to_inbox;
			$session->redirect( 'submit?eprintid='.$eprint->get_id.'&_action_edit=1' );
			return;
		}
	}

	# If submit is defined, must have been a removal request form that was
	# filled out.
	if( $action eq "send" )
	{
		&handle_removal_request( $session, $user );
		return;
	}
		
	# no valid action or "cancel" from the remove request.
	&make_form( $session );
}



##
## remove_request_form( $session, $eprint )
##
##  Display a removal request form.
##

sub removal_request_form
{
	my( $session, $eprint ) = @_;

	my $title = $session->html_phrase( "cgi/users/review:remove_title" );
	my $page = $session->make_doc_fragment();

	$page->appendChild( $session->html_phrase( "cgi/users/review:remove_blurb",
		citation=>$eprint->render_citation_link() ) );

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


	my $reason_field = EPrints::MetaField->new(
		repository=> $session->get_repository,
		confid=>'remove_request',
		name=>'reason',
		type=>'longtext',
		input_rows=>15 );

	$page->appendChild(
		$session->render_input_form(
			fields=>[ $reason_field ],
			show_help=>1,
			default_action=>"send",
			buttons=>{
				_order=> [ "send","cancel" ],
				send => $session->phrase( "cgi/users/review:send" ),
				cancel => $session->phrase( "cgi/users/review:cancel" )
			},
			hidden_fields=>{
				eprintid => $eprint->get_value( "eprintid" )
			},
			dest=>"review" ) );

	$session->build_page( $title, $page, "review_remove_form" );
	$session->send_page();
	return;
}


##
## $wrote_page = handle_removal_request( $session, $user )
##
##  Handle the POSTed information from a removal request form.
##

sub handle_removal_request
{
	my( $session, $user ) = @_;
	
	# Get the EPrint we're dealing with
	my $ds = $session->get_repository->get_dataset( "archive" );	

	my $eprint = EPrints::DataObj::EPrint->new( 
		$session, 
		$session->param( "eprintid" ),
		$ds );
	if( !defined $eprint )
	{
		$session->render_error(
			$session->html_phrase( "cgi/users/review:cant_find_it" ) );
		return;
	}		
	if( !$user->is_owner( $eprint ) )
	{
		$session->render_error(
			$session->html_phrase( "cgi/users/review:cant_remove" ) );
		return;
	}

	my $reason = $session->param( "reason" );
	my $url = $session->get_repository->get_conf( "perl_url" ).
		"/users/staff/edit_eprint?eprintid=".$eprint->get_value( "eprintid" );
	
	my $rc = $session->mail_administrator(
		"cgi/users/review:remove_mail_subject",
		"cgi/users/review:remove_mail",
		citation=>$eprint->render_citation(),
		user=>$user->render_description(),
		email=>$user->render_value( "email" ),
		reason=>( EPrints::Utils::is_set( $reason ) ? $session->make_text( $reason ) 
			 : $session->html_phrase( "cgi/users/review:no_reason" ) ),
		url=>$session->make_text( $url ) );
	if( !$rc )
	{
		$session->render_error(
			$session->html_phrase( "general:email_failed" ) );
		return;
	}

	my $title = $session->html_phrase( "cgi/users/review:remove_title" );
	my $page = $session->make_doc_fragment();
	$page->appendChild( $session->html_phrase( "cgi/users/review:done" ) );
	$page->appendChild( $session->html_phrase( "general:userhome_link" ) );
	$session->build_page( $title, $page, "review_remove_done" );
	$session->send_page();
	
	return;
}

