######################################################################
#
#  Add, Remove and Edit a the subscriptions for the current user.
#
######################################################################
#
#  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( "subscription" ) )
{
	$session->terminate();
	exit( 0 );
}

my $user = $session->current_user;
my $action = $session->get_action_button();
my $subject_ds = $session->get_repository->get_dataset( "subscription" );

# handle user response

if( $action eq "new" )
{
	my $newsub = $subject_ds->create_object( $session, { userid=>$user->get_id } );
	edit_subscription( $session, $subject_ds, $user, $newsub );
} 
elsif( $action =~ /^edit_([0-9]+)$/ || $action eq "finish" )
{
	my $subid;
	if( $action eq "finish" )
	{
		$subid = $session->param( "subid" );
	}
	else
	{
		$subid = $1;
	}

	my $subscr = EPrints::DataObj::Subscription->new( $session, $subid );
	if( !defined $subscr )
	{
		$session->get_repository->log( "Attempt by user #".
			$session->current_user->get_id." to edit ".
			"subscription #".$subid." which does not ".
			"exist." );
		&show_subscriptions( $session, $subject_ds, $user );
	}
	elsif( $subscr->get_value( "userid" ) != $user->get_id )
	{
		$session->get_repository->log( "Attempt by user #".
			$session->current_user->get_id." to edit ".
			"subscription #".$subid." which does does not ".
			"belong to them." );
		&show_subscriptions( $session, $subject_ds, $user );
	}
	else
	{
		if( $action eq "finish" )
		{
			$subscr->set_value( 
				"spec",
				$subject_ds->get_field( "spec" )->form_value(
					$session ) );
			$subscr->set_value( 
				"frequency",
				$subject_ds->get_field( 
					"frequency" )->form_value(
						$session ) );
			$subscr->set_value( 
				"mailempty",
				$subject_ds->get_field( 
					"mailempty" )->form_value(
						$session ) );
			$subscr->commit()
			&show_subscriptions( $session, $subject_ds, $user );
		}
		else
		{
			edit_subscription( $session, $subject_ds, $user, $subscr );
		}
	}
}
elsif( $action =~ /^finish$/ )
{
	my $subid = $session->param( "subid" );
}
elsif( $action =~ /^remove_([0-9]+)$/ )
{
	my $subscr = EPrints::DataObj::Subscription->new( $session, $1 );
	if( !defined $subscr )
	{
		$session->get_repository->log( "Attempt by user #".
			$session->current_user->get_id." to remove ".
			"subscription #".$1." which does not ".
			"exist." );
	}
	elsif( $subscr->get_user->get_id == $session->current_user->get_id )
	{
		$subscr->remove;
	}
	else
	{
		$session->get_repository->log( "Attempt by user #".
			$session->current_user->get_id." to remove ".
			"subscription #".$subscr->get_id." owned by user ".
			"#".$subscr->get_user->get_id );
	}
	&show_subscriptions( $session, $subject_ds, $user );
}
else
{
	&show_subscriptions( $session, $subject_ds, $user );
}
$session->terminate();


sub show_subscriptions
{
	my( $session, $subject_ds, $user ) = @_;

	my $page = $session->make_doc_fragment;

	my $title = $session->html_phrase( "cgi/users/subscribe:list_title" );

	$page->appendChild( $session->html_phrase( 
		"cgi/users/subscribe:intro" ) );

	my @subs = $user->get_subscriptions;

	my( $form );
	$form = $session->render_form( "post", "subscribe" );	
	$page->appendChild( $form );

	if( scalar @subs == 0 )
	{
		$form->appendChild( $session->html_phrase( 
			"cgi/users/subscribe:no_subs" ) );
	}
	else
	{
		my( $table, $tr, $td, $th );
		$table = $session->make_element( 
			"table",
			style => "margin-bottom: 12pt",
			cellpadding => 4,
			cellspacing => 0,
			border => 1 );
		$form->appendChild( $table );
		
		foreach my $subscr ( @subs )
		{
			$tr = $session->make_element( "tr" );
			$table->appendChild( $tr );

			my $id = $subscr->get_id;
			my %buttons = (
				 _order => [ "edit_".$id, "remove_".$id ] );
			$buttons{"edit_".$id} = $session->phrase( 
				"cgi/users/subscribe:action_edit" );
			$buttons{"remove_".$id} = $session->phrase( 
				"cgi/users/subscribe:action_remove" );
			
			$td = $session->make_element( 
				"td",
				width=>"20%",
				align=>"center" );
			$tr->appendChild( $td );
			$td->appendChild( $session->render_action_buttons( 
				%buttons ) );
		
			$td = $session->make_element( "td" );
			$tr->appendChild( $td );

			foreach( "frequency","spec","mailempty" )
			{
				my $strong;
				$strong = $session->make_element( "strong" );
				$strong->appendChild( $subject_ds->get_field( $_ )->render_name( $session ) );
				$strong->appendChild( 
					$session->make_text( ": " ) );
				$td->appendChild( $strong );
				$td->appendChild( $subscr->render_value( $_ ) );
				$td->appendChild( $session->make_element( "br" ) );
			}

		}
		
	}

	$form->appendChild(
		$session->render_action_buttons( 	
			new => $session->phrase( 
				"cgi/users/subscribe:action_new" ) ) );
	$page->appendChild( $session->html_phrase( "general:userhome_link" ) );
	
	$session->build_page( $title, $page, "subscribe_list"  );
	$session->send_page();
}

sub edit_subscription
{
	my( $session, $subject_ds, $user, $subscr ) = @_;

	my $page = $session->make_doc_fragment;

	my $title = $session->html_phrase( "cgi/users/subscribe:list_title" );

	$page->appendChild( $session->html_phrase( 
		"cgi/users/subscribe:edit_blurb" ) );

	$page->appendChild(
		$session->render_input_form( 
			fields => [
				$subject_ds->get_field( "spec" ),
				$subject_ds->get_field( "frequency" ),
				$subject_ds->get_field( "mailempty" )
			],
			values => $subscr->get_data,
			show_names => 1,
			show_help => 1,
			hidden_fields => {
				subid => $subscr->get_value( "subid" ) } ,
			default_action => "finish",
			buttons => {
				cancel => $session->phrase( 
					"cgi/users/subscribe:action_cancel" ),
				finish => $session->phrase( 
					"cgi/users/subscribe:action_finish" )
			}
		) );
			
	
	$session->build_page( $title, $page, "subscribe_edit" );
	$session->send_page();
}
