From 7dafa73f01a7a03392d716a042aa962c7a5b7986 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Tue, 31 Oct 2017 16:28:53 -0300
Subject: [PATCH] Bug 7317: Handle backend absense more gracefuly

5/ This patch is WIP, it makes Koha::Illrequest->load_backend raise an exception
if the passed backend is invalid. This way we will catch more errors introduced.

The patch also disables the 'New Ill request' when no backends are available. Gets
rid of a related warning.

TODO: Should add a noticeable warning on the UI, or a tooltip on the button.
TODO: add a couple tests for the exception

4/ This patch fixes the path for the checkboxes jquery plugin, and removes the include
for tablesorter, as this implementation uses Datatables. This is obviously code for older
Koha, ported to master.

3/ I know it doesn't look right, but am not sure how to display. This is the usual empty
datatable display we have everywhere in Koha.

2/ We could fix it with a proper IDE, I use Komodo and refactoring code is pretty straigh-forward.
Worth a separate bug report.

1/ That TODO is misleading. It refers to their own migration from a different design. I checked this
code and cannot find occurences of ->status with that annotation. I think this relates to backends code.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
---
 Koha/Exceptions/Ill.pm                                   | 16 ++++++++++++++++
 Koha/Illrequest.pm                                       | 12 ++++++++++--
 ill/ill-requests.pl                                      | 13 +++++++------
 koha-tmpl/intranet-tmpl/prog/en/includes/ill-toolbar.inc | 10 ++++++++--
 .../intranet-tmpl/prog/en/modules/ill/ill-requests.tt    |  3 +--
 5 files changed, 42 insertions(+), 12 deletions(-)
 create mode 100644 Koha/Exceptions/Ill.pm

diff --git a/Koha/Exceptions/Ill.pm b/Koha/Exceptions/Ill.pm
new file mode 100644
index 0000000..38643d4
--- /dev/null
+++ b/Koha/Exceptions/Ill.pm
@@ -0,0 +1,16 @@
+package Koha::Exceptions::Ill;
+
+use Modern::Perl;
+
+use Exception::Class (
+
+    'Koha::Exceptions::Ill' => {
+        description => 'Something went wrong!',
+    },
+    'Koha::Exceptions::Ill::InvalidBackendId' => {
+        isa => 'Koha::Exceptions::Ill',
+        description => "Invalid backend name required",
+    }
+);
+
+1;
diff --git a/Koha/Illrequest.pm b/Koha/Illrequest.pm
index 5adecb5..5435bf5 100644
--- a/Koha/Illrequest.pm
+++ b/Koha/Illrequest.pm
@@ -18,16 +18,18 @@ 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 Koha::Database;
 use Koha::Email;
+use Koha::Exceptions::Ill;
 use Koha::Illrequest;
 use Koha::Illrequestattributes;
 use Koha::Patron;
 use Mail::Sendmail;
 use Try::Tiny;
-use Modern::Perl;
 
 use base qw(Koha::Object);
 
@@ -138,14 +140,20 @@ sub load_backend {
 
     my @raw = qw/Koha Illbackends/; # Base Path
 
+    unless ( defined $backend_id && $backend_id ne '' ) {
+        Koha::Exceptions::Ill::InvalidBackendId->throw(
+            "An invalid backend ID was requested ('')");
+    }
+
     my $backend_name = $backend_id || $self->backend;
-    my $location = join "/", @raw, $backend_name, "Base.pm"; # File to load
+    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);
diff --git a/ill/ill-requests.pl b/ill/ill-requests.pl
index f6572e4..e316acd 100755
--- a/ill/ill-requests.pl
+++ b/ill/ill-requests.pl
@@ -224,14 +224,15 @@ if ( $op eq 'illview' ) {
 }
 
 # Get a list of backends
-my $ir = Koha::Illrequest->new;
+my $available_backends = Koha::Illrequest->new->available_backends;
 
 $template->param(
-    backends    => $ir->available_backends,
-    media       => [ "Book", "Article", "Journal" ],
-    query_type  => $op,
-    branches    => Koha::Libraries->search->unblessed,
-    here_link   => "/cgi-bin/koha/ill/ill-requests.pl"
+    backends_available => ( scalar $available_backends > 0 ),
+    backends           => $available_backends,
+    media              => [ "Book", "Article", "Journal" ],
+    query_type         => $op,
+    branches           => Koha::Libraries->search,
+    here_link          => "/cgi-bin/koha/ill/ill-requests.pl"
 );
 
 output_html_with_http_headers( $cgi, $cookie, $template->output );
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/ill-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/ill-toolbar.inc
index 3f541d9..717d12b 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/ill-toolbar.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/ill-toolbar.inc
@@ -1,7 +1,8 @@
 [% USE Koha %]
 [% IF Koha.Preference('ILLModule ') %]
     <div id="toolbar" class="btn-toolbar">
-        [% IF backends.size > 1 %]
+        [% IF available_backends %]
+          [% IF backends.size > 1 %]
             <div class="dropdown btn-group">
                 <button class="btn btn-sm btn-default dropdown-toggle" type="button" id="ill-backend-dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                     <i class="fa fa-plus"></i> New ILL request <span class="caret"></span>
@@ -12,10 +13,15 @@
                     [% END %]
                 </ul>
             </div>
-        [% ELSE %]
+          [% ELSE %]
             <a id="ill-new" class="btn btn-sm btn-default" href="/cgi-bin/koha/ill/ill-requests.pl?method=create&amp;backend=[% backends.0 %]">
                 <i class="fa fa-plus"></i> New ILL request
             </a>
+          [% END %]
+        [% ELSE %]
+            <a id="ill-new" class="btn btn-sm btn-default disabled" href="">
+                <i class="fa fa-plus"></i> New ILL request
+            </a>
         [% END %]
         <a id="ill-list" class="btn btn-sm btn-default btn-group" href="/cgi-bin/koha/ill/ill-requests.pl">
             <i class="fa fa-list"></i> List requests
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/ill/ill-requests.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/ill/ill-requests.tt
index 96943c1..6a32d5e 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/ill/ill-requests.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/ill/ill-requests.tt
@@ -4,8 +4,7 @@
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; ILL requests  &rsaquo;</title>
 [% INCLUDE 'doc-head-close.inc' %]
-<script type="text/javascript" src="[% themelang %]/lib/jquery/plugins/jquery.tablesorter.min.js"></script>
-<script type="text/javascript" src="[% themelang %]/lib/jquery/plugins/jquery.checkboxes.min.js"></script>
+<script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.checkboxes.min.js"></script>
 <link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/datatables.css">
 [% INCLUDE 'datatables.inc' %]
 <script type="text/javascript">
-- 
2.7.4