From 89c5077b4156d5a60999f42321e7005340c3ff26 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 31 Oct 2017 16:28:53 -0300 Subject: [PATCH] Bug 7317: Handle backend absense more gracefuly 5/ This patch 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 warnings. Both OPAC and Intranet now display a warning message when no backends are available. Tests are added for the load_backend changes. 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. TODO: There's something wrong on the styling. My idea is to get rid of the custom column visualization tool, and have it display as regular DataTables. We can then introduce the use of colvis on a separate bug report. Note: POD coverage for the exceptions file is wrongly tested. It is a false positive. Signed-off-by: Tomas Cohen Arazi --- Koha/Exceptions/Ill.pm | 47 +++ Koha/Illrequest.pm | 18 +- Koha/Illrequest/Config.pm | 18 ++ Koha/Illrequestattribute.pm | 4 +- about.pl | 14 + ill/ill-requests.pl | 329 +++++++++++---------- .../intranet-tmpl/prog/en/includes/ill-toolbar.inc | 10 +- koha-tmpl/intranet-tmpl/prog/en/modules/about.tt | 9 +- .../prog/en/modules/ill/ill-requests.tt | 14 +- .../bootstrap/en/modules/opac-illrequests.tt | 4 + opac/opac-illrequests.pl | 8 +- t/db_dependent/Illrequests.t | 53 ++-- 12 files changed, 325 insertions(+), 203 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 0000000000..9e1376109b --- /dev/null +++ b/Koha/Exceptions/Ill.pm @@ -0,0 +1,47 @@ +package Koha::Exceptions::Ill; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +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", + } +); + +=head1 NAME + +Koha::Exceptions::Ill - Base class for ILL exceptions + +=head1 Exceptions + +=head2 Koha::Exceptions::Ill + +Generic Ill exception + +=head2 Koha::Exceptions::Ill::InvalidBackend + +Exception to be used when the required ILL backend is invalid. + +=cut + +1; diff --git a/Koha/Illrequest.pm b/Koha/Illrequest.pm index 5adecb53af..488fa46cd6 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); @@ -139,13 +141,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,10 +477,7 @@ 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; + my @backends = $self->_config->available_backends; return \@backends; } diff --git a/Koha/Illrequest/Config.pm b/Koha/Illrequest/Config.pm index 3bc78d15e9..5c78e80976 100644 --- a/Koha/Illrequest/Config.pm +++ b/Koha/Illrequest/Config.pm @@ -18,6 +18,9 @@ package Koha::Illrequest::Config; # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. use Modern::Perl; + +use File::Basename; + use C4::Context; =head1 NAME @@ -100,6 +103,21 @@ sub backend_dir { return $self->{configuration}->{backend_directory}; } +=head3 available_backends + +Return a list of available backends. + +=cut + +sub available_backends { + my ( $self ) = @_; + my $backend_dir = $self->backend_dir; + my @backends = (); + @backends = glob "$backend_dir/*" if ( $backend_dir ); + @backends = map { basename($_) } @backends; + return \@backends; +} + =head3 partner_code $partner_code = $config->partner_code($new_code); diff --git a/Koha/Illrequestattribute.pm b/Koha/Illrequestattribute.pm index 16bc0867a5..cbdee5a3ca 100644 --- a/Koha/Illrequestattribute.pm +++ b/Koha/Illrequestattribute.pm @@ -30,11 +30,11 @@ Koha::Illrequestattribute - Koha Illrequestattribute Object class =head1 API -=head2 Class Methods +=head2 Internal methods =cut -=head3 type +=head3 _type =cut diff --git a/about.pl b/about.pl index 61e81c641f..a7daa3780c 100755 --- a/about.pl +++ b/about.pl @@ -38,6 +38,7 @@ use Koha::Acquisition::Currencies; use Koha::Patrons; use Koha::Caches; use Koha::Config::SysPrefs; +use Koha::Illrequest::Config; use C4::Members::Statistics; #use Smart::Comments '####'; @@ -260,6 +261,19 @@ if ( !defined C4::Context->config('use_zebra_facets') ) { } } +if ( C4::Context->preference('ILLModule') ) { + my $warnILLConfiguration = 0; + my $available_ill_backends = + ( scalar @{ Koha::Illrequest::Config->new->available_backends } > 0 ); + + if ( !$available_ill_backends ) { + $template->param( no_ill_backends => 1 ); + $warnILLConfiguration = 1; + } + + $template->param( warnILLConfiguration => $warnILLConfiguration ); +} + # Sco Patron should not contain any other perms than circulate => self_checkout if ( C4::Context->preference('WebBasedSelfCheck') and C4::Context->preference('AutoSelfCheckAllowed') diff --git a/ill/ill-requests.pl b/ill/ill-requests.pl index f6572e494d..140eff90c7 100755 --- a/ill/ill-requests.pl +++ b/ill/ill-requests.pl @@ -50,188 +50,190 @@ my ( $template, $patronnumber, $cookie ) = get_template_and_user( { flagsrequired => { ill => '*' }, } ); -if ( $op eq 'illview' ) { - # View the details of an ILL - my $request = Koha::Illrequests->find($params->{illrequest_id}); - - $template->param( - request => $request - ); - -} elsif ( $op eq 'create' ) { - # We're in the process of creating a request - my $request = Koha::Illrequest->new - ->load_backend($params->{backend}); - my $backend_result = $request->backend_create($params); - $template->param( - whole => $backend_result, - request => $request - ); - handle_commit_maybe($backend_result, $request); - -} elsif ( $op eq 'confirm' ) { - # Backend 'confirm' method - # confirm requires a specific request, so first, find it. - my $request = Koha::Illrequests->find($params->{illrequest_id}); - my $backend_result = $request->backend_confirm($params); - $template->param( - whole => $backend_result, - request => $request, - ); - - # handle special commit rules & update type - handle_commit_maybe($backend_result, $request); - -} elsif ( $op eq 'cancel' ) { - # Backend 'cancel' method - # cancel requires a specific request, so first, find it. - my $request = Koha::Illrequests->find($params->{illrequest_id}); - my $backend_result = $request->backend_cancel($params); - $template->param( - whole => $backend_result, - request => $request, - ); - - # handle special commit rules & update type - handle_commit_maybe($backend_result, $request); - -} elsif ( $op eq 'edit_action' ) { - # Handle edits to the Illrequest object. - # (not the Illrequestattributes) - # We simulate the API for backend requests for uniformity. - # So, init: - my $request = Koha::Illrequests->find($params->{illrequest_id}); - if ( !$params->{stage} ) { - my $backend_result = { - error => 0, - status => '', - message => '', - method => 'edit_action', - stage => 'init', - next => '', - value => {} - }; +# Are we able to actually work? +my $backends = Koha::Illrequest::Config->new->available_backends; +my $backends_available = ( scalar @{$backends} > 0 ); +$template->param( backends_available => $backends_available ); + +if ( $backends_available ) { + if ( $op eq 'illview' ) { + # View the details of an ILL + my $request = Koha::Illrequests->find($params->{illrequest_id}); + + $template->param( + request => $request + ); + + } elsif ( $op eq 'create' ) { + # We're in the process of creating a request + my $request = Koha::Illrequest->new->load_backend( $params->{backend} ); + my $backend_result = $request->backend_create($params); $template->param( whole => $backend_result, request => $request ); - } else { - # Commit: - # Save the changes - $request->borrowernumber($params->{borrowernumber}); - $request->biblio_id($params->{biblio_id}); - $request->branchcode($params->{branchcode}); - $request->notesopac($params->{notesopac}); - $request->notesstaff($params->{notesstaff}); - $request->store; - my $backend_result = { - error => 0, - status => '', - message => '', - method => 'edit_action', - stage => 'commit', - next => 'illlist', - value => {} - }; handle_commit_maybe($backend_result, $request); - } -} elsif ( $op eq 'moderate_action' ) { - # Moderate action is required for an ILL submodule / syspref. - # Currently still needs to be implemented. - redirect_to_list(); + } elsif ( $op eq 'confirm' ) { + # Backend 'confirm' method + # confirm requires a specific request, so first, find it. + my $request = Koha::Illrequests->find($params->{illrequest_id}); + my $backend_result = $request->backend_confirm($params); + $template->param( + whole => $backend_result, + request => $request, + ); -} elsif ( $op eq 'delete_confirm') { - my $request = Koha::Illrequests->find($params->{illrequest_id}); + # handle special commit rules & update type + handle_commit_maybe($backend_result, $request); + + } elsif ( $op eq 'cancel' ) { + # Backend 'cancel' method + # cancel requires a specific request, so first, find it. + my $request = Koha::Illrequests->find($params->{illrequest_id}); + my $backend_result = $request->backend_cancel($params); + $template->param( + whole => $backend_result, + request => $request, + ); - $template->param( - request => $request - ); + # handle special commit rules & update type + handle_commit_maybe($backend_result, $request); -} elsif ( $op eq 'delete' ) { + } elsif ( $op eq 'edit_action' ) { + # Handle edits to the Illrequest object. + # (not the Illrequestattributes) + # We simulate the API for backend requests for uniformity. + # So, init: + my $request = Koha::Illrequests->find($params->{illrequest_id}); + if ( !$params->{stage} ) { + my $backend_result = { + error => 0, + status => '', + message => '', + method => 'edit_action', + stage => 'init', + next => '', + value => {} + }; + $template->param( + whole => $backend_result, + request => $request + ); + } else { + # Commit: + # Save the changes + $request->borrowernumber($params->{borrowernumber}); + $request->biblio_id($params->{biblio_id}); + $request->branchcode($params->{branchcode}); + $request->notesopac($params->{notesopac}); + $request->notesstaff($params->{notesstaff}); + $request->store; + my $backend_result = { + error => 0, + status => '', + message => '', + method => 'edit_action', + stage => 'commit', + next => 'illlist', + value => {} + }; + handle_commit_maybe($backend_result, $request); + } - # Check if the request is confirmed, if not, redirect - # to the confirmation view - if ($params->{confirmed} == 1) { - # We simply delete the request... - my $request = Koha::Illrequests->find( - $params->{illrequest_id} - )->delete; - # ... then return to list view. + } elsif ( $op eq 'moderate_action' ) { + # Moderate action is required for an ILL submodule / syspref. + # Currently still needs to be implemented. redirect_to_list(); - } else { - print $cgi->redirect( - "/cgi-bin/koha/ill/ill-requests.pl?" . - "method=delete_confirm&illrequest_id=" . - $params->{illrequest_id}); - } -} elsif ( $op eq 'mark_completed' ) { - my $request = Koha::Illrequests->find($params->{illrequest_id}); - my $backend_result = $request->mark_completed($params); - $template->param( - whole => $backend_result, - request => $request, - ); - - # handle special commit rules & update type - handle_commit_maybe($backend_result, $request); - -} elsif ( $op eq 'generic_confirm' ) { - my $request = Koha::Illrequests->find($params->{illrequest_id}); - $params->{current_branchcode} = C4::Context->mybranch; - my $backend_result = $request->generic_confirm($params); - $template->param( - whole => $backend_result, - request => $request, - ); - - # handle special commit rules & update type - handle_commit_maybe($backend_result, $request); - -} elsif ( $op eq 'illlist') { - # Display all current ILLs - my $requests = $illRequests->search(); - - $template->param( - requests => $requests - ); - - # If we receive a pre-filter, make it available to the template - my $possible_filters = ['borrowernumber']; - my $active_filters = []; - foreach my $filter(@{$possible_filters}) { - if ($params->{$filter}) { - push @{$active_filters}, - { name => $filter, value => $params->{$filter}}; + } elsif ( $op eq 'delete_confirm') { + my $request = Koha::Illrequests->find($params->{illrequest_id}); + + $template->param( + request => $request + ); + + } elsif ( $op eq 'delete' ) { + + # Check if the request is confirmed, if not, redirect + # to the confirmation view + if ($params->{confirmed}) { + # We simply delete the request... + Koha::Illrequests->find( $params->{illrequest_id} )->delete; + # ... then return to list view. + redirect_to_list(); + } else { + print $cgi->redirect( + "/cgi-bin/koha/ill/ill-requests.pl?" . + "method=delete_confirm&illrequest_id=" . + $params->{illrequest_id}); + exit; } - } - if (scalar @{$active_filters} > 0) { + + } elsif ( $op eq 'mark_completed' ) { + my $request = Koha::Illrequests->find($params->{illrequest_id}); + my $backend_result = $request->mark_completed($params); + $template->param( + whole => $backend_result, + request => $request, + ); + + # handle special commit rules & update type + handle_commit_maybe($backend_result, $request); + + } elsif ( $op eq 'generic_confirm' ) { + my $request = Koha::Illrequests->find($params->{illrequest_id}); + $params->{current_branchcode} = C4::Context->mybranch; + my $backend_result = $request->generic_confirm($params); $template->param( - prefilters => $active_filters + whole => $backend_result, + request => $request, ); + + # handle special commit rules & update type + handle_commit_maybe($backend_result, $request); + + } elsif ( $op eq 'illlist') { + # Display all current ILLs + my $requests = $illRequests->search(); + + $template->param( + requests => $requests + ); + + # If we receive a pre-filter, make it available to the template + my $possible_filters = ['borrowernumber']; + my $active_filters = []; + foreach my $filter(@{$possible_filters}) { + if ($params->{$filter}) { + push @{$active_filters}, + { name => $filter, value => $params->{$filter}}; + } + } + if (scalar @{$active_filters} > 0) { + $template->param( + prefilters => $active_filters + ); + } + } else { + my $request = Koha::Illrequests->find($params->{illrequest_id}); + my $backend_result = $request->custom_capability($op, $params); + $template->param( + whole => $backend_result, + request => $request, + ); + + # handle special commit rules & update type + handle_commit_maybe($backend_result, $request); } -} else { - my $request = Koha::Illrequests->find($params->{illrequest_id}); - my $backend_result = $request->custom_capability($op, $params); - $template->param( - whole => $backend_result, - request => $request, - ); - - # handle special commit rules & update type - handle_commit_maybe($backend_result, $request); } -# Get a list of backends -my $ir = Koha::Illrequest->new; - $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 => $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 ); @@ -255,4 +257,5 @@ sub handle_commit_maybe { sub redirect_to_list { print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl'); + exit; } 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 3f541d9283..b747ee030b 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 ') %]
- [% IF warnPrefBiblioAddsAuthorities || warnPrefEasyAnalyticalRecords || warnPrefAnonymousPatron || warnPrefAnonymousPatron_PatronDoesNotExist || warnNoActiveCurrency || QueryParserError || warnIsRootUser || xml_config_warnings.size || AutoSelfCheckPatronDoesNotHaveSelfCheckPerm || AutoSelfCheckPatronHasTooManyPerm || warnStatisticsFieldsError || warnNoTemplateCaching || has_ai_issues %] + [% IF warnPrefBiblioAddsAuthorities || warnPrefEasyAnalyticalRecords || warnPrefAnonymousPatron || warnPrefAnonymousPatron_PatronDoesNotExist || warnNoActiveCurrency || QueryParserError || warnIsRootUser || xml_config_warnings.size || AutoSelfCheckPatronDoesNotHaveSelfCheckPerm || AutoSelfCheckPatronHasTooManyPerm || warnStatisticsFieldsError || warnNoTemplateCaching || warnILLConfiguration || has_ai_issues %] [% IF (warnIsRootUser) %]

