######################################################################
#
#  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::User;
use EPrints::Subscription;
use EPrints::Session;
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 $subs_ds = $session->get_archive->get_dataset( "subscription" );

# handle user response

if( $action eq "new" )
{
	my $newsub = EPrints::Subscription->create( $session, $user->get_id );
	edit_subscription( $session, $subs_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::Subscription->new( $session, $subid );
	if( !defined $subscr )
	{
		$session->get_archive->log( "Attempt by user #".
			$session->current_user->get_id." to edit ".
			"subscription #".$subid." which does not ".
			"exist." );
		&show_subscriptions( $session, $subs_ds, $user );
	}
	elsif( $subscr->get_value( "userid" ) != $user->get_id )
	{
		$session->get_archive->log( "Attempt by user #".
			$session->current_user->get_id." to edit ".
			"subscription #".$subid." which does does not ".
			"belong to them." );
		&show_subscriptions( $session, $subs_ds, $user );
	}
	else
	{
		if( $action eq "finish" )
		{
			$subscr->set_value( 
				"spec",
				$subs_ds->get_field( "spec" )->form_value(
					$session ) );
			$subscr->set_value( 
				"frequency",
				$subs_ds->get_field( 
					"frequency" )->form_value(
						$session ) );
			$subscr->set_value( 
				"mailempty",
				$subs_ds->get_field( 
					"mailempty" )->form_value(
						$session ) );
			$subscr->commit()
			&show_subscriptions( $session, $subs_ds, $user );
		}
		else
		{
			edit_subscription( $session, $subs_ds, $user, $subscr );
		}
	}
}
elsif( $action =~ /^finish$/ )
{
	my $subid = $session->param( "subid" );
}
elsif( $action =~ /^remove_([0-9]+)$/ )
{
	my $subscr = EPrints::Subscription->new( $session, $1 );
	if( !defined $subscr )
	{
		$session->get_archive->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_archive->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, $subs_ds, $user );
}
else
{
	&show_subscriptions( $session, $subs_ds, $user );
}
$session->terminate();


sub show_subscriptions
{
	my( $session, $subs_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( $subs_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, $subs_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 => [
				$subs_ds->get_field( "spec" ),
				$subs_ds->get_field( "frequency" ),
				$subs_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();
}
