######################################################################
#
#  Add a 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 the permissions

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



my $page = $session->make_doc_fragment();
if( $session->seen_form() )
{
	&process_form( $session , $page );
}
else
{
	&render_form( $session , $page );
}


sub render_form
{
	my( $session , $page ) = @_;

	$page->appendChild( $session->html_phrase( "cgi/users/add_user:enter_username" ) );
	my $userds = $session->get_repository->get_dataset( "user" );

	$page->appendChild( $session->render_input_form(
		show_names=>1,
		show_help=>1,
		fields=>[
			$userds->get_field( "username" ),
			$userds->get_field( "usertype" )
		],
		buttons=>{ 
			createuser=>$session->phrase( "cgi/users/add_user:create_user" ) 
		} 
	) );
	
	$session->build_page(
		$session->html_phrase( "cgi/users/add_user:create_user_title" ),
		$page,
		"adduser" );
	$session->send_page();
}

$session->terminate();

sub process_form
{
	my( $session , $page ) = @_;

	# First ensure something's been entered
	my $candidate_username = $session->param( "username" );

	unless( EPrints::Utils::is_set( $candidate_username ) )
	{
		$page->appendChild(
			$session->html_phrase( "cgi/users/add_user:no_username" ) );
		$page->appendChild( $session->render_ruler() );
		&render_form( $session , $page );
		return;
	}

	if( defined EPrints::DataObj::User::user_with_username( $session, $candidate_username ) )
	{
		$page->appendChild(
			$session->html_phrase( "cgi/users/add_user:user_exists",
				username=>$session->make_text( $candidate_username ) ) );
		$page->appendChild( $session->render_ruler() );
		&render_form( $session , $page );
		return;
	}


	my $usertype = $session->param( "usertype" );
	if( !defined $usertype )
	{
		$usertype = $session->get_repository->get_conf( "default_user_type" ); 
	}

	# Attempt to create a new account
	my $user_ds = $session->get_repository->get_dataset( "user" );

	my $new_user = $user_ds->create_object( $session, { 
				usertype=>$usertype,
				username=>$candidate_username } );
	
	unless( defined $new_user )
	{
		$session->render_error(
			$session->html_phrase( "cgi/users/add_user:mk_acc_err" ) );
		return;
	}

	# Created the user OK, send to edit screen
	$session->redirect(
			"edit_user?username=$candidate_username" );
}