Warning regarding current user

You are logged in as the database administrative user. This is not recommended because some parts of Koha will not function as expected when using this account.

@@ -176,7 +176,7 @@
[% END %] - [% IF warnPrefBiblioAddsAuthorities || warnPrefEasyAnalyticalRecords || warnPrefAnonymousPatron || warnPrefAnonymousPatron_PatronDoesNotExist || warnNoActiveCurrency || QueryParserError || AutoSelfCheckPatronDoesNotHaveSelfCheckPerm || AutoSelfCheckPatronHasTooManyPerm || warnStatisticsFieldsError || warnNoTemplateCaching %] + [% IF warnPrefBiblioAddsAuthorities || warnPrefEasyAnalyticalRecords || warnPrefAnonymousPatron || warnPrefAnonymousPatron_PatronDoesNotExist || warnNoActiveCurrency || QueryParserError || AutoSelfCheckPatronDoesNotHaveSelfCheckPerm || AutoSelfCheckPatronHasTooManyPerm || warnStatisticsFieldsError || warnNoTemplateCaching || warnILLConfiguration %]

Warnings regarding the system configuration

@@ -226,6 +226,11 @@ That will bring a performance boost to enable it. [% END %] + [% IF warnILLConfiguration %] + + [% END %]
Preferences and parameters
Warning + [% IF no_ill_backends %]The ILL module is enabled, but there are no backends available.[%END %] +
[% END %] 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 96943c19ff..e57115bf05 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' %] Koha › ILL requests › [% INCLUDE 'doc-head-close.inc' %] - - + [% INCLUDE 'datatables.inc' %]