######################################################################
#
#  EPrints Subject Editor
#
######################################################################
#
#  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 strict;

use EPrints;

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

# Check we have privs
if( !$session->auth_check( "edit-subject" ) )
{
	$session->terminate();
	exit( 0 );
}
my( $title, $page );

my( $subid ) = $session->param( "subjectid" );
if( defined $subid )
{
	mkpage_editsubject( $session, $subid );
	
}
else
{
	#cjg How About Whole Lot on this page - all subjects?

	$session->build_page( 
		$session->html_phrase( "cgi/users/edit_subject:intro_title" ),
		$session->html_phrase( "cgi/users/edit_subject:intro_blurb",
			continuelink=>$session->render_link( "edit_subject?subjectid=".$EPrints::DataObj::Subject::root_subject ) ),
		"edit_subject_intro" ); 
	$session->send_page();
}

$session->terminate();

exit;

#######################################################

sub mkpage_editsubject
{
	my( $session, $subid ) = @_;

	my $subject_ds = $session->get_repository->get_dataset( "subject" );
	my $archive_ds = $session->get_repository->get_dataset( "archive" );
	my $buffer_ds = $session->get_repository->get_dataset( "buffer" );
	my $subject = EPrints::DataObj::Subject->new( $session, $subid );
	if( !defined $subject )
	{
		$session->render_error( 
			$session->html_phrase( "cgi/users/edit_subject:error_badsubject" ), 
			"edit_subject" );
		return;
	}
	
	
	my( $title, $page );
	my $page = $session->make_doc_fragment();

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

	if( defined $action )
	{
		if( $action eq "submit" )
		{
			$subject->set_value( "name", $subject_ds->get_field( "name" )->form_value( $session ) );
			my $d =  $subject_ds->get_field( "name" )->form_value( $session );

			my $depositable = $subject_ds->get_field( "depositable" )->form_value( $session );
			$subject->set_value( "depositable", $depositable );
			$subject->commit();
		}
		if( $action =~ m/^unlink_(.*)$/ )
		{
			my $victimid = $1;
			my $victim = EPrints::DataObj::Subject->new( $session, $victimid );
				foreach( @{$victim->get_value( "parents" )} )
				{
				}
			if( !defined $victim )
			{
				#cjg render an error?	
			}
			elsif( scalar @{$victim->get_value( "parents" )}==1 )
			{
				$victim->remove();
			}
			else
			{
				my @newparents = ();
				foreach( @{$victim->get_value( "parents" )} )
				{
					push @newparents,$_ if($_ ne $subject->get_value("subjectid"));
				}
				$victim->set_value( "parents", \@newparents );
				$victim->commit();
			}
		}
		if( $action eq "add" )
		{
			my $newid = $session->param( "newsubjectid" );
			if( EPrints::Utils::is_set( $newid ) )
			{
				my $newchild = EPrints::DataObj::Subject->new( $session, $newid );
				if( defined $newchild )
				{
					if( grep( /^$newid$/, @{$subject->get_value( "ancestors" )} ) )
					{
						push @problems, $session->html_phrase( "cgi/users/edit_subject:problem_ancestor" );
					}
					else
					{
						my @parents = @{$newchild->get_value( "parents" )}; 
						push @parents, $subject->get_value( "subjectid" );
						$newchild->set_value( "parents", \@parents );
						$newchild->commit();
					}
				}
				else
				{
					my $new_subject = $subject_ds->create_object( $session, {
						subjectid => $newid,
						name => ''
						parents => [ $subject->get_value( "subjectid" ) ],
						depositable => 1 } );

					my $newname = $session->param( "name" );
					$newsubject->set_value( 
						"name", 
						{ $session->get_lang => $newname } );
					my $depositable = $subject_ds->get_field( "depositable" )->form_value( $session );
					$newsubject->set_value( "depositable", $depositable );
					$newsubject->commit;
#					$subid = $newid;
				}
			}
			else
			{
				push @problems, $session->html_phrase( "cgi/users/edit_subject:problem_noid" );
			}
		}
	}
	$page->appendChild( $session->html_phrase( "cgi/users/edit_subject:subjectid", 
		id=>$session->make_text( $subject->get_value( "subjectid" ) ) ) );
	$page->appendChild( $session->render_ruler() );

	if( scalar @problems )
	{	
		my $probdiv = $session->make_element( "div", class=>"problems" );
		my $ul = $session->make_element( "ul" );
		$probdiv->appendChild( $session->html_phrase( "cgi/users/edit_subject:problems" ) );
		$probdiv->appendChild( $ul );
		foreach( @problems )
		{
			my $li = $session->make_element( "li" );
			$li->appendChild( $_ );
			$ul->appendChild( $li );
		}
		$page->appendChild( $probdiv );
	}
		
		

	my @children = $subject->children();

	my @ids = @{$subject->get_value( "ancestors" )};
	foreach( @children )
	{
		push @ids, $_->get_value( "subjectid" );
	}
	$page->appendChild( $session->html_phrase( "cgi/users/edit_subject:location" ) );
	$page->appendChild( $session->render_subjects( \@ids, undef, $subid, 1 ) );

	if( $subid ne $EPrints::DataObj::Subject::root_subject )
	{
		my $div = $session->make_element( 	
			"div", 
			style=>"margin: 1em; padding: 1em; border: solid black 2px;" );
		$div->appendChild( $session->html_phrase( 
			"cgi/users/edit_subject:edit_heading",
			subid=>$session->make_text( $subid ) ) );
        	$div->appendChild(
                	$session->render_input_form(
                        	fields=>[ 
					$subject_ds->get_field( "name" ),
					$subject_ds->get_field( "depositable" )
				],
                        	values=>{ 
					name=>
					  $subject->get_value( "name" ),
					depositable=>
					  $subject->get_value( "depositable" )
				},
                        	show_names=>1,
                        	show_help=>1,
				default_action => "submit",
                        	buttons=>{ submit => $session->phrase( "cgi/users/edit_subject:action_submit" ) },
                        	hidden_fields=>{ subjectid => $subid },
                        	dest=>"edit_subject" ) );

		$page->appendChild( $div );
	}


	my $form = $session->render_form( "post" );
	$form->appendChild( $session->render_hidden_field( "subjectid", $subid ) );
	$page->appendChild( $form );

	my( $table, $tr, $td, $th, $a );
	$table = $session->make_element( "table", border=>1, cellpadding=>4 );

	$tr = $session->make_element( "tr" );
	
	$th = $session->make_element( "th" );
	$th->appendChild( $session->html_phrase( "cgi/users/edit_subject:subject" ) );
	$tr->appendChild( $th );

	$th = $session->make_element( "th" );
	$th->appendChild( $session->html_phrase( "cgi/users/edit_subject:inarchive" ) );
	$tr->appendChild( $th );

#	$th = $session->make_element( "th" );
#	$th->appendChild( $session->html_phrase( "cgi/users/edit_subject:inbuffer" ) );
#	$tr->appendChild( $th );

	$th = $session->make_element( "th" );
	$th->appendChild( $session->html_phrase( "cgi/users/edit_subject:nparents" ) );
	$tr->appendChild( $th );

	$th = $session->make_element( "th" );
	$th->appendChild( $session->html_phrase( "cgi/users/edit_subject:nchildren" ) );
	$tr->appendChild( $th );

	$table->appendChild( $tr );


	foreach( @children )
	{
		$tr = $session->make_element( "tr" );

		$td = $session->make_element( "td", align=>"left");
		$a = $session->render_link( "edit_subject?subjectid=".$_->get_value( "subjectid" ) );
		$a->appendChild( $_->render_description() );
		$td->appendChild( $a );
		$tr->appendChild( $td );
		
		$td = $session->make_element( "td", align=>"center");
		$td->appendChild( $session->make_text( $_->count_eprints( $archive_ds ) ) );
		$tr->appendChild( $td );

#		$td = $session->make_element( "td", align=>"center");
#		$td->appendChild( $session->make_text( $_->count_eprints( $buffer_ds ) ) );
#		$tr->appendChild( $td );

		my $parents_n = scalar @{$_->get_value( "parents" )};
		my $children_n = scalar $_->children();

		$td = $session->make_element( "td", align=>"center");
		$td->appendChild( $session->make_text( $parents_n ) );
		$tr->appendChild( $td );
		
		$td = $session->make_element( "td", align=>"center");
		$td->appendChild( $session->make_text( $children_n ) );
		$tr->appendChild( $td );
		
		$td = $session->make_element( "td" );
		if( $children_n == 0 )
		{
			$td->appendChild( $session->render_action_buttons( 
				"unlink_".$_->get_value( "subjectid" ) =>
					$session->phrase( "cgi/users/edit_subject:action_".($parents_n == 1?"delete":"unlink") ) ) );
		}
		$tr->appendChild( $td );
		$table->appendChild( $tr );
	}
	$form->appendChild( $session->html_phrase( "cgi/users/edit_subject:children" ) );
	$form->appendChild( $table );

#	$form = $session->render_form( "post" );
#	$form->appendChild( $session->render_hidden_field( "subjectid", $subid ) );
#	$page->appendChild( $form );
#	#$form->appendChild( $newidfield->render_input_field( $session ) );
#	$form->appendChild( $newnamefield->render_input_field( $session ) );
#	$form->appendChild( $session->render_action_buttons( 
#		"add" => $session->phrase( "cgi/users/edit_subject:action_add" ) ) );
#	$page->appendChild( $session->render_ruler );

	my $f2div = $session->make_element( 
		"div", 
		style=>"margin: 1em; padding: 1em; border: solid black 2px;" );

	my $newidfield = $subject_ds->get_field( "subjectid" )->clone();
	$newidfield->set_property( "name", "newsubjectid" );
	my $newnamefield = $subject_ds->get_field( "name" )->clone();
	$newnamefield->set_property( "multilang", 0 );
	$f2div->appendChild( $session->html_phrase( 
		"cgi/users/edit_subject:add_child",
		subid=>$session->make_text( $subid ) ) );
	$f2div->appendChild( $session->html_phrase( "cgi/users/edit_subject:add_child_help" ) );
	$f2div->appendChild( $session->render_input_form(
                       	fields=>[ 
				$newidfield,
				$newnamefield,
				$subject_ds->get_field( "depositable" )
			],
			values =>{ depositable=>"TRUE" },
                       	show_names=>1,
                       	show_help=>1,
			default_action => "add",
                       	buttons=>{ add => $session->phrase( "cgi/users/edit_subject:action_add" ) },
                       	hidden_fields=>{ subjectid => $subid },
                       	dest=>"edit_subject" ) );

	$page->appendChild( $f2div );

	$title = $session->html_phrase( "cgi/users/edit_subject:title", subjectname=>$subject->render_description() ), 

	$session->build_page( $title, $page, "edit_subject" );
	$session->send_page();

}
