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