import of rt 3.0.9
[freeside.git] / rt / html / REST / 1.0 / Forms / ticket / links
1 %# BEGIN LICENSE BLOCK
2 %# 
3 %# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4 %# 
5 %# (Except where explictly superceded by other copyright notices)
6 %# 
7 %# This work is made available to you under the terms of Version 2 of
8 %# the GNU General Public License. A copy of that license should have
9 %# been provided with this software, but in any event can be snarfed
10 %# from www.gnu.org.
11 %# 
12 %# This work is distributed in the hope that it will be useful, but
13 %# WITHOUT ANY WARRANTY; without even the implied warranty of
14 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 %# General Public License for more details.
16 %# 
17 %# Unless otherwise specified, all modifications, corrections or
18 %# extensions to this work which alter its source code become the
19 %# property of Best Practical Solutions, LLC when submitted for
20 %# inclusion in the work.
21 %# 
22 %# 
23 %# END LICENSE BLOCK
24 %# REST/1.0/Forms/ticket/links
25 %#
26 <%ARGS>
27 $id
28 $format => 's'
29 $changes => undef
30 </%ARGS>
31 <%perl>
32 my @data;
33 my $ticket = new RT::Ticket $session{CurrentUser};
34
35 $ticket->Load($id);
36 if (!$ticket->Id) {
37     return [ "# Ticket $id does not exist.", [], {}, 1 ];
38 }
39
40 my ($c, $o, $k, $e) = ("", [], {}, 0);
41 my @fields = qw(DependsOn DependedOnBy RefersTo ReferredToBy Members MemberOf);
42 my %fields = map { lc $_ => $_ } @fields;
43
44 my %lfields = (
45     Members      => { Type => 'MemberOf',  Mode => 'Base'   },
46     ReferredToBy => { Type => 'RefersTo',  Mode => 'Base'   },
47     DependedOnBy => { Type => 'DependsOn', Mode => 'Base'   },
48     MemberOf     => { Type => 'MemberOf',  Mode => 'Target' },
49     RefersTo     => { Type => 'RefersTo',  Mode => 'Target' },
50     DependsOn    => { Type => 'DependsOn', Mode => 'Target' },
51 );
52
53 if ($changes) {
54     my ($get, $set, $key, $val, $n, $s);
55     my %data = %$changes;
56     my @comments;
57
58     foreach $key (keys %data) {
59         $val = $data{$key};
60         $key = lc $key;
61         $n = 1;
62
63         if (exists $fields{$key}) {
64             $key = $fields{$key};
65
66             my %old;
67             my $field = $lfields{$key}->{Mode};
68             while (my $link = $ticket->$key->Next) {
69                 $old{$link->$field} = 1;
70             }
71
72             my %new;
73             foreach my $nkey (@{vsplit($val)}) {
74                 if ($nkey =~ /^\d+$/) {
75                     my $uri = new RT::URI $session{CurrentUser};
76                     my $tick = new RT::Ticket $session{CurrentUser};
77                     $tick->Load($nkey);
78                     if ($tick->Id) {
79                         $nkey = $uri->FromObject($tick);
80                     }
81                     else {
82                         $n = 0;
83                         $s = "Ticket $nkey does not exist.";
84                         goto SET;
85                     }
86                 }
87                 $new{$nkey} = 1;
88             }
89
90             foreach my $u (keys %old) {
91                 if (exists $new{$u}) {
92                     delete $new{$u};
93                 }
94                 else {
95                     my $type = $lfields{$key}->{Type};
96                     my $mode = $lfields{$key}->{Mode};
97                     ($n, $s) = $ticket->DeleteLink(Type => $type, $mode => $u);
98                     goto SET;
99                 }
100             }
101             foreach my $u (keys %new) {
102                 my $type = $lfields{$key}->{Type};
103                 my $mode = $lfields{$key}->{Mode};
104                 ($n, $s) = $ticket->AddLink(Type => $type, $mode => $u);
105                 goto SET;
106             }
107         }
108         elsif ($key ne 'id' && $key ne 'type') {
109             $n = 0;
110             $s = "Unknown field: $key";
111         }
112
113     SET:
114         if ($n == 0) {
115             $e = 1;
116             push @comments, "# $key: $s";
117             unless (@$o) {
118                 @$o = ("id", @fields);
119                 %$k = %data;
120             }
121         }
122     }
123
124     push(@comments, "# Links for ticket $id updated.") unless @comments;
125     $c = join("\n", @comments) if @comments;
126 }
127 else {
128     my @data;
129
130     push @data, [ id => "ticket/".$ticket->Id."/links" ];
131     foreach my $key (@fields) {
132         my @val;
133
134         my $field = $lfields{$key}->{Mode};
135         while (my $link = $ticket->$key->Next) {
136             push @val, $link->$field;
137         }
138         push(@val, "") if (@val == 0 && $format eq 'l');
139         push @data, [ $key => [ @val ] ] if @val;
140     }
141
142     my %k = map {@$_} @data;
143     $o = [ map {$_->[0]} @data ];
144     $k = \%k;
145 }
146
147 return [ $c, $o, $k, $e ];
148 </%perl>