%#-- Begin HTML --#
<& '/widgets/wrappers/sharky/header.mc',
   title => "$disp Profile",
   context => "Admin | Profile | $disp | $crumb"
 &>
<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 || $is_perm,
	);

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

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

if (defined $id) {
    unless ($is_perm) {
        # 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);
        $m->print("\n");

        # 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;

        if ($class_name eq 'Bric::Util::Grp::Source') {
            # Get existing members.
            foreach my $obj ( $grp->get_objects ) {
                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') {
            # Get existing members.
            foreach my $obj ( $grp->get_objects ) {
                push @$right, { value =>  $obj->get_id,
                                description => $obj->get_uri,
                                site  => $obj->get_site_id };
            }
            # Get potential members.
            foreach my $obj ( $memb_pkg->list ) {
                push @$left, { value =>  $obj->get_id,
                               description => $obj->get_uri,
                               site => $obj->get_site_id };
            }
        } else {
            my $robjs = $grp->get_objects;
            my $lobjs = $memb_pkg->list({ active => 1 });

            my $has_site;
            $has_site = 1 if $class_name ne 'Bric::Util::Grp::ElementType'
              && ($lobjs->[0] && $lobjs->[0]->HAS_MULTISITE
                  || $robjs->[0] && $robjs->[0]->HAS_MULTISITE);

            # Get existing members.
            foreach my $obj ( @$robjs ) {
                push @$right, { value =>  $obj->get_id,
                                description => $obj->get_name($format),
                                site => $has_site && $obj->get_site_id };
            }
            # Get potential members.
            foreach my $obj ( @$lobjs ) {
                push @$left, { value =>  $obj->get_id,
                               description => $obj->get_name($format),
                               site => $has_site && $obj->get_site_id };
            }
        }

        # Add to the description site name, if there are duplicate
        # descriptions. Only run if sites are already cached (which means
        # there is more than one site).
        my $sites = $c->get('__SITES__') || Bric::Biz::Site->list({ active => 1 });
        if(@$sites > 1) {

            my %sites;
            $sites{$_->get_id} = $_->get_name for @$sites;
            for my $row (@$left, @$right) {
                if ($row->{site}) {
                    $row->{description} .= ' ('. $sites{$row->{site} } . ') ';
                }
            }
        }

        # All of these negative assertion variable names are annoying!
        my $no_member_manage = $no_edit;
        unless ($no_member_manage) {
            if ($memb_pkg eq 'Bric::Biz::Person::User') {
                # No member management only if the current user is a global admin
                # or a member of the group.
                $no_member_manage = !user_is_admin
                && !$grp->has_member(get_user_object);
            } else {
                # No member management if the current user does not already have
                # permisssion to edit the members of the group.
                $no_member_manage = !chk_authz(0, EDIT, 1, $id);
            }
        }

        # 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_member_manage,
                  leftCaption  => $no_member_manage ? undef : "Available $pl_disp",
                  showLeftList => !$no_member_manage || 0,
                  rightCaption => "Current $pl_disp" );

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

        # Output group membership.
        $m->out("<br />\n");
        $m->comp("/widgets/grp_membership/grp_membership.mc",
                 grp_class => 'Bric::Util::Grp::Grp',
                 obj => $grp,
                 formName => 'grp_profile',
                 no_edit => $no_edit,
                 widget => $widget,
                 num => 3,
        );

    }
</%perl>

<div style="float: left;">
<& '/widgets/profile/formButtons.mc', type => $type, return => '/admin/manager/grp',
	    section => $section, no_del => $no_del, no_save => $no_edit,
        widget => $type
&>
</div>
   
<%perl>;
    # Add the buttons.
    $m->out('<div style="float: right; text-align: right;">');
    $m->comp('/widgets/profile/imageSubmit.mc',
	     formName => "grp_profile",
	     callback => "$type|permissions_cb",
         alt      => 'Permissions',
	     image    => "permissions_dgreen"
	) if $id != ADMIN_GRP_ID && chk_authz(0, READ, 1, $id);
    # Comment on the above line: We exclude the admin group because it has no
    # permissions. We also exclude users who don't have permission to access
    # the objects of this group already so as to prevent them from giving
    # themselves greater permissions than they already have.
    $m->out('</div>');

} 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',
             widget => $type);
}
</%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;
    }
    # XXX Dual-purpose class.
    $class_opt->{'Bric::Util::Grp::Person'} = 'Contributor Group';
}

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;

# Check authorization.
chk_authz($grp, $id ? READ : CREATE);
my $no_edit = !chk_authz($grp, ($id ? EDIT : CREATE), 1);
my $is_perm = $grp->get_permanent;
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_key_name eq 'contrib_type'
  ? 'Contributor Group'
  : $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

$LastChangedRevision$

=head1 DATE

$LastChangedDate$

=head1 DESCRIPTION

Diplays the profile for a single group.

</%doc>
