######################################################################
#
#  Subscription page
#
# Not yet working!!cjg
#
######################################################################
#
#  This file is part of EPrints 2.
#  
#  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::Database;
use EPrints::EPrint;
use EPrints::Session;
use EPrints::Subscription;

use strict;
use vars qw($action_subscribe $action_create $action_remove);

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

my $user = EPrints::User::current_user( $session );

# Check user
if( !defined $user )
{
	# Can't find the user
	$session->render_error( 
		$session->html_phrase( "cgi/users/subscribe:dont_know_you" ) );
	$session->terminate();
	exit;
}

my @all_subs = EPrints::Subscription::subscriptions_for( $session, $user );


my $subscription = undef;

$subscription = $all_subs[0] if( scalar @all_subs > 0 );

$action_subscribe = $session->{lang}->phrase( "cgi/users/subscribe:subscribe" );
$action_create    = $session->{lang}->phrase( "cgi/users/subscribe:create" );
$action_remove    = $session->{lang}->phrase( "cgi/users/subscribe:remove" );

# Do we have an action?
my $action = $session->{render}->param( "submit" );


if( defined $action && $action eq $action_subscribe )
{
	# User wants to update subscription info.
	if( defined $subscription )
	{
		my $problems = $subscription->from_form();
		
		if( defined $problems )
		{
			&edit_subscription( $session, $subscription, $problems );
		}
		else
		{
			&update_subscription( $session, $subscription );
		}
	}
}
elsif( defined $action && $action eq $action_create )
{
	# Create a new subscription if necessary
	if( !defined $subscription )
	{
		$subscription = EPrints::Subscription->create( $session,
		                                               $user->{username} );
	}

	if( !defined $subscription )
	{		
		$session->render_error(
			$session->html_phrase( "cgi/users/subscribe:mk_sub_error" ),
			$session->get_archive()->get_conf( "userhome" ),
			$session->html_phrase( "cgi/users/subscribe:return_dep" ) );
	}
	else
	{
		&edit_subscription( $session, $subscription, undef );
	}
}
elsif( defined $action && $action eq $action_remove )
{
	# Remove the user's subscription. For now, remove the first in the list
	# (which should only ever have one in it anyway.)
	if( defined $subscription )
	{
		my $success = $subscription->remove();
		if( $success )
		{
			&no_subscription( $session );
		}
		else
		{
			$session->render_error(
				$session->html_phrase( "cgi/users/subscribe:rm_sub_error" ),
				$session->get_archive()->get_conf( "userhome" ),
				$session->html_phrase( "cgi/users/subscribe:return_dep" ) );
		}
	}
}
else
{
	# Must have come straight here

	# If the user has a subscription, let them edit it, otherwise offer them
	# the option of creating a subscription
	if( !defined $subscription )
	{
		# No subscription yet
		&no_subscription( $session );
	}
	else
	{
		# Let them edit the one they have
		&edit_subscription( $session, $subscription, undef );
	}
}

$session->terminate();



######################################################################
#
# no_subscription( $session )
#
#  Offer the user the chance to create a subscription
#
######################################################################

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


	print $session->{render}->start_html( 
		$session->{lang}->phrase( "cgi/users/subscribe:subscribe" ) );
	
	print "<CENTER><P>";
	print $session->{lang}->phrase( "cgi/users/subscribe:no_sub" );
	print "</P></CENTER>\n";
	
	print $session->{render}->start_form();

	print "<CENTER><P>";	
	print $session->{render}->submit_buttons( [ $action_create ] );
	print "</P></CENTER>\n";
	
	print $session->{render}->end_form();
	
	print $session->{render}->end_html();
}


######################################################################
#
# edit_subscription( $session, $subscription, $problems )
#
#  Show the subscription form
#
######################################################################

sub edit_subscription
{
	my( $session, $subscription, $problems ) = @_;


	print $session->{render}->start_html( 
		$session->{lang}->phrase( "cgi/users/subscribe:subscribe" ) );
	
	if( defined $problems )
	{
		print "<P>";
		print $session->{lang}->phrase( "cgi/users/subscribe:cant_accept" );
		print "</P>\n<UL>\n";
		foreach (@$problems)
		{
			print "<LI>$_</LI>\n";
		}
		print "</UL>\n";
	}

	print "<CENTER><P>";
	print $session->{lang}->phrase( "cgi/users/subscribe:please_enter" );
	print "</P></CENTER>\n";
	
	print $session->{render}->start_form();
	print $subscription->render_subscription_form();

	print "<CENTER><P>";
	print $session->{render}->submit_buttons( [ $action_subscribe,
	                                            $action_remove ] );
	print "</P></CENTER>\n";
	print $session->{render}->end_form();
	
	print $session->{render}->end_html();
}


######################################################################
#
# update_subscription( $session, $subscription )
#
#  Call when subscription has been updated, with no problems. Displays
#  a message.
#
######################################################################

sub update_subscription
{
	my( $session, $subscription ) = @_;

	my $success = $subscription->commit();


	if( $success )
	{
		print $session->{render}->start_html( 
			$session->{lang}->phrase( "cgi/users/subscribe:subscribe" ) );
		print "<CENTER><P><STRONG>";
		print $session->{lang}->phrase( "cgi/users/subscribe:sub_accept" );
		print "</STRONG></P></CENTER>";
		print "<CENTER><P><A HREF=\"".$session->get_archive()->get_conf( "userhome" ),
		print $session->{lang}->phrase( "cgi/users/subscribe:ret_deposit" );
		print "</A></P></CENTER>\n";
		print $session->{render}->end_html();
	}
	else
	{
		$session->render_error(
			$session->html_phrase( "cgi/users/subscribe:ud_sub_error" ),
			$session->get_archive()->get_conf( "userhome "),
			$session->html_phrase( "cgi/users/subscribe:ret_deposit" ) );
	}
}
