import rt 3.6.4
[freeside.git] / rt / lib / RT / URI / fsck_com_rt.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/copyleft/gpl.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 package RT::URI::fsck_com_rt;
49
50 use RT::Ticket;
51
52 use RT::URI::base;
53
54 use strict;
55 use vars qw(@ISA);
56 @ISA = qw/RT::URI::base/;
57
58
59
60
61 =head2 LocalURIPrefix  
62
63 Returns the prefix for a local URI. 
64
65 =begin testing
66
67 use_ok("RT::URI::fsck_com_rt");
68 my $uri = RT::URI::fsck_com_rt->new($RT::SystemUser);
69
70 ok(ref($uri));
71
72 use Data::Dumper;
73
74
75 ok (UNIVERSAL::isa($uri,RT::URI::fsck_com_rt), "It's an RT::URI::fsck_com_rt");
76
77 ok ($uri->isa('RT::URI::base'), "It's an RT::URI::base");
78 ok ($uri->isa('RT::Base'), "It's an RT::Base");
79
80 is ($uri->LocalURIPrefix , 'fsck.com-rt://'.$RT::Organization);
81
82 =end testing
83
84
85
86 =cut
87
88 sub LocalURIPrefix {
89     my $self = shift;
90     
91     my $prefix = $self->Scheme. "://$RT::Organization";
92
93     return ($prefix);
94 }
95
96 =head2 ObjectType
97
98 =cut
99
100 sub ObjectType {
101     my $self = shift;
102     my $object = shift || $self->Object;
103
104     my $type = 'ticket';
105     if (ref($object) && (ref($object) ne 'RT::Ticket')) {
106             $type = ref($object);
107     }
108
109     return ($type);
110 }
111
112
113
114
115 =head2 URIForObject RT::Record
116
117 Returns the RT URI for a local RT::Record object
118
119 =begin testing
120
121 my $ticket = RT::Ticket->new($RT::SystemUser);
122 $ticket->Load(1);
123 my $uri = RT::URI::fsck_com_rt->new($ticket->CurrentUser);
124 is($uri->LocalURIPrefix. "/ticket/1" , $uri->URIForObject($ticket));
125
126 =end testing
127
128 =cut
129
130 sub URIForObject {
131     my $self = shift;
132     my $obj = shift;
133     return ($self->LocalURIPrefix ."/". $self->ObjectType($obj) ."/". $obj->Id);
134 }
135
136
137 =head2 ParseURI URI
138
139 When handed an fsck.com-rt: URI, figures out things like whether its a local record and what its ID is
140
141 =cut
142
143
144 sub ParseURI {
145     my $self = shift;
146     my $uri  = shift;
147
148     if ( $uri =~ /^\d+$/ ) {
149         my $ticket = RT::Ticket->new( $self->CurrentUser );
150         $ticket->Load( $uri );
151         $self->{'uri'} = $ticket->URI;
152         $self->{'object'} = $ticket;
153         return ($ticket->id);
154     }
155     else {
156         $self->{'uri'} = $uri;
157     }
158
159     #If it's a local URI, load the ticket object and return its URI
160     if ( $self->IsLocal ) {
161         my $local_uri_prefix = $self->LocalURIPrefix;
162         if ( $self->{'uri'} =~ /^\Q$local_uri_prefix\E\/(.*?)\/(\d+)$/i ) {
163             my $type = $1;
164             my $id   = $2;
165
166             if ( $type eq 'ticket' ) { $type = 'RT::Ticket' }
167
168             # We can instantiate any RT::Record subtype. but not anything else
169
170             if ( UNIVERSAL::isa( $type, 'RT::Record' ) ) {
171                 my $record = $type->new( $self->CurrentUser );
172                 $record->Load($id);
173
174                 if ( $record->Id ) {
175                     $self->{'object'} = $record;
176                     return ( $record->Id );
177                 }
178             }
179
180         }
181     }
182     return undef;
183 }
184
185 =head2 IsLocal 
186
187 Returns true if this URI is for a local ticket.
188 Returns undef otherwise.
189
190
191
192 =cut
193
194 sub IsLocal {
195         my $self = shift;
196     my $local_uri_prefix = $self->LocalURIPrefix;
197     if ( $self->{'uri'} =~ /^\Q$local_uri_prefix/i ) {
198         return 1;
199     }
200         else {
201                 return undef;
202         }
203 }
204
205
206
207 =head2 Object
208
209 Returns the object for this URI, if it's local. Otherwise returns undef.
210
211 =cut
212
213 sub Object {
214     my $self = shift;
215     return ($self->{'object'});
216
217 }
218
219 =head2 Scheme
220
221 Return the URI scheme for RT records
222
223 =cut
224
225
226 sub Scheme {
227     my $self = shift;
228         return "fsck.com-rt";
229 }
230
231 =head2 HREF
232
233 If this is a local ticket, return an HTTP url to it.
234 Otherwise, return its URI
235
236 =cut
237
238
239 sub HREF {
240     my $self = shift;
241     if ($self->IsLocal && $self->Object && ($self->ObjectType eq 'ticket')) {
242         return ( $RT::WebURL . "Ticket/Display.html?id=".$self->Object->Id);
243     }   
244     else {
245         return ($self->URI);
246     }
247 }
248
249 =head2 AsString
250
251 Returns either a localized string 'ticket #23' or the full URI if the object is not local
252
253 =cut
254
255 sub AsString {
256     my $self = shift;
257     if ($self->IsLocal && $self->Object) {
258             return $self->loc("[_1] #[_2]", $self->ObjectType, $self->Object->Id);
259     }
260     else {
261             return $self->URI;
262     }
263 }
264
265 eval "require RT::URI::fsck_com_rt_Vendor";
266 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI/fsck_com_rt_Vendor.pm});
267 eval "require RT::URI::fsck_com_rt_Local";
268 die $@ if ($@ && $@ !~ qr{^Can't locate RT/URI/fsck_com_rt_Local.pm});
269
270 1;