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->Resolver and $base->Scheme ) {
85 $RT::Logger->warning( "$self couldn't resolve base:'"
86 . $args{'Base'} . " - "
90 #$RT::Logger->warning(scalar Dumper $base);
94 my $target = RT::URI->new( $self->CurrentUser );
95 $target->FromURI( $args{'Target'} );
97 unless ( $target->Resolver ) {
98 $RT::Logger->warning( "$self couldn't resolve target:'"
99 . $args{'Target'} . " - "
100 . "' into a URI\n" );
103 #$RT::Logger->warning(scalar Dumper $target);
113 if ( $base->IsLocal ) {
114 unless (UNIVERSAL::can($base->Object, 'Id')) {
115 return (undef, $self->loc("[_1] appears to be a local object, but can't be found in the database", $args{'Base'}));
118 $base_id = $base->Object->Id;
120 if ( $target->IsLocal ) {
121 unless (UNIVERSAL::can($target->Object, 'Id')) {
122 return (undef, $self->loc("[_1] appears to be a local object, but can't be found in the database", $args{'Target'}));
125 $target_id = $target->Object->Id;
128 # {{{ We don't want references to ourself
129 if ( $base->URI eq $target->URI ) {
130 return ( 0, $self->loc("Can't link a ticket to itself") );
135 my ( $id, $msg ) = $self->SUPER::Create( Base => $base->URI,
136 Target => $target->URI,
137 LocalBase => $base_id,
138 LocalTarget => $target_id,
139 Type => $args{'Type'} );
140 return ( $id, $msg );
144 # {{{ sub LoadByParams
148 Load an RT::Link object from the database. Takes three parameters
154 Base and Target are expected to be integers which refer to Tickets or URIs
155 Type is the link type
161 my %args = ( Base => undef,
166 my $base = RT::URI->new($self->CurrentUser);
167 $base->FromURI( $args{'Base'} );
169 my $target = RT::URI->new($self->CurrentUser);
170 $target->FromURI( $args{'Target'} );
172 unless ($base->Resolver && $target->Resolver) {
173 return ( 0, $self->loc("Couldn't load link") );
177 my ( $id, $msg ) = $self->LoadByCols( Base => $base->URI,
178 Type => $args{'Type'},
179 Target => $target->URI );
182 return ( 0, $self->loc("Couldn't load link") );
191 Load an RT::Link object from the database. Takes one parameter, the id of an entry in the links table.
198 my $identifier = shift;
203 if ( $identifier !~ /^\d+$/ ) {
204 return ( 0, $self->loc("That's not a numerical id") );
207 my ( $id, $msg ) = $self->LoadById($identifier);
208 unless ( $self->Id ) {
209 return ( 0, $self->loc("Couldn't load link") );
211 return ( $id, $msg );
222 returns an RT::URI object for the "Target" of this link.
228 my $URI = RT::URI->new($self->CurrentUser);
229 $URI->FromURI($self->Target);
242 return $self->TargetURI->Object;
250 returns an RT::URI object for the "Base" of this link.
256 my $URI = RT::URI->new($self->CurrentUser);
257 $URI->FromURI($self->Base);
270 return $self->BaseURI->Object;
278 # {{{ sub BaseIsLocal
282 Returns true if the base of this link is a local ticket
288 $RT::Logger->crit("Link::BaseIsLocal is deprecated in favor of Link->BaseURI->IsLocal");
289 return $self->BaseURI->IsLocal;
294 # {{{ sub TargetIsLocal
298 Returns true if the target of this link is a local ticket
304 $RT::Logger->crit("Link::BaseIsLocal is deprecated in favor of Link->BaseURI->IsLocal");
305 return $self->TargetURI->IsLocal;
315 Returns an HTTP url to access the base of this link
321 $RT::Logger->crit("Link::BaseAsHREF deprecated in favor of ->BaseURI->AsHREF");
322 return $self->BaseURI->HREF;
326 # {{{ sub TargetAsHREF
330 return an HTTP url to access the target of this link
336 $RT::Logger->crit("Link::TargetAsHREF deprecated in favor of ->TargetURI->AsHREF");
337 return $self->TargetURI->HREF;
341 # {{{ sub AsHREF - Converts Link URIs to HTTP URLs
345 Takes a URI and returns an http: url to access that object.
353 $RT::Logger->crit("AsHREF is gone. look at URI::HREF to figure out what to do with \$URI");