3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5 # (Except where explictly superceded by other copyright notices)
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
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.
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.
26 RT::Link - an RT Link object
34 This module should never be called directly by client code. it's an internal module which
35 should only be accessed through exported APIs in Ticket other similar objects.
44 my $link = RT::Link->new($RT::SystemUser);
48 ok (UNIVERSAL::isa($link, 'RT::Link'));
49 ok (UNIVERSAL::isa($link, 'RT::Base'));
50 ok (UNIVERSAL::isa($link, 'RT::Record'));
51 ok (UNIVERSAL::isa($link, 'DBIx::SearchBuilder::Record'));
58 no warnings qw(redefine);
67 =head2 Create PARAMHASH
69 Create a new link object. Takes 'Base', 'Target' and 'Type'.
70 Returns undef on failure or a Link Id on success.
76 my %args = ( Base => undef,
81 my $base = RT::URI->new( $self->CurrentUser );
82 $base->FromURI( $args{'Base'} );
84 unless ( $base->Scheme ) {
85 $RT::Logger->warning( "$self couldn't resolve base:'"
86 . $args{'Base'} . " - "
91 #$RT::Logger->warning(scalar Dumper $base);
95 my $target = RT::URI->new( $self->CurrentUser );
96 $target->FromURI( $args{'Target'} );
98 unless ( $target->Resolver ) {
99 $RT::Logger->warning( "$self couldn't resolve target:'"
100 . $args{'Target'} . " - "
101 . "' into a URI\n" );
104 #$RT::Logger->warning(scalar Dumper $target);
114 if ( $base->IsLocal ) {
115 unless (UNIVERSAL::can($base->Object, 'Id')) {
116 return (undef, $self->loc("[_1] appears to be a local object, but can't be found in the database", $args{'Base'}));
119 $base_id = $base->Object->Id;
121 if ( $target->IsLocal ) {
122 unless (UNIVERSAL::can($target->Object, 'Id')) {
123 return (undef, $self->loc("[_1] appears to be a local object, but can't be found in the database", $args{'Target'}));
126 $target_id = $target->Object->Id;
129 # {{{ We don't want references to ourself
130 if ( $base->URI eq $target->URI ) {
131 return ( 0, $self->loc("Can't link a ticket to itself") );
136 my ( $id, $msg ) = $self->SUPER::Create( Base => $base->URI,
137 Target => $target->URI,
138 LocalBase => $base_id,
139 LocalTarget => $target_id,
140 Type => $args{'Type'} );
141 return ( $id, $msg );
145 # {{{ sub LoadByParams
149 Load an RT::Link object from the database. Takes three parameters
155 Base and Target are expected to be integers which refer to Tickets or URIs
156 Type is the link type
162 my %args = ( Base => undef,
167 my $base = RT::URI->new($self->CurrentUser);
168 $base->FromURI( $args{'Base'} );
170 my $target = RT::URI->new($self->CurrentUser);
171 $target->FromURI( $args{'Target'} );
173 unless ($base->Resolver && $target->Resolver) {
174 return ( 0, $self->loc("Couldn't load link") );
178 my ( $id, $msg ) = $self->LoadByCols( Base => $base->URI,
179 Type => $args{'Type'},
180 Target => $target->URI );
183 return ( 0, $self->loc("Couldn't load link") );
192 Load an RT::Link object from the database. Takes one parameter, the id of an entry in the links table.
199 my $identifier = shift;
204 if ( $identifier !~ /^\d+$/ ) {
205 return ( 0, $self->loc("That's not a numerical id") );
208 my ( $id, $msg ) = $self->LoadById($identifier);
209 unless ( $self->Id ) {
210 return ( 0, $self->loc("Couldn't load link") );
212 return ( $id, $msg );
223 returns an RT::URI object for the "Target" of this link.
229 my $URI = RT::URI->new($self->CurrentUser);
230 $URI->FromURI($self->Target);
243 return $self->TargetURI->Object;
251 returns an RT::URI object for the "Base" of this link.
257 my $URI = RT::URI->new($self->CurrentUser);
258 $URI->FromURI($self->Base);
271 return $self->BaseURI->Object;
279 # {{{ sub BaseIsLocal
283 Returns true if the base of this link is a local ticket
289 $RT::Logger->crit("Link::BaseIsLocal is deprecated in favor of Link->BaseURI->IsLocal");
290 return $self->BaseURI->IsLocal;
295 # {{{ sub TargetIsLocal
299 Returns true if the target of this link is a local ticket
305 $RT::Logger->crit("Link::BaseIsLocal is deprecated in favor of Link->BaseURI->IsLocal");
306 return $self->TargetURI->IsLocal;
316 Returns an HTTP url to access the base of this link
322 $RT::Logger->crit("Link::BaseAsHREF deprecated in favor of ->BaseURI->AsHREF");
323 return $self->BaseURI->HREF;
327 # {{{ sub TargetAsHREF
331 return an HTTP url to access the target of this link
337 $RT::Logger->crit("Link::TargetAsHREF deprecated in favor of ->TargetURI->AsHREF");
338 return $self->TargetURI->HREF;
342 # {{{ sub AsHREF - Converts Link URIs to HTTP URLs
346 Takes a URI and returns an http: url to access that object.
354 $RT::Logger->crit("AsHREF is gone. look at URI::HREF to figure out what to do with \$URI");