%# BEGIN LICENSE BLOCK
%# 
%# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
%# 
%# (Except where explictly superceded by other copyright notices)
%# 
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%# 
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%# General Public License for more details.
%# 
%# Unless otherwise specified, all modifications, corrections or
%# extensions to this work which alter its source code become the
%# property of Best Practical Solutions, LLC when submitted for
%# inclusion in the work.
%# 
%# 
%# END LICENSE BLOCK
<& /Elements/Header, Title => $title &>
<& /User/Elements/GroupTabs, 
    GroupObj => $Group, 
    current_subtab => "User/Groups/Members.html?id=".$Group->id, 
    Title => $title &>
<& /Elements/ListActions, actions => \@results &>



<FORM ACTION="<%$RT::WebPath%>/User/Groups/Members.html" METHOD="POST">
<INPUT TYPE=HIDDEN NAME=id VALUE="<%$Group->Id%>">
<TABLE WIDTH="100%">
<TR>
<TD>
<&|/l&>Add members</&>
</TD>
<TD>
<&|/l&>Current members</&>
</TD>
</TR>

<TR>
<TD VALIGN=TOP>
<& /Admin/Elements/SelectNewGroupMembers, Name => "AddMembers", Group => $Group &>
</TD>
<TD VALIGN=TOP>

% if ($Group->MembersObj->Count == 0 ) {
<i><&|/l&>(No members)</&></i>
% } else {
<i><&|/l&>(Check box to delete)</&></i>
<br>
<br>
<&|/l&>Users</&>
% my $UserMembers = $Group->MembersObj;
% $UserMembers->LimitToUsers();
<UL>
% while (my $member = $UserMembers->Next()) {
<LI><INPUT TYPE=CHECKBOX Name="DeleteMember-<%$member->MemberId%>">
<%$member->MemberObj->Object->Name%> (<%$member->MemberObj->Object->RealName%>)
% }
</ul>
<&|/l&>Groups</&>
<ul>
% my $GroupMembers = $Group->MembersObj;
% $GroupMembers->LimitToGroups();
% while (my $member = $GroupMembers->Next()) {
<LI><INPUT TYPE=CHECKBOX Name="DeleteMember-<%$member->MemberId%>">
<%$member->MemberObj->Object->Name%>
% }
% }
</UL>
</TD>
</TR>
</TABLE>
<& /Elements/Submit, Label => loc('Modify Members') &>
</form>


<%INIT>

my $Group = new RT::Group($session{'CurrentUser'});
$Group->Load($id) ;

unless ($Group->id) {
    Abort(loc('Could not load group'));
}

my (@results);

foreach my $key (keys %ARGS) {

if ($key =~ /^DeleteMember-(\d+)$/) {
    my $mem_id = $1; 
    my ($val,$msg) = $Group->DeleteMember($mem_id);
    push (@results, $msg);
}
}

# Make sure AddMembers is always an array
my @AddMembersUsers = (ref $AddMembersUsers eq 'ARRAY') ? @{$AddMembersUsers} : ($AddMembersUsers);
my @AddMembersGroups = (ref $AddMembersGroups eq 'ARRAY') ? @{$AddMembersGroups} : ($AddMembersGroups);

foreach my $member (@AddMembersUsers, @AddMembersGroups) {
    next unless ($member);

    my $principal;

    if ($member =~ /^Group-(\d+)$/) {
        $principal = RT::Group->new($session{'CurrentUser'});
        $principal->Load($1);
    } elsif ($member =~ /^User-(\d+)$/) {
        $principal = RT::User->new($session{'CurrentUser'});
        $principal->Load($1);
    } else {
        next;
    }


    my ($val, $msg) = $Group->AddMember($principal->PrincipalId);
    push (@results, $msg);
}


my $title = loc('Editing membership for personal group [_1]', $Group->Name);

</%INIT>

<%ARGS>
$AddMembersUsers => undef
$AddMembersGroups => undef
$id => undef
</%ARGS>
