%# One learns to itch where one can scratch.  -- Ernest Bramah
  <tr> 
    <td class="tableEmboss">
% if (@results) {
<ul>
%   foreach $result (grep defined, @results) {
<li><% loc($result) %></li>
%   }
</ul>
% }
      <table width="100%" border="0" cellspacing="4" cellpadding="0" height="100%">
        <tr> 
          <td valign="top"> 
<form name="userform" id="userform" style="margin: 0" action="index.html" method="post">
<input type="hidden" NAME="Begin" VALUE="<% $Begin %>">
%if ($Create) {
<INPUT TYPE=HIDDEN NAME="User" VALUE="new">
% } else {
<INPUT TYPE=HIDDEN NAME="User" VALUE="<%$UserObj->Id%>">
% }
<TABLE WIDTH=100% BORDER=0>
% if ($OriginObj) {
<TR><TD ALIGN="RIGHT"><&|/l&>Member since</&><BR><BR></TD>
    <TD><% eval { $OriginObj->CreatedObj->ISO } %><BR><BR></TD></TR>
% }
% while (my ($name, $text) = splice(@Fields, 0, 2)) {
<TR><TD ALIGN="RIGHT"><% loc($text) %></TD><TD>
%     if ($name eq 'Lang') {
<& /Elements/SelectLang, Name => 'Lang', Default => $UserObj->Lang &>
%     } else {
<input id="<% $name %>" name="<% $name %>" value="<%$UserObj->$name%>" <% $disabled %>>
%     }
%     if ($name eq 'Name') {
<b><&|/l&>(required)</&></b>
%     }
</TD></TR>
% }
% if ($Create) {
<script><!--
document.getElementById('Name').focus();
--></script>
% }
% unless ($RT::WebExternalAuth and !$RT::WebFallbackToInternalAuth) {
<TR>
<TD ALIGN=RIGHT>
<&|/l&>New Password</&>:
</TD>
<TD ALIGN=LEFT>
<input type=password name="Pass1" <% $disabled %>>
</TD>
</TR>
% }
% if ($RT::Foundry) {
<TR><TD ALIGN="RIGHT">
<&|/l&>Personal Homepage</&>: 
</TD><TD>
%    if (exists $ARGS{'PersonalHomepage'} and $OriginObj) { eval {
%       $OriginObj->AddCustomFieldValue(
%	    Field => RT::CustomField->NameToId('PersonalHomepage'),
%	    Value => $ARGS{'PersonalHomepage'},
%	);
%    } }
<input name="PersonalHomepage" value="<% eval { $ARGS{'PersonalHomepage'} || $OriginObj->CustomFieldValue('PersonalHomepage') } %>" <% $disabled %>>
</TD></TR>
<TR><TD ALIGN="RIGHT">
<&|/l&>About Me</&>: 
</TD><TD>
%    if (exists $ARGS{Description} and $OriginObj) {
%        my $Object = $OriginObj->Transactions->First->ContentObj;
%	 $Object->__Set( Field => 'Content', Value => $ARGS{Description});
%    }
<textarea name="Description" cols=20 rows=5 <% $disabled %>><% eval { $OriginObj->Transactions->First->Content } %></TEXTAREA>
</TD></TR>
% if ($OriginObj) {
<TR><TD ALIGN="RIGHT">
<&|/l&>Public Info</&>: 
</TD><TD>
% ProcessTicketCustomFieldUpdates(ARGSRef => \%ARGS);
<& /Work/Tickets/Elements/EditCustomField, TicketObj => $OriginObj, CustomField => $OriginObj->QueueObj->CustomField('Public Info'), Queue => $OriginObj->Queue, NamePrefix => 'Ticket-' . $OriginObj->Id . '-CustomField-', Loc => 1 &>
<BR>
</TD></TR>
% }
% } else {
<TR><TD ALIGN="RIGHT">
<&|/l&>Extra info</&>: 
</TD><TD>
<textarea name="FreeformContactInfo" cols=20 rows=2 <% $disabled %>><%$UserObj->FreeformContactInfo%></TEXTAREA>
</TD></TR>
<TR><TD ALIGN="RIGHT">
<&|/l&>Comments about this user</&>:
</TD><TD>
<TEXTAREA name="Comments" COLS=20 ROWS=3 WRAP=VIRTUAL <% $disabled %>><%$UserObj->Comments%>
</TEXTAREA>
</TD></TR>
<TR><TD ALIGN="RIGHT">
<&|/l&>Signature</&>:
</TD><TD>
<TEXTAREA COLS=20 ROWS=3 name="Signature" WRAP=HARD
<% ($UserObj->Privileged) ? '' : 'DISABLED' %>>
<%$UserObj->Signature%></TEXTAREA>
</TD></TR>
% }
<TR><TD ALIGN="RIGHT">
<INPUT TYPE=HIDDEN NAME="SetEnabled" VALUE="1">
<INPUT TYPE=CHECKBOX NAME="Enabled" VALUE="1" <%$EnabledChecked%> <% $disabled %>>
</TD><TD>
<&|/l&>Let this user access RT</&><BR>
</TD></TR>
<TR><TD ALIGN="RIGHT">
<INPUT TYPE=HIDDEN NAME="SetPrivileged" VALUE="1">
<INPUT TYPE=CHECKBOX NAME="Privileged" VALUE="1" <%$PrivilegedChecked%> <% $disabled %>>
</TD><TD>
<&|/l&>Let this user be granted rights</&>
</TD></TR>
	    </TABLE>
