%# 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
<& /Admin/Elements/Header, Title => "RT/Admin/Edit the group ". $Group->Name &>
<& /Admin/Elements/GroupTabs, GroupObj => $Group, 
    current_tab => 'Admin/Groups/Members.html?id='.$id, 
    Title => "RT/Admin/Edit the group ". $Group->Name &>
<& /Elements/ListActions, actions => \@results &>


<& /Elements/TitleBoxStart, title => loc('Editing membership for group [_1]', $Group->Name) &>

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

<TR>
<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>
<TD VALIGN=TOP>
<& /Admin/Elements/SelectNewGroupMembers, Name => "AddMembers", Group => $Group &>
</TD>
</TR>
</TABLE>
<& /Elements/TitleBoxEnd &>
<& /Elements/Submit, Label => loc('Modify Members'), Reset => 1 &>
</form>


<%INIT>

my $Group = new RT::Group($session{'CurrentUser'});
$Group->Load($id) || Abort(loc('Could not load group'));

my (@results);

my $key;
foreach $key (keys %ARGS) {

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

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

foreach my $member (@AddMembers) {
    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);
}


</%INIT>

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