######################################################################
#
#  EPrints Author Record Editing for Staff
#
######################################################################
#
#  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::Database;
use EPrints::Session;
use EPrints::UserForm;

use strict;

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

# Check we have privs
if( !$session->auth_check( "edit-user" ) )
{
	$session->terminate();
	exit( 0 );
}

&process( $session );

$session->terminate();

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

	my $user = EPrints::UserPage::user_from_param( $session );

	return if( !defined $user );

	my $action = $session->get_action_button();

	my( $page, $title );
	$page = $session->make_doc_fragment();

	if( $action eq "delete" )
	{
		my( $userinfo, $usertitle ) = $user->render_full();
		$page->appendChild( $userinfo );
		$page->appendChild( $session->render_ruler() );
		$page->appendChild( $session->html_phrase( "cgi/users/edit_user:really_remove" ) );
		$page->appendChild( $session->render_input_form(
			default_action=>"confirm",
			buttons=>{
				_order => [ "confirm", "cancel" ],
				confirm=>$session->phrase( "cgi/users/edit_user:action_confirm" ),
				cancel=>$session->phrase( "cgi/users/edit_user:action_cancel" ) 
			},
			hidden_fields=>{
				userid=>$user->get_value( "userid" )
			}
		) );			

		$session->build_page(
			$session->html_phrase( "cgi/users/edit_user:delete_title" ),
			$page,
			"delete_user_form" );
		$session->send_page();

		return;
	}

	if( $action eq "confirm" )
	{
		my $userid = $user->get_value( "userid" );
		my $username = $user->get_value( "username" );

		my $success = $user->remove();

		if( !$success )
		{
			$session->render_error(
				$session->html_phrase( "cgi/users/edit_user:could_not_remove" ) );
			return;
		}
		
		$page->appendChild( $session->html_phrase( 
			"cgi/users/edit_user:deleted",
			username=>$session->make_text( $username ), 
			userid=>$session->make_text( $userid ) ) );

		$page->appendChild( $session->html_phrase( "general:userhome_link" ) );

		$session->build_page(
			$session->html_phrase( "cgi/users/edit_user:delete_title" ),
			$page,
			"delete_user_done" );
		$session->send_page();

		return;
	}

	if( $action eq "cancel" )
	{
		$session->redirect( $session->get_archive()->get_conf( "userhome" ) );
		return;
	}
	

	my $userform = new EPrints::UserForm(
		$session,
		$session->get_archive()->get_conf( "userhome" ),
		1,
		$user,
		"edit_user" );
	$userform->process();
}

