#!/usr/bin/perl -w -I/opt/eprints2/perl_lib

use EPrints;
use strict;

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

# Check parameters
my $eprintid = $session->param( "eprintid" );
my $eprint = EPrints::DataObj::EPrint->new( $session, $eprintid );
if( !defined $eprint || $eprint->get_value( "metadata_visibility" ) eq "hide" ) 
{
	$session->render_error( $session->html_phrase( 
		"cgi/users/edit_eprint:cant_find_it", 
		id => $session->make_text( $eprintid ) ) );
	$session->terminate();
	exit( 1 );
}

# Check item isn't already OA
my $status = $eprint->get_value( "full_text_status" );
if( $status eq "public" )
{
	$session->render_error( $session->html_phrase( 
		"request/error:already_oa" ), 
		$eprint->get_url );
	$session->terminate();
	exit( 1 );
}

# Check contact email for this eprint
my $contact_email = undef;
if( $session->get_repository->can_call( "email_for_doc_request" ) ) 
{
	$contact_email = $session->get_repository->call( "email_for_doc_request", $session, $eprint );
}
if( !defined( $contact_email ) )
{
	$session->render_error( $session->html_phrase( 
		"request/error:no_contact" ), 
		$eprint->get_url );
	$session->terminate();
	exit( 1 );
}

my $title = $session->html_phrase( "request/request_page:title" );
my $page = $session->make_doc_fragment();

my $email = $session->param( "email" );
if( defined( $email ) && $email ne "" )
{
	# Send request email
	my $subject = $session->phrase( "request/request_email:subject", eprint => $eprint->get_value( "title" ) );
	my $mail = $session->make_element( "mail" );
	$mail->appendChild($session->html_phrase(
		"request/request_email:body", 
		eprint 		=> $eprint->render_citation_link,
		requester	=> $session->make_text( $email ) ) );

	my $result;
	my $user = EPrints::DataObj::User::user_with_email( $session, $contact_email );
	if( defined( $user ) && $status eq "restricted" )
	{
		# Contact is registered user and EPrints holds restricted documents
		# Send email to contact with accept/reject links
		my $url =  $session->get_repository->get_conf( "perl_url" ) . 
			"/users/respond_doc?eprintid=" . $eprint->get_id . 
			"&email=" . EPrints::Utils::encode_str( $session->param( "email" ) );

		$mail->appendChild( $session->html_phrase( "request/request_email:links",
			accept => $session->render_link( "$url&action=accept" ),
			reject => $session->render_link( "$url&action=reject" ) ) );

		$result = EPrints::Utils::send_mail(
			session => $session,
			langid => $session->get_langid,
			to_name => EPrints::Utils::tree_to_utf8( $user->render_description ),
			to_email => $contact_email,
			subject => $subject,
			message => $mail,
			sig => $session->html_phrase( "mail_sig" ),
		);
	} 
	else
	{
		# Contact is non-registered user or EPrints holds no documents
		# Send email to contact with 'replyto'
		$result = EPrints::Utils::send_mail(
			session => $session,
			langid => $session->get_langid,
			to_name => defined $user ? EPrints::Utils::tree_to_utf8( $user->render_description ) : "",
			to_email => $contact_email,
			subject => $subject,
			message => $mail,
			sig => $session->html_phrase( "mail_sig" ),
			replyto_email => $email,
		);

	}

	if( !$result )
	{
		$session->render_error( $session->html_phrase( 
			"request/error:email" ), 
			'?eprintid=' . $eprint->get_id );
		$session->terminate();
		exit( 1 );
	}
	else
	{
		# Send acknowledgement to requester
		# TODO: worry about the result?
		my $mail = $session->make_element( "mail" );
		$mail->appendChild( $session->html_phrase(
			"request/ack_email:body", 
			eprint	=> $eprint->render_citation_link ) );

		EPrints::Utils::send_mail(
			session => $session,
			langid => $session->get_langid,
			to_email => $email,
			subject => $session->phrase( "request/ack_email:subject", eprint=>$eprint->get_value( "title" ) ), 
			message => $mail,
			sig => $session->html_phrase( "mail_sig" )
		);

		# Render confirmation page
		$page->appendChild( $session->html_phrase( "request/ack_page",
			link => $session->render_link( $eprint->get_url ),
		) );
		$session->build_page( $title, $page );
		$session->send_page();
		$session->terminate();
		exit( 1 );
	}
}

# Render requested eprint
my $p = $session->make_element( "p" );
$page->appendChild( $p );
$p->appendChild( $eprint->render_citation );

# Render request form
my $form =  $session->render_form( "post" );
$page->appendChild( $form );
$form->appendChild( $session->html_phrase( "request/request_page:fieldname_email" ) );
$form->appendChild( $session->make_element( "input", type => "text", name => "email" ) );
$form->appendChild( $session->make_element( "br" ) );
$form->appendChild( $session->render_hidden_field( "eprintid", $eprintid ) );
$form->appendChild( $session->render_action_buttons( submit => $session->phrase( "request/request_page:action_request" ) ) );

$session->build_page( $title, $page );
$session->send_page();

$session->terminate();
