import rt 3.4.6
[freeside.git] / rt / html / REST / 1.0 / Forms / ticket / links
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
6 %#                                          <jesse@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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 %# 
27 %# 
28 %# CONTRIBUTION SUBMISSION POLICY:
29 %# 
30 %# (The following paragraph is not intended to limit the rights granted
31 %# to you to modify and distribute this software under the terms of
32 %# the GNU General Public License and is only of importance to you if
33 %# you choose to contribute your changes and enhancements to the
34 %# community by submitting them to Best Practical Solutions, LLC.)
35 %# 
36 %# By intentionally submitting any modifications, corrections or
37 %# derivatives to this work, or any other work intended for use with
38 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 %# you are the copyright holder for those contributions and you grant
40 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
41 %# royalty-free, perpetual, license to use, copy, create derivative
42 %# works based on those contributions, and sublicense and distribute
43 %# those contributions and any derivatives thereof.
44 %# 
45 %# END BPS TAGGED BLOCK }}}
46 %# REST/1.0/Forms/ticket/links
47 %#
48 <%ARGS>
49 $id
50 $format => 's'
51 $changes => undef
52 </%ARGS>
53 <%perl>
54 my @data;
55 my $ticket = new RT::Ticket $session{CurrentUser};
56
57 $ticket->Load($id);
58 if (!$ticket->Id) {
59     return [ "# Ticket $id does not exist.", [], {}, 1 ];
60 }
61
62 my ($c, $o, $k, $e) = ("", [], {}, 0);
63 my @fields = qw(DependsOn DependedOnBy RefersTo ReferredToBy Members MemberOf);
64 my %fields = map { lc $_ => $_ } @fields;
65
66 my %lfields = (
67     Members      => { Type => 'MemberOf',  Mode => 'Base'   },
68     ReferredToBy => { Type => 'RefersTo',  Mode => 'Base'   },
69     DependedOnBy => { Type => 'DependsOn', Mode => 'Base'   },
70     MemberOf     => { Type => 'MemberOf',  Mode => 'Target' },
71     RefersTo     => { Type => 'RefersTo',  Mode => 'Target' },
72     DependsOn    => { Type => 'DependsOn', Mode => 'Target' },
73 );
74
75 if ($changes) {
76     my ($get, $set, $key, $val, $n, $s);
77     my %data = %$changes;
78     my @comments;
79
80     foreach $key (keys %data) {
81         $val = $data{$key};
82         $key = lc $key;
83         $n = 1;
84
85         if (exists $fields{$key}) {
86             $key = $fields{$key};
87
88             my %old;
89             my $field = $lfields{$key}->{Mode};
90             while (my $link = $ticket->$key->Next) {
91                 $old{$link->$field} = 1;
92             }
93
94             my %new;
95             foreach my $nkey (@{vsplit($val)}) {
96                 if ($nkey =~ /^\d+$/) {
97                     my $uri = new RT::URI $session{CurrentUser};
98                     my $tick = new RT::Ticket $session{CurrentUser};
99                     $tick->Load($nkey);
100                     if ($tick->Id) {
101                         $nkey = $uri->FromObject($tick);
102                     }
103                     else {
104                         $n = 0;
105                         $s = "Ticket $nkey does not exist.";
106                         goto SET;
107                     }
108                 }
109                 $new{$nkey} = 1;
110             }
111
112             foreach my $u (keys %old) {
113                 if (exists $new{$u}) {
114                     delete $new{$u};
115                 }
116                 else {
117                     my $type = $lfields{$key}->{Type};
118                     my $mode = $lfields{$key}->{Mode};
119                     ($n, $s) = $ticket->DeleteLink(Type => $type, $mode => $u);
120                     goto SET;
121                 }
122             }
123             foreach my $u (keys %new) {
124                 my $type = $lfields{$key}->{Type};
125                 my $mode = $lfields{$key}->{Mode};
126                 ($n, $s) = $ticket->AddLink(Type => $type, $mode => $u);
127                 goto SET;
128             }
129         }
130         elsif ($key ne 'id' && $key ne 'type') {
131             $n = 0;
132             $s = "Unknown field: $key";
133         }
134
135     SET:
136         if ($n == 0) {
137             $e = 1;
138             push @comments, "# $key: $s";
139             unless (@$o) {
140                 @$o = ("id", @fields);
141                 %$k = %data;
142             }
143         }
144     }
145
146     push(@comments, "# Links for ticket $id updated.") unless @comments;
147     $c = join("\n", @comments) if @comments;
148 }
149 else {
150     my @data;
151
152     push @data, [ id => "ticket/".$ticket->Id."/links" ];
153     foreach my $key (@fields) {
154         my @val;
155
156         my $field = $lfields{$key}->{Mode};
157         while (my $link = $ticket->$key->Next) {
158             push @val, $link->$field;
159         }
160         push(@val, "") if (@val == 0 && $format eq 'l');
161         push @data, [ $key => [ @val ] ] if @val;
162     }
163
164     my %k = map {@$_} @data;
165     $o = [ map {$_->[0]} @data ];
166     $k = \%k;
167 }
168
169 return [ $c, $o, $k, $e ];
170 </%perl>