<input type="submit" style="display: none" value="<&|/l&>Submit</&>">
          </td>
        </tr>
      </table>
    </td>
  </tr>

<%INIT>
my $current_tab;
my $UserObj = $Item;
my ($title, $PrivilegedChecked, $EnabledChecked, $Disabled, $result);

my ($val, $msg);

my @results;

if ($User eq 'new' and length($Name)) {
    ($val, $msg) = $UserObj->Create(
	 Name => $Name,
	 EmailAddress => $ARGS{'EmailAddress'}
    );
    # set the id, so the the menu will have the right info
    $User = $UserObj->Id;	
    $m->print("<script><!--\nlocation.href='index.html?DoSearch=1&UserString=&id=$User';\n--></script>");
}


# If we have a user to modify, lets try. 
if ($UserObj->Id) {
    my @fields = qw(Name Comments Signature EmailAddress FreeformContactInfo 
		    Organization RealName NickName Lang EmailEncoding WebEncoding 
		    ExternalContactInfoId ContactInfoSystem Gecos ExternalAuthId 
		    AuthSystem HomePhone WorkPhone MobilePhone PagerPhone Address1
		    Address2 City State Zip Country 
		   );

    # clear old untouched users
    if (length(my $email = $ARGS{EmailAddress})) {
	if ($email ne $UserObj->EmailAddress) {
	    my $user = RT::User->new($session{CurrentUser});
	    if ($user->LoadByEmail( $email )) {
		if ($user->Name eq $email) {
		    $user->SetEmailAddress('');
		}
	    }
	}
    }

    push @results, UpdateRecordObject(
	AttributesRef => \@fields,
	Object => $UserObj,
	ARGSRef => \%ARGS
    );

    # Deal with special fields: Privileged, Enabled and Password
    if  ( ($SetPrivileged) and ( $Privileged != $UserObj->Privileged) ) {
	push @results, $UserObj->SetPrivileged($Privileged);
    }

    #we're asking about enabled on the web page but really care about disabled.
    $Disabled = ($Enabled == 1) ? 0 : 1;

    if  ( ($SetEnabled) and ( $Disabled != $UserObj->Disabled) ) { 
	push @results, ($UserObj->SetDisabled($Disabled))[1];
    }


    if ((defined $Pass1) and ($Pass1 ne '') and (!$UserObj->IsPassword($Pass1))) {
	push @results, ($UserObj->SetPassword($Pass1))[1];
	my $obj = eval { $UserObj->OriginObj };
	$obj->SetCustomFieldValue( Password => $Pass1 ) if $obj;
    }

    eval { $UserObj->_expire( $UserObj->_gen_primary_cache_key()) }
}

# Do some setup for the ui
unless ($UserObj->Disabled()) {
    $EnabledChecked ="CHECKED";
}

if ($UserObj->Privileged()) {  
    $PrivilegedChecked = "CHECKED";
}

my @Fields = (
    Name	    => 'Username',
    EmailAddress    => 'Email',
    RealName	    => 'Real Name',
    Lang	    => 'Language',
($RT::Foundry ? () : (
    NickName	    => 'Nickname',
    Gecos	    => 'Unix login',
)),
);

my $disabled = (($Create or $UserObj->Id) ? '' : 'disabled');

my $OriginObj;
if ($RT::Foundry) {
    $OriginObj = $UserObj->OriginObj;
}
</%INIT>
<%ARGS>
$Item => $RT::Nothing
$User => undef
$Name  => undef
$Comments  => undef
$Signature  => undef
$EmailAddress  => undef
$FreeformContactInfo => undef
$Organization  => undef
$RealName  => undef
$NickName  => undef
$Privileged => undef
$SetPrivileged => undef
$Enabled => undef
$SetEnabled => undef
$Lang  => undef
$EmailEncoding  => undef
$WebEncoding => undef
$ExternalContactInfoId  => undef
$ContactInfoSystem  => undef
$Gecos => undef
$ExternalAuthId  => undef
$AuthSystem  => undef
$HomePhone => undef
$WorkPhone  => undef
$MobilePhone  => undef
$PagerPhone  => undef
$Address1 => undef
$Address2  => undef
$City  => undef
$State  => undef
$Zip  => undef
$Country => undef
$Pass1 => undef
$Create=> undef
$Begin => 0
</%ARGS>
