rt 4.2.15
[freeside.git] / rt / share / html / REST / 1.0 / Forms / group / default
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2018 Best Practical Solutions, LLC
6 %#                                          <sales@bestpractical.com>
7 %#
8 %# (Except where explicitly superseded by other copyright notices)
9 %#
10 %#
11 %# LICENSE:
12 %#
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %#
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %#
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %#
29 %#
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %#
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %#
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %#
47 %# END BPS TAGGED BLOCK }}}
48 %# REST/1.0/Forms/group/default
49 %#
50 <%ARGS>
51 $id
52 $format => 's'
53 $fields => undef # these are the fields passed to the rt "-f" flag.
54 $changes => {}
55 </%ARGS>
56 <%perl>
57 my @comments;
58 my ($c, $o, $k, $e) = ("", [], {}, 0);
59 my %data = %$changes;
60 my $group = RT::Group->new($session{CurrentUser});
61
62 my @fields = qw(Name Description Disabled);
63 if ( $fields && %$fields ) {
64     @fields = grep { exists $fields->{ lc $_ } } @fields;
65 }
66
67 my %fields = map { lc $_ => $_ } @fields;
68
69 if ($id ne 'new') {
70     $group->Load($id);
71     if (!$group->Id) {
72         return [ "# Group $id does not exist.", [], {}, 1 ];
73     }
74 }
75 else {
76     if (%data == 0) {
77         return [
78             "# Required: Name",
79             [ qw(id Name Description) ],
80             {
81                 id => "group/new",
82                 Name => "",
83                 Description => ""
84             },
85             0
86         ];
87     }
88     else {
89         my %v;
90         my %create = %fields;
91         $create{name}         = "Name";
92         $create{description}   = "Description";
93         # Do any fields need to be excluded here?
94
95         foreach my $k (keys %data) {
96             if (exists $create{lc $k}) {
97                 $v{$create{lc $k}} = delete $data{$k};
98             }
99         }
100
101         $group->CreateUserDefinedGroup(%v);
102         unless ($group->Id) {
103             return [ "# Could not create group.", [], {}, 1 ];
104         }
105
106         $id = $group->Id;
107         delete $data{id};
108         push(@comments, "# Group $id created.");
109         goto DONE if %data == 0;
110     }
111 }
112
113 if (%data == 0) {
114     my @data;
115
116     push @data, [ id => "group/".$group->Id ];
117     foreach my $key (@fields) {
118         push @data, [ $key => $group->$key ];
119     }
120
121     # Members
122     unless ( $fields && !exists $fields->{members} ) {
123         my $gms = [];
124         my $GroupMembers = $group->MembersObj();
125         while ( my $mo = $GroupMembers->Next() ) {
126             if ( $mo->MemberObj->IsGroup ) {
127                 my $us = $mo->MemberObj->Object->UserMembersObj();
128                 my @users;
129                 while ( my $u = $us->Next() ) {
130                     push @users, $u->RealName . ' <' . $u->EmailAddress . '>';
131                 }
132                 push @$gms,
133                     'GROUP ['
134                     . $mo->MemberObj->Object->Name . ']' . ' ('
135                     . join( ';', @users ) . ')';
136             } elsif ( $mo->MemberObj->IsUser ) {
137                 push @$gms,
138                     $mo->MemberObj->Object->RealName . ' <'
139                     . $mo->MemberObj->Object->EmailAddress . '>';
140             }
141         }
142         push @data, [ Members => $gms ];
143     }
144
145     # Custom fields
146     my $CustomFields = $group->CustomFields;
147     while ( my $CustomField = $CustomFields->Next() ) {
148         next
149             unless ( !%$fields
150             || exists $fields->{ lc "CF-" . $CustomField->Name } );
151         next unless $CustomField->CurrentUserHasRight('SeeCustomField');
152         my $CFvalues = $group->CustomFieldValues( $CustomField->Id );
153         my @CFvalues;
154         while ( my $CFvalue = $CFvalues->Next() ) {
155             push @CFvalues, $CFvalue->Content;
156         }
157         push @data, [ "CF-" . $CustomField->Name => \@CFvalues ];
158     }
159
160     my %k = map {@$_} @data;
161     $o = [ map {$_->[0]} @data ];
162     $k = \%k;
163 }
164 else {
165     my ($get, $set, $key, $val, $n, $s);
166     my $updated;
167     foreach $key (keys %data) {
168         $val = $data{$key};
169         $key = lc $key;
170         $n = 1;
171
172         if ($key eq 'name' || $key eq 'description' || exists $fields{$key})
173         {
174             if (exists $fields{$key}) {
175                 $key = $fields{$key};
176             }
177             else {
178                 $key = "Description" if $key eq 'description';
179                 $key = "Name" if $key eq 'name';
180             }
181             $set = "Set$key";
182
183             next if $val eq $group->$key;
184             ($n, $s) = $group->$set($val);
185         }
186         elsif ($key ne 'id') {
187             $n = 0;
188             $s = "Unknown field.";
189         }
190
191     SET:
192         if ($n == 0) {
193             $e = 1;
194             push @comments, "# $key: $s";
195             unless (@$o) {
196                 my %o = keys %$changes;
197                 delete @o{"id", @fields};
198                 @$o = ("id", @fields, keys %o);
199                 $k = $changes;
200             }
201         }
202         else {
203             $updated ||= 1;
204         }
205     }
206
207     push(@comments, "# Group $id updated.") if $updated;
208 }
209
210 DONE:
211 $c ||= join("\n", @comments) if @comments;
212 return [ $c, $o, $k, $e ];
213 </%perl>