import of rt 3.0.9
[freeside.git] / rt / lib / RT / URI / fsck_com_rt.pm
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 package RT::URI::fsck_com_rt;
25
26 use RT::Ticket;
27
28 use RT::URI::base;
29
30 use strict;
31 use vars qw(@ISA);
32 @ISA = qw/RT::URI::base/;
33
34
35
36
37 =head2 LocalURIPrefix 
38
39 Returns the prefix for a local ticket URI
40
41 =begin testing
42
43 use_ok("RT::URI::fsck_com_rt");
44 my $uri = RT::URI::fsck_com_rt->new($RT::SystemUser);
45
46 ok(ref($uri));
47
48 use Data::Dumper;
49
50
51 ok (UNIVERSAL::isa($uri,RT::URI::fsck_com_rt), "It's an RT::URI::fsck_com_rt");
52
53 ok ($uri->isa('RT::URI::base'), "It's an RT::URI::base");
54 ok ($uri->isa('RT::Base'), "It's an RT::Base");
55
56 is ($uri->LocalURIPrefix , 'fsck.com-rt://example.com/ticket/');
57
58 =end testing
59
60
61
62 =cut
63
64 sub LocalURIPrefix {
65     my $self = shift;
66     my $prefix = $self->Scheme. "://$RT::Organization/ticket/";
67     return ($prefix);
68 }
69
70
71
72
73
74 =head2 URIForObject RT::Ticket
75
76 Returns the RT URI for a local RT::Ticket object
77
78 =begin testing
79
80 my $ticket = RT::Ticket->new($RT::SystemUser);
81 $ticket->Load(1);
82 my $uri = RT::URI::fsck_com_rt->new($ticket->CurrentUser);
83 is($uri->LocalURIPrefix . "1" , $uri->URIForObject($ticket));
84
85 =end testing
86
87 =cut
88
89 sub URIForObject {
90
91     my $self = shift;
92
93     my $obj = shift;
94     return ($self->LocalURIPrefix. $obj->Id);
95 }
96
97
98 =head2 ParseObject $TicketObj
99
100 When handed an RT::Ticekt object, figure out its URI
101
102
103 =cut
104
105
106
107 =head2 ParseURI URI
108
109 When handed an fsck.com-rt: URI, figures out things like whether its a local ticket
110 and what its ID is
111
112 =cut
113
114
115 sub ParseURI { 
116     my $self = shift;
117     my $uri = shift;
118
119         my $ticket;
120  
121         if ($uri =~ /^(\d+)$/) {
122                 $ticket = RT::Ticket->new($self->CurrentUser);
123                 $ticket->Load($uri);    
124                 $self->{'uri'} = $ticket->URI;
125         }
126         else {
127             $self->{'uri'} = $uri;
128         }
129  
130  
131  
132        #If it's a local URI, load the ticket object and return its URI
133     if ( $self->IsLocal) {
134    
135         my $local_uri_prefix = $self->LocalURIPrefix;
136         if ($self->{'uri'} =~ /^$local_uri_prefix(\d+)$/i) {
137                 my $id = $1;
138         
139     
140                 $ticket = RT::Ticket->new( $self->CurrentUser );
141             $ticket->Load($id);
142
143             #If we couldn't find a ticket, return undef.
144             unless ( defined $ticket->Id ) {
145                 return undef;
146             }
147             } else {
148             return undef;
149             }   
150     }
151  
152         $self->{'object'} = $ticket;
153     if ( UNIVERSAL::can( $ticket, 'Id' ) ) {
154         return ( $ticket->Id );
155     }
156     else {
157         return undef;
158     }
159 }
160
161 =head2 IsLocal 
162
163 Returns true if this URI is for a local ticket.
164 Returns undef otherwise.
165
166
167
168 =cut
169
170 sub IsLocal {
171         my $self = shift;
172         my $local_uri_prefix = $self->LocalURIPrefix;
173         if ($self->{'uri'} =~ /^$local_uri_prefix/i) {
174                 return 1;
175     }
176         else {
177                 return undef;
178         }
179 }
180
181
182
183 =head2 Object
184
185 Returns the object for this URI, if it's local. Otherwise returns undef.
186
187 =cut
188
189 sub Object {
190     my $self = shift;
191     return ($self->{'object'});
192
193 }
194
195 =head2 Scheme
196
197 Return the URI scheme for RT tickets
198
199 =cut
200
201
202 sub Scheme {
203     my $self = shift;
204         return "fsck.com-rt";
205 }
206
207 =head2 HREF
208
209 If this is a local ticket, return an HTTP url to it.
210 Otherwise, return its URI
211
212 =cut
213
214
215 sub HREF {
216     my $self = shift;
217     if ($self->IsLocal && $self->Object) {
218         return ( $RT::WebURL . "Ticket/Display.html?id=".$self->Object->Id);
219     }   
220     else {
221         return ($self->URI);
222     }
223 }
224
225 =head2 AsString
226
227 Returns either a localized string 'ticket #23' or the full URI if the object is not local
228
229 =cut
230
231 sub AsString {
232     my $self = shift;
233     if ($self->IsLocal && $self->Object) {
234             return $self->loc("ticket #[_1]", $self->Object->Id);
235     }
236     else {
237             return $self->URI;
238     }
239 }
240
241 eval "require RT::URI::fsck_com_rt_Vendor";
242 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI/fsck_com_rt_Vendor.pm});
243 eval "require RT::URI::fsck_com_rt_Local";
244 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI/fsck_com_rt_Local.pm});
245
246 1;