Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / lib / RT / URI / fsck_com_article.pm
index 503434a..7132770 100644 (file)
@@ -2,7 +2,7 @@
 #
 # COPYRIGHT:
 #
-# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
+# This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
 #                                          <sales@bestpractical.com>
 #
 # (Except where explicitly superseded by other copyright notices)
@@ -52,8 +52,8 @@ use strict;
 use warnings;
 no warnings 'redefine';
 
-use RT::Article;
 use base qw/RT::URI::base/;
+use RT::Article;
 
 =head2 LocalURIPrefix 
 
@@ -63,8 +63,7 @@ Returns the prefix for a local article URI
 
 sub LocalURIPrefix {
     my $self = shift;
-    my $prefix = $self->Scheme. "://". RT->Config->Get('Organization')
-        . "/article/";
+    my $prefix = $self->Scheme. "://". RT->Config->Get('Organization');
     return ($prefix);
 }
 
@@ -79,7 +78,7 @@ sub URIForObject {
     my $self = shift;
 
     my $obj = shift;
-    return ($self->LocalURIPrefix. $obj->Id);
+    return ($self->LocalURIPrefix . "/article/" . $obj->Id);
 }
 
 
@@ -100,39 +99,45 @@ sub ParseURI {
     my $self = shift;
     my $uri = shift;
 
-       my $article;
-       if ($uri =~ /^(\d+)$/) {
-               $article = RT::Article->new($self->CurrentUser);
-               $article->Load($uri);   
-               $self->{'uri'} = $article->URI;
-       }
-       else {
-           $self->{'uri'} = $uri;
-       }
+    my $article;
+
+    if ($uri =~ /^(\d+)$/) {
+        $article = RT::Article->new($self->CurrentUser);
+        $article->Load($uri);
+        $self->{'uri'} = $article->URI;
+    }
+    else {
+        $self->{'uri'} = $uri;
+    }
+
        #If it's a local URI, load the article object and return its URI
     if ( $self->IsLocal) {
-   
         my $local_uri_prefix = $self->LocalURIPrefix;
-       if ($self->{'uri'} =~ /^$local_uri_prefix(\d+)$/) {
-               my $id = $1;
-       
-    
-               $article = RT::Article->new( $self->CurrentUser );
-           $article->Load($id);
-
-           #If we couldn't find a article, return undef.
-           unless ( defined $article->Id ) {
-               return undef;
-           }
-           } else {
-           return undef;
-           }   
+        if ($self->{'uri'} =~ /^$local_uri_prefix\/article\/(\d+)$/) {
+            my $id = $1;
+            $article = RT::Article->new( $self->CurrentUser );
+            my ($ret, $msg) = $article->Load($id);
+
+            #If we couldn't find a article, return undef.
+            unless ( $article and $article->Id ) {
+                # We got an id, but couldn't load it, so warn that it may
+                # have been deleted.
+                RT::Logger->warning("Unable to load article for id $id. It may"
+                    . " have been deleted: $msg");
+                return undef;
+            }
+        } else {
+            return undef;
+        }
+    }
+
+    #If we couldn't find a article, return undef.
+    unless ( $article and $article->Id ) {
+        return undef;
     }
-       $self->{'object'} = $article;
-       return ($article->Id);
+
+    $self->{'object'} = $article;
+    return ($article->Id);
 }
 
 =head2 IsLocal 
@@ -143,14 +148,14 @@ Returns undef otherwise.
 =cut
 
 sub IsLocal {
-       my $self = shift;
-        my $local_uri_prefix = $self->LocalURIPrefix;
-       if ($self->{'uri'} =~ /^$local_uri_prefix/) {
-               return 1;
+    my $self = shift;
+    my $local_uri_prefix = $self->LocalURIPrefix;
+    if ($self->{'uri'} =~ /^$local_uri_prefix/) {
+        return 1;
+    }
+    else {
+        return undef;
     }
-       else {
-               return undef;
-       }
 }
 
 
@@ -175,7 +180,7 @@ Return the URI scheme for RT articles
 
 sub Scheme {
     my $self = shift;
-       return "fsck.com-article";
+    return "fsck.com-article";
 }
 
 =head2 HREF
@@ -188,7 +193,7 @@ Otherwise, return its URI
 sub HREF {
     my $self = shift;
     if ($self->IsLocal && $self->Object) {
-        return ( RT->Config->Get('WebURL') . "/Articles/Article/Display.html?id=".$self->Object->Id);
+        return ( RT->Config->Get('WebURL') . "Articles/Article/Display.html?id=".$self->Object->Id);
     }   
     else {
         return ($self->URI);
@@ -203,9 +208,12 @@ Return "Article 23"
 
 sub AsString {
     my $self = shift;
-    if ($self->IsLocal && $self->Object) {
-    return $self->loc('Article [_1]', $self->Object->id);
-
+    if ($self->IsLocal && ( my $object = $self->Object )) {
+        if ( $object->Name ) {
+            return $self->loc('Article #[_1]: [_2]', $object->id, $object->Name);
+        } else {
+            return $self->loc('Article #[_1]', $object->id);
+        }
     } else {
         return $self->SUPER::AsString(@_);
     }