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