%#-- Begin HTML --#
<& '/widgets/wrappers/sharky/header.mc',
   title => "$disp Profile",
   context => "Admin | Profile | $disp | $crumb"
 &>

<form method="post" action="<% $r->uri %>" name="user_profile" onsubmit="if ( checkLogin(this.login, <% LOGIN_LENGTH %>, this.pass_1, this.pass_2, <% PASSWD_LENGTH %>) ){ return confirmChanges(this)} else { return false}" autocomplete="off">

<%perl>;
$m->comp("/widgets/wrappers/sharky/table_top.mc", caption => "Name",
	 number => 1);
# Output the ID, if there is one.
$m->comp('/widgets/profile/hidden.mc', value => $id, name => 'user_id')
  if $isid;

# Display the major fields.
$m->comp('/widgets/profile/dumpRemainingFields.mc', objref => $user,
	 readOnly => $no_edit,
	 fieldsUsed => { active => 1, password => 1,
			 login  => 1,  name    => 1 });

$m->comp("/widgets/wrappers/sharky/table_bottom.mc");

# Display the Contact info.
$m->comp("/widgets/wrappers/sharky/table_top.mc",
	 caption => "Contacts", number  => 2);
my $contacts = $user->get_contacts;
$m->comp('/widgets/add_more/add_more.mc', type => 'contact',
     fields => [qw(type value)], name => 'contact', read_only => $no_edit,
     reset_key => $id, objs => $contacts, incr => 2, useTable => 0,
     formName => 'user_profile',
     deleteLabelOnly => 1, param => \%ARGS);
$m->comp("/widgets/wrappers/sharky/table_bottom.mc");


# Get the group's membership listed
$m->comp("/widgets/grp_membership/grp_membership.mc",
         grp_class => 'Bric::Util::Grp::User',
         obj => $user,
         formName => 'user_profile',
         no_edit => $no_edit || ($is_user && !$can_edit),
         widget  => $type,
         all => 1,
         no_cb => 1,
         num => 3,
);

$m->comp("/widgets/wrappers/sharky/table_top.mc", number  => 4,
	 caption => "Login and Password");

# Display the login and password info.
$m->comp("/widgets/profile/displayFormElement.mc",
	 objref => $user, key => 'login', readOnly => $no_edit);

if (!$no_edit) {

    # output a state variable for pw checking javascript:
    $m->out(qq{<script language="javascript">\nvar newUser = }
      . ($isid ? 'false' : 'true') . "\n</script>\n");

    # Old password.
    $m->comp("/widgets/profile/password.mc", name => 'old_pass',
	     disp => 'Old password', length => $pass_len,
	     maxlength => $pass_max) if $is_user;
    my $req = ! $isid;
    # New password.
    $m->comp("/widgets/profile/password.mc", name => 'pass_1',
	     disp => 'New password', req => $req, length => $pass_len,
	     maxlength => $pass_max);
    # New password again.
    $m->comp("/widgets/profile/password.mc", name => 'pass_2',
	     disp => 'Repeat new password', req => $req, length => $pass_len,
	     maxlength => $pass_max);
}
$m->comp("/widgets/wrappers/sharky/table_bottom.mc");

my @prefs = Bric::Util::Pref->list({ can_be_overridden => 1 });

if (@prefs) {
    $m->comp('/widgets/wrappers/sharky/table_top.mc',
             caption => 'User Preferences',
             number => 5 );
    $m->comp('/widgets/listManager/listManager.mc',
             behavior => 'narrow',
             object => 'pref',
             addition => undef,
             profile =>
             sub {
                 [($can_edit || $is_user ? 'Edit' : 'View'),
                  "/admin/profile/user_pref/" . $_[0]->get_id,
                  'user_id=' . $id]
             },
             select  => sub {
                 my $up = Bric::Util::UserPref->lookup({
                     user_id => $id,
                     pref_id => $_[0]->get_id });
                 $up ? ['Reset', 'user_pref|delete_cb', $up->get_id] : undef;
             },
             fields => [qw(name description val_name)],
             alter => { val_name =>
                        sub { my $up =
                                  Bric::Util::UserPref->lookup
                                      ({ user_id => $id,
                                         pref_id => $_[1]->get_id });

                              if ($up) {
                                  return escape_html($up->get_val_name);
                              } else {
                                  escape_html($_[0])
                                  . ' (' . $lang->maketext('Default Value') . ')'
                              } } },
             objs => \@prefs,
            );
    $m->comp('/widgets/wrappers/sharky/table_bottom.mc');
}

</%perl>

<& '/widgets/profile/formButtons.mc', type => $type, section => $section,
   no_del => $no_del, no_save => $no_edit, return => $return, widget => $type &>

</form>

<& '/widgets/wrappers/sharky/footer.mc', param => \%ARGS &>
%#-- End HTML --#

<%once>;
my $class = 'Bric::Biz::Person::User';
my $section = 'admin';
my $type = 'user';
my $disp = get_disp_name($type);
# Grab the length setting for the password fields.
my $pass_len = $class->my_meths->{password}{props}{length};
my $pass_max = $class->my_meths->{password}{props}{maxlength};
my $port = LISTEN_PORT == 80 ? '' : ':' . LISTEN_PORT;
</%once>
<%args>
$id => undef
</%args>

<%init>;
$id = $ARGS{user_id} unless defined $id;
# Instantiate an object.
my $user = $ARGS{obj} ? $ARGS{obj} : defined $id ? $class->lookup({ id => $id})
  : $class->new;
$id = $user->get_id unless defined $id;
my $isid = defined $id && $id ne '';

# Check authorization - but allow a user to edit his/her own profile.
my ($no_edit, $no_del, $is_user, $can_edit);
if (defined $id && $id == get_user_id()) {
    ($no_edit, $no_del, $is_user) = (0, 1, 1);
    $can_edit = chk_authz($user, ($isid ? EDIT : CREATE), 1);
} else {
    chk_authz($user, $isid ? READ : CREATE);
    $can_edit = chk_authz($user, ($isid ? EDIT : CREATE), 1);
    $no_edit = !$can_edit;
    $no_del = !$id || $no_edit;
}

# Get the name for the breadcrumb trail.
my $crumb = $user->format_name;
$crumb = $crumb ? "&quot;$crumb&quot;" : 'New';

# Roll in any changes to the user object if we're just adding contacts.
if (exists($ARGS{'addmore_type'}) && $ARGS{'addmore_type'} eq 'contact') {
    foreach my $meth ($user->my_meths(1)) {
	$meth->{set_meth}->($user, @{$meth->{set_args}}, $ARGS{$meth->{name}})
	  if defined $meth->{set_meth};
    }
}

# Establish the return URI.
my $return = get_state_name('login') eq 'ssl' ? last_page
      : 'http://' . $r->hostname . $port . last_page;
</%init>


<%doc>
###############################################################################

=head1 NAME

/admin/profile/user/dhandler - User Profile.

=head1 VERSION

$LastChangedRevision$

=head1 DATE

$LastChangedDate$

=head1 DESCRIPTION

This dhandler manages user profiles.

</%doc>
