Bug 21317: Format long lists under Koha Team tab as columns
[koha.git] / Koha / Illrequest.pm
index 5adecb5..99b97bd 100644 (file)
@@ -18,16 +18,19 @@ package Koha::Illrequest;
 # Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin
 # Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
+use Modern::Perl;
+
 use Clone 'clone';
-use File::Basename qw/basename/;
+use File::Basename qw( basename );
+use Encode qw( encode );
+use Mail::Sendmail;
+use Try::Tiny;
+
 use Koha::Database;
 use Koha::Email;
-use Koha::Illrequest;
+use Koha::Exceptions::Ill;
 use Koha::Illrequestattributes;
 use Koha::Patron;
-use Mail::Sendmail;
-use Try::Tiny;
-use Modern::Perl;
 
 use base qw(Koha::Object);
 
@@ -139,13 +142,20 @@ sub load_backend {
     my @raw = qw/Koha Illbackends/; # Base Path
 
     my $backend_name = $backend_id || $self->backend;
-    my $location = join "/", @raw, $backend_name, "Base.pm"; # File to load
+
+    unless ( defined $backend_name && $backend_name ne '' ) {
+        Koha::Exceptions::Ill::InvalidBackendId->throw(
+            "An invalid backend ID was requested ('')");
+    }
+
+    my $location = join "/", @raw, $backend_name, "Base.pm";    # File to load
     my $backend_class = join "::", @raw, $backend_name, "Base"; # Package name
     require $location;
     $self->{_my_backend} = $backend_class->new({ config => $self->_config });
     return $self;
 }
 
+
 =head3 _backend
 
     my $backend = $abstract->_backend($new_backend);
@@ -468,11 +478,8 @@ Return a list of available backends.
 
 sub available_backends {
     my ( $self ) = @_;
-    my $backend_dir = $self->_config->backend_dir;
-    my @backends = ();
-    @backends = glob "$backend_dir/*" if ( $backend_dir );
-    @backends = map { basename($_) } @backends;
-    return \@backends;
+    my $backends = $self->_config->available_backends;
+    return $backends;
 }
 
 =head3 available_actions
@@ -605,7 +612,8 @@ sub backend_create {
                 backend => $self->_backend->name
             }
         };
-    } elsif ( $params->{stage} eq 'copyrightclearance' ) {
+    } elsif (     defined $params->{stage}
+               && $params->{stage} eq 'copyrightclearance' ) {
         $params->{stage} = 'init';
     }
 
@@ -684,9 +692,13 @@ sub getLimits {
     my ( $self, $params ) = @_;
     my $limits = $self->_config->getLimitRules($params->{type});
 
-    return $limits->{$params->{value}}
-        || $limits->{default}
-        || { count => -1, method => 'active' };
+    if (     defined $params->{value}
+          && defined $limits->{$params->{value}} ) {
+            return $limits->{$params->{value}};
+    }
+    else {
+        return $limits->{default} || { count => -1, method => 'active' };
+    }
 }
 
 =head3 getPrefix
@@ -878,14 +890,20 @@ EOF
     } elsif ( 'draft' eq $params->{stage} ) {
         # Create the to header
         my $to = $params->{partners};
-        $to =~ s/^\x00//;       # Strip leading NULLs
-        $to =~ s/\x00/; /;      # Replace others with '; '
-        die "No target email addresses found. Either select at least one partner or check your ILL partner library records." if ( !$to );
+        if ( defined $to ) {
+            $to =~ s/^\x00//;       # Strip leading NULLs
+            $to =~ s/\x00/; /;      # Replace others with '; '
+        }
+        Koha::Exceptions::Ill::NoTargetEmail->throw(
+            "No target email addresses found. Either select at least one partner or check your ILL partner library records.")
+          if ( !$to );
         # Create the from, replyto and sender headers
         my $from = $branch->branchemail;
         my $replyto = $branch->branchreplyto || $from;
-        die "Your branch has no email address. Please set it."
-            if ( !$from );
+        Koha::Exceptions::Ill::NoLibraryEmail->throw(
+            "Your library has no usable email address. Please set it.")
+          if ( !$from );
+
         # Create the email
         my $message = Koha::Email->new;
         my %mail = $message->create_message_headers(
@@ -1000,8 +1018,8 @@ sub TO_JSON {
             $object->{capabilities} = $self->capabilities;
         }
         # Augment the request response with library details if appropriate
-        if ( $embed->{branch} ) {
-            $object->{branch} = Koha::Libraries->find(
+        if ( $embed->{library} ) {
+            $object->{library} = Koha::Libraries->find(
                 $self->branchcode
             )->TO_JSON;
         }