%#-- Begin HTML --#
<& '/widgets/wrappers/sharky/header.mc',
   title => "$disp Profile",
   context => "Admin | Profile | $disp | $crumb"
 &>
<& '/javascripts/profile/formValidator.js', id => $id &>
<form method="post" action="<% $r->uri %>" name="grp_profile" onSubmit="return confirmChanges(this)">
<%perl>;
$m->comp('/widgets/profile/hidden.mc', value => $id, name => 'grp_id')
  if defined $id;

$m->comp("/widgets/wrappers/sharky/table_top.mc",
	 caption => "Properties",
	 number  => 1);

my $is_admin = defined $id && $id == ADMIN_GRP_ID;
$m->comp('/widgets/profile/hidden.mc',
	 value => $grp->get_name, name => 'name') if $is_admin;

$m->comp('/widgets/profile/displayFormElement.mc',
	 key => 'name',
	 objref => $grp,
	 readOnly => $no_edit || $is_admin
	);

$m->comp('/widgets/profile/displayFormElement.mc',
	 key => 'description',
	 objref => $grp,
	 readOnly => $no_edit
	);

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

if (defined $id) {
    # It's an existing group. Give no object choice.
    $m->comp("/widgets/wrappers/sharky/table_top.mc",
	     caption => "Members", number  => 2);

    # Store the class name.
    $m->comp('/widgets/profile/hidden.mc', name => 'grp_type',
	     value => $class_name);

    # Create a double list so they can move objects around.
    my ($right, $left) = ([], []);
    # Grab the Bric::Util::Class object describing the member class.
    my $memb_class = $grp->member_class;
    my $memb_pkg = $memb_class->get_pkg_name;
    my $pl_disp = $memb_class->get_plural_name;

    # Get the List Name Format, for people.
    my $format = $memb_pkg eq 'Bric::Biz::Person'
      || $memb_pkg eq 'Bric::Biz::Person::User'
      ? get_pref('List Name Format') : undef;

    # Get the existing members.
    if ($class_name eq 'Bric::Util::Grp::Source') {
	foreach my $mem ( $grp->get_members ) {
	    my $obj = $mem->get_object;
	    push @$right, { value =>  $obj->get_id,
			    description => $obj->get_source_name };
    }
	# Get potential members.
	foreach my $obj ( $memb_pkg->list ) {
	    push @$left, { value =>  $obj->get_id,
			   description => $obj->get_source_name };
	}
    } elsif ($class_name eq 'Bric::Util::Grp::CategorySet') {
	foreach my $mem ( $grp->get_members ) {
	    my $obj = $mem->get_object;
	    push @$right, { value =>  $obj->get_id,
			    description => $obj->get_uri };
        }
	# Get potential members.
	foreach my $obj ( $memb_pkg->list ) {
	    push @$left, { value =>  $obj->get_id,
			   description => $obj->get_uri };
	}
    } else {
	foreach my $mem ( $grp->get_members ) {
	    my $obj = $mem->get_object;
	    push @$right, { value =>  $obj->get_id,
			    description => $obj->get_name($format) };
    }
	# Get potential members.
	foreach my $obj ( $memb_pkg->list ) {
	    push @$left, { value =>  $obj->get_id,
			   description => $obj->get_name($format) };
	}
    }
    # Load up the double-list manager.
    $m->comp( "/widgets/doubleListManager/doubleListManager.mc", rightSort => 1,
	      leftOpts => $left, rightOpts => $right, formName => 'grp_profile',
	      leftName => 'objects', rightName => 'members', readOnly => $no_edit,
	      leftCaption => $no_edit ? undef : "Available $pl_disp",
              showLeftList => !$no_edit || 0,
	      rightCaption => "Current $pl_disp" );


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

</%perl>
<table width=580 cellpadding=0 cellspacing=0>
<tr>
  <td width=265><&
    '/widgets/profile/formButtons.mc', type => $type, return => '/admin/manager/grp',
	     section => $section, no_del => $no_del, no_save => $no_edit
   &></td>
  <td width=265 align=right valign=top>
<%perl>

    # Add the buttons.
    $m->comp('/widgets/profile/imageSubmit.mc',
	     formName => "grp_profile",
	     callback => "$widget|permissions_cb",
	     image    => "permissions_dgreen",
             vspace   => 3
	    ) unless $id == ADMIN_GRP_ID;
    $m->out('</td></tr></table>');

} else {
    # It's a new object. Give 'em a choice.
    $m->comp("/widgets/wrappers/sharky/table_top.mc",
	     caption => "Group Type",
	     number  => 2);

    $m->comp('/widgets/profile/select.mc',
	     name => 'grp_type',
	     options => $class_opt,
	     value => get_package_name(get_state_name($type)));

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

    # Add the buttons.
    $m->comp('/widgets/profile/formButtons.mc', type => $type,
	     section => $section, no_del => $no_del, val => 'next_dkgreen');
}
</%perl>
</form>
<& '/widgets/wrappers/sharky/footer.mc', param => \%ARGS &>

%#-- End HTML --#


%#-- Once Section --#
<%once>;
my $class = 'Bric::Util::Grp';
# HACK: There should probably be a Bric::Util::Grp class method or somesuch
# to load the Bric::Util::Grp subclass class objects.
my $class_opt;
{
    my $classes = Bric::Util::Class->pkg_href;
    while (my ($k, $v) = each %$classes) {
	next unless $k =~ /^bric::util::grp::/;
	next if index($k, 'member') != -1;
	my $pkg = $v->get_pkg_name;
	next if $pkg->get_secret;
	$class_opt->{$pkg} = $v->get_disp_name;
    }
}

my $section = 'admin';
my $type = 'grp';
my $widget = 'profile';
</%once>

%#-- Args Section --#
<%args>
$id => undef
</%args>

%#-- Init Section --#
<%init>;
# Instantiate an object.
my $grp = $ARGS{obj} ? $ARGS{obj} : defined $id ? $class->lookup({ id => $id})
  : $class->new;
$id ||= $grp->get_id;

# Disallow access to the permanent groups.
$m->comp('/errors/403.mc', obj => $grp) if $grp->get_permanent;

# Check authorization.
chk_authz($grp, $id ? READ : CREATE);
my $no_edit = !chk_authz($grp, ($id ? EDIT : CREATE), 1);
my $no_del = !$id || $no_edit || $grp->get_permanent || $id == ADMIN_GRP_ID;
my $class_obj = $grp->my_class;
my $class_name = $class_obj->get_pkg_name;
my $disp = $class_obj->get_disp_name;

# Get the name for the breadcrumb trail.
my $crumb = $grp->get_name;
$crumb = $crumb ? "&quot;$crumb&quot;" : 'New';
</%init>

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

=head1 NAME

/admin/profile/grp/dhandler - Group profile interface

=head1 VERSION

$Revision: 1.6.4.1 $

=head1 DATE

$Date: 2002/11/07 00:57:39 $

=head1 DESCRIPTION

Diplays the profile for a single group.

</%doc>
