1 # $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Link.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $
2 # (c) 1996-1999 Jesse Vincent <jesse@fsck.com>
3 # This software is redistributable under the terms of the GNU GPL
7 RT::Link - an RT Link object
15 This module should never be called directly by client code. it's an internal module which
16 should only be accessed through exported APIs in Ticket other similar objects.
23 ok (require RT::TestHarness);
24 ok (require RT::Link);
38 $self->{'table'} = "Links";
39 return ($self->SUPER::_Init(@_));
46 =head2 Create PARAMHASH
48 Create a new link object. Takes 'Base', 'Target' and 'Type'.
49 Returns undef on failure or a Link Id on success.
55 my %args = ( Base => undef,
58 @_ # get the real argumentlist
61 my $BaseURI = $self->CanonicalizeURI($args{'Base'});
62 my $TargetURI = $self->CanonicalizeURI($args{'Target'});
64 unless (defined $BaseURI) {
65 $RT::Logger->warning ("$self couldn't resolve base:'".$args{'Base'}.
69 unless (defined $TargetURI) {
70 $RT::Logger->warning ("$self couldn't resolve target:'".$args{'Target'}.
75 my $LocalBase = $self->_IsLocal($BaseURI);
76 my $LocalTarget = $self->_IsLocal($TargetURI);
77 my $id = $self->SUPER::Create(Base => "$BaseURI",
78 Target => "$TargetURI",
79 LocalBase => $LocalBase,
80 LocalTarget => $LocalTarget,
81 Type => $args{'Type'});
91 Load an RT::Link object from the database. Takes one parameter or three.
92 One parameter is the id of an entry in the links table. Three parameters are a tuple of (base, linktype, target);
99 my $identifier = shift;
100 my $linktype = shift if (@_);
101 my $target = shift if (@_);
104 my $BaseURI = $self->CanonicalizeURI($identifier);
105 my $TargetURI = $self->CanonicalizeURI($target);
106 $self->LoadByCols( Base => $BaseURI,
109 ) || return (0, "Couldn't load link");
112 elsif ($identifier =~ /^\d+$/) {
113 $self->LoadById($identifier) ||
114 return (0, "Couldn't load link");
117 return (0, "That's not a numerical id");
131 return $self->_TicketObj('base',$self->Target);
143 return $self->_TicketObj('target',$self->Base);
152 my $tag="$name\_obj";
154 unless (exists $self->{$tag}) {
156 $self->{$tag}=RT::Ticket->new($self->CurrentUser);
158 #If we can get an actual ticket, load it up.
159 if ($self->_IsLocal($ref)) {
160 $self->{$tag}->Load($ref);
163 return $self->{$tag};
167 # {{{ sub _Accessible
172 LocalTarget => 'read',
176 Creator => 'read/auto',
177 Created => 'read/auto',
178 LastUpdatedBy => 'read/auto',
179 LastUpdated => 'read/auto'
181 return($self->SUPER::_Accessible(@_, %Cols));
188 # {{{ sub BaseIsLocal
192 Returns true if the base of this link is a local ticket
198 return $self->_IsLocal($self->Base);
203 # {{{ sub TargetIsLocal
207 Returns true if the target of this link is a local ticket
213 return $self->_IsLocal($self->Target);
222 When handed a URI returns the local ticket id if it\'s local. otherwise returns undef.
230 $RT::Logger->warning ("$self _IsLocal called without a URI\n");
233 # TODO: More thorough check
234 if ($URI =~ /^$RT::TicketBaseURI(\d+)$/) {
248 Returns an HTTP url to access the base of this link
254 return $self->AsHREF($self->Base);
258 # {{{ sub TargetAsHREF
262 return an HTTP url to access the target of this link
268 return $self->AsHREF($self->Target);
272 # {{{ sub AsHREF - Converts Link URIs to HTTP URLs
275 Takes a URI and returns an http: url to access that object.
281 if ($self->_IsLocal($URI)) {
282 my $url=$RT::WebURL . "Ticket/Display.html?id=$URI";
286 my ($protocol) = $URI =~ m|(.*?)://|;
287 unless (exists $RT::URI2HTTP{$protocol}) {
288 $RT::Logger->warning("Linking for protocol $protocol not defined in the config file!");
291 return $RT::URI2HTTP{$protocol}->($URI);
297 # {{{ sub GetContent - gets the content from a link
299 my ($self, $URI)= @_;
300 if ($self->_IsLocal($URI)) {
304 if ($URI =~ m|^(.*?)://|) {
305 if (exists $RT::ContentFromURI{$1}) {
306 return $RT::ContentFromURI{$1}->($URI);
308 warn "No sub exists for fetching the content from a $1 in $URI";
311 warn "No protocol specified in $URI";
317 # {{{ sub CanonicalizeURI
319 =head2 CanonicalizeURI
321 Takes a single argument: some form of ticket identifier.
322 Returns its canonicalized URI.
324 Bug: ticket aliases can't have :// in them. URIs must have :// in them.
328 sub CanonicalizeURI {
333 #If it's a local URI, load the ticket object and return its URI
334 if ($id =~ /^$RT::TicketBaseURI/) {
335 my $ticket = new RT::Ticket($self->CurrentUser);
337 #If we couldn't find a ticket, return undef.
338 return undef unless (defined $ticket->Id);
339 #$RT::Logger->debug("$self -> CanonicalizeURI was passed $id and returned ".$ticket->URI ." (uri)\n");
340 return ($ticket->URI);
342 #If it's a remote URI, we're going to punt for now
343 elsif ($id =~ '://' ) {
347 #If the base is an integer, load it as a ticket
348 elsif ( $id =~ /^\d+$/ ) {
350 #$RT::Logger->debug("$self -> CanonicalizeURI was passed $id. It's a ticket id.\n");
351 my $ticket = new RT::Ticket($self->CurrentUser);
353 #If we couldn't find a ticket, return undef.
354 return undef unless (defined $ticket->Id);
355 #$RT::Logger->debug("$self returned ".$ticket->URI ." (id #)\n");
356 return ($ticket->URI);
359 #It's not a URI. It's not a numerical ticket ID
362 #If we couldn't find a ticket, return undef.