######################################################################
#
#  Change Language Tool
#
######################################################################
#
#  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
#
######################################################################

#cjg Not lang'd

use EPrints;

use strict;

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


my $referer = $session->param( "fromurl" );
if( !EPrints::Utils::is_set( $referer ) )
{
	$referer = EPrints::Apache::AnApache::header_in( 
				$session->get_request,
				'Referer' );
}

my $newlang = $session->param( "langid" );

my @langids = @{$session->get_repository->get_conf( "languages" )};

if( !defined $newlang )
{
	my $page = $session->make_doc_fragment;
	$page->appendChild( $session->html_phrase( "cgi/set_lang:blurb" ) );
	my $langid;
	foreach $langid ( @langids )
	{	
		my $p = $session->make_element( "p" );
		my $a = $session->render_link( "set_lang?langid=$langid&fromurl=$referer" );
		$a->appendChild( $session->render_language_name( $langid ) );
		$p->appendChild( $a );
		$page->appendChild( $p );
	}

	my $p = $session->make_element( "p" );
	my $a = $session->render_link( "set_lang?langid=&fromurl=$referer" );
	$a->appendChild( $session->html_phrase( "cgi/set_lang:clear_cookie" ) );
	$p->appendChild( $a );
	$page->appendChild( $p );
	
	$session->build_page( $session->html_phrase( "cgi/set_lang:title" ), $page, "set_lang_choice" );
        $session->send_page();
}
elsif( $newlang eq "" )
{
	my $page = $session->make_doc_fragment;
	my $langid = EPrints::Session::get_session_language( $session->get_repository, $session->get_request );
	$session->change_lang( $langid );
	$page->appendChild( $session->html_phrase( 
			"cgi/set_lang:cleared",
			lang=>$session->render_language_name( $langid )));

	$page->appendChild( $session->html_phrase( 
			"cgi/set_lang:default_lang",
			lang=>$session->render_language_name( 
				$session->get_repository->get_conf( "defaultlanguage" ) ) ) );
	$page->appendChild( $session->html_phrase( "cgi/set_lang:cache_warning" ) );
	if( 0 && EPrints::Utils::is_set( $referer ) )
	{
		$page->appendChild( $session->html_phrase( "cgi/set_lang:back_to_lastpage",
			link => $session->render_link( $referer ) ) );
	}
	$page->appendChild( $session->html_phrase( "general:frontpage_link" ) );

	$session->build_page( $session->html_phrase( "cgi/set_lang:lang_changed" ), $page, "setlang_done" );
        $session->send_page( lang=>$newlang );
}
elsif( grep(/^$newlang$/,@langids) )
{
	my $page = $session->make_doc_fragment;
	$session->change_lang( $newlang );
	$page->appendChild( $session->html_phrase( 
			"cgi/set_lang:changed_to",
			lang=>$session->render_language_name( $newlang )));

	$page->appendChild( $session->html_phrase( 
			"cgi/set_lang:default_lang",
			lang=>$session->render_language_name( 
				$session->get_repository->get_conf( "defaultlanguage" ) ) ) );
	$page->appendChild( $session->html_phrase( "cgi/set_lang:cache_warning" ) );
	if( 0 && EPrints::Utils::is_set( $referer ) )
	{
		$page->appendChild( $session->html_phrase( "cgi/set_lang:back_to_lastpage",
			link => $session->render_link( $referer ) ) );
	}
	$page->appendChild( $session->html_phrase( "general:frontpage_link" ) );

	$session->build_page( $session->html_phrase( "cgi/set_lang:lang_changed" ), $page, "setlang_done" );
        $session->send_page( lang=>$newlang );
}
else 
{
	my $page = $session->make_doc_fragment;
	$page->appendChild( $session->html_phrase( "cgi/set_lang:unknown_lang",
		id=>$session->make_text( $newlang ) ) );
	$page->appendChild( $session->html_phrase( "general:frontpage_link" ) );

	$session->build_page( $session->html_phrase( "cgi/set_lang:failed" ), $page, "setlang_failed" );
        $session->send_page();
}

$session->terminate();

