package EPrints::Plugin::InputForm::Component::USQ_DepositorEmail;

use EPrints::Plugin::InputForm::Component;

@ISA = ( "EPrints::Plugin::InputForm::Component" );

use Unicode::String qw(latin1);

use strict;

sub new
{
	my( $class, %opts ) = @_;

	my $self = $class->SUPER::new( %opts );

	$self->{name} = "USQ_DepositorEmail";
	$self->{visible} = "all";
	$self->{surround} = "Light" unless defined $self->{surround};
	return $self;
}

=pod

=item $bool = $component->parse_config( $dom )

Parses the supplied DOM object and populates $component->{config}

=cut

sub parse_config
{
	my( $self, $dom ) = @_;

	$self->{config}->{dom} = $dom;
}

=pod

=item $content = $component->render_content()

Returns the DOM for the content of this component.

=cut


sub render_content
{
	my( $self ) = @_;

	# Grab the session for easy access
	my $session = $self->{session};
	
	# Create the root node we will return
	my $input = $session->make_element( "div" );	

	# Grab a copy of the current item and the relevant ID's
	my $item = $self->{workflow}->{item};
	my $new_depositor_id = $item->get_value("new_depositor_assignment");
	my $current_depositor_id = $item->get_value("userid");
	
	if( $new_depositor_id == 0 )
	{
		$input->appendChild( $session->html_phrase( "Plugin/InputForm/Component/USQ_DepositorEmail:new_depositor_id_unset" ) );
		return $input;
	}
	
	# Grab the user dataset so we can get the user details
	my $user_ds = $session->get_repository->get_dataset( "user" );
	
	# Get the new and current depositors from the system.
	my $new_depositor = $user_ds->get_object($session, $new_depositor_id);
	my $current_depositor = $user_ds->get_object($session, $current_depositor_id);
	
	# Translate the new depositor's name into a text object so we can embed it into the email
	my $new_depositor_name = $user_ds->get_field("name")->render_single_value( $session, $new_depositor->get_value("name") )->textContent;
	my $current_depositor_name = $user_ds->get_field("name")->render_single_value( $session, $current_depositor->get_value("name") )->textContent;
	
	# Generate the link and output
	my $href = 'mailto:'. $new_depositor->get_value("email") .'?cc='. $current_depositor->get_value("email"); 
	$href .= '&subject=' . $session->html_phrase( "Plugin/InputForm/Component/USQ_DepositorEmail:email_message_subject")->toString;
	$href .= '&body='.$session->html_phrase( "Plugin/InputForm/Component/USQ_DepositorEmail:email_message_body", 
			new_depositor=>$session->make_text($new_depositor_name), 
			current_depositor=>$session->make_text($current_depositor_name),
			eprint_url=>$session->make_text($item->get_url)
		)->toString;
	my $link = $session->make_element( "a", href => $href );
	$link->appendChild( $session->html_phrase( "Plugin/InputForm/Component/USQ_DepositorEmail:mail_depositors" ) );
	$input->appendChild( $link );
	return $input;
}

1;






