<& /Admin/Elements/Header, Title => $title &>
<& /Admin/Elements/Tabs &>


<& /Elements/TitleBoxStart, title => %$title  &>
<FORM METHOD="POST" ACTION="<%$RT::WebPath%>/Admin/Keywords/Modify.html">
<INPUT TYPE="hidden" NAME="id" VALUE="<% $id %>">
Keyword <INPUT NAME="Name" VALUE="<% $Keyword->Name %>"><BR>

Parent <SELECT NAME="Parent">
	      <OPTION VALUE=""<% defined($Keyword->Parent) ? '' : ' SELECTED' %>>-</OPTION>
%while ( $parent = $parents->Next ) {
	      <OPTION VALUE="<% $parent->id %>"<% defined($Keyword->Parent) && $parent->id == $Keyword->Parent ? ' SELECTED' : '' %>><% $parent->Name %></OPTION>
%}
</SELECT>


New children of this keyword. one per line.
<TEXTAREA NAME="Kids" ROWS=4><% $kidstring %></TEXTAREA>
<BR>

<& /Elements/Submit, Label => $submit &>
</FORM>
<& /Elements/TitleBoxEnd &>

<%INIT>

my $Keyword = new RT::Keyword($session{CurrentUser});
my ($title, $submit, %kids, $kid);

if ( $Create ) {
    $title = "Create a new Keyword";
    $submit = "Create";
    $id = "new";
    %kids = ();
    $Parent = ''; #silence 
} 
else {
    if ( $id eq 'new' ) {
	$id = $Keyword->Create( Name => $Name, Parent => $Parent )
	  or Abort("can't create keyword Name=>$Name, Parent=>$Parent");
    } else {
	$Keyword->Load($id) || Abort("Can't load keyword id $id");
	
	#foreach my $field ( grep eval "defined(\$$_)", qw( Name Parent )) {
	#  eval "\$Keyword->Set(\$field=>\$$field); #sigh
	#}
	
	$Keyword->SetName($Name) if defined($Name);
	$Keyword->SetParent($Parent) if defined($Parent);
    }

    $title = "Modify the Keyword <B>". $Keyword->Name. "</B>";
    $submit = "Modify";
}


my $kids = $Keyword->Children(new RT::Keywords($session{CurrentUser}));

$kids{$kid->Name} = $kid while $kid = $kids->Next;

if ( defined($Kids) ) {
    my %newkids;

	%newkids = map { $_=>1 } split(/\r/, $Kids);

    }
    foreach ( grep { ! defined($newkids{$_}) } keys %kids ) {
	$kids{$_}->Delete;
	delete $kids{$_};
    }
    foreach ( grep { ! defined($kids{$_}) } keys %newkids ) {
	$kids{$_} = new RT::Keyword($session{CurrentUser});
	$kids{$_}->Create( Name => $_, Parent => $id )
	  or Abort("can't create keyword Name=>$_, Parent=>$id");
    }

}


my $parent;
my $parents = new RT::Keywords($session{CurrentUser});
$parents->UnLimit;

my $kidstring = join("\r", keys %kids);

</%INIT>

<%ARGS>
$id => undef
$Create => undef
$Name => undef
$Parent => undef
$Kids => undef

</%ARGS>
