import of rt 3.0.9
[freeside.git] / rt / html / REST / 1.0 / ticket / link
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/ticket/link
25 %#
26 <%ARGS>
27 $id => undef
28 $del => 0
29 $rel
30 $to
31 </%ARGS>
32 <%INIT>
33 use RT::Interface::REST;
34
35 my $output;
36 my $status = "200 Ok";
37 my $ticket = new RT::Ticket $session{CurrentUser};
38 my $object = $r->path_info;
39
40 my @fields = qw(DependsOn DependedOnBy RefersTo ReferredToBy HasMember MemberOf);
41 my %fields = map { lc $_ => $_ } @fields;
42 my %lfields = (
43     HasMember    => { Type => 'MemberOf',  Mode => 'Base'   },
44     ReferredToBy => { Type => 'RefersTo',  Mode => 'Base'   },
45     DependedOnBy => { Type => 'DependsOn', Mode => 'Base'   },
46     MemberOf     => { Type => 'MemberOf',  Mode => 'Target' },
47     RefersTo     => { Type => 'RefersTo',  Mode => 'Target' },
48     DependsOn    => { Type => 'DependsOn', Mode => 'Target' },
49 );
50
51 # http://.../REST/1.0/ticket/link/1
52
53 $object =~ s#^/##;
54 if ($id && $object && $id != $object) {
55     $output = "Different ids in URL (`$object') and submitted form (`$id').\n";
56     $status = "400 Bad Request";
57     goto OUTPUT;
58 }
59 $id ||= $object;
60 unless ($id =~ /^\d+$/ && $to =~ /^\d+$/) {
61     my $bad = ($id !~ /^\d+$/) ? $id : $to;
62     $output = $r->path_info. "\n";
63     $output .= "Invalid ticket id: '$bad'.\n";
64     $status = "400 Bad Request";
65     goto OUTPUT;
66 }
67 unless (exists $fields{lc $rel}) {
68     $output = "Invalid relationship: '$rel'.\n";
69     $status = "400 Bad Request";
70     goto OUTPUT;
71 }
72 $rel = $fields{lc $rel};
73
74 $ticket->Load($id);
75 unless ($ticket->Id) {
76     $output = "Couldn't load ticket id: '$id'.\n";
77     $status = "404 Ticket not found";
78     goto OUTPUT;
79 }
80
81 my $type = $lfields{$rel}->{Type};
82 my $mode = $lfields{$rel}->{Mode};
83
84 my $n = 1;
85 my $op = $del ? "DeleteLink" : "AddLink";
86
87 ($n, $output) = $ticket->$op(Type => $type, $mode => $to);
88 if ($n == 0) {
89     $status = "500 Error";
90 }
91
92 OUTPUT:
93 </%INIT>
94 RT/<% $RT::VERSION %> <% $status %>
95
96 <% $output |n %>