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