Bug 17842: UTF-8 encode ISO2709 MARC download from cart
[srvgit] / ill / ill-requests.pl
index 77a8d05..df80dd5 100755 (executable)
@@ -4,18 +4,18 @@
 # Copyright 2014 PTFS-Europe Ltd
 #
 # 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 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.
+# 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.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
@@ -26,10 +26,13 @@ use C4::Output;
 use Koha::AuthorisedValues;
 use Koha::Illcomment;
 use Koha::Illrequests;
+use Koha::Illrequest::Availability;
 use Koha::Libraries;
 use Koha::Token;
 
 use Try::Tiny;
+use URI::Escape;
+use JSON;
 
 our $cgi = CGI->new;
 my $illRequests = Koha::Illrequests->new;
@@ -74,17 +77,58 @@ if ( $backends_available ) {
             csrf_token => Koha::Token->new->generate_csrf({
                 session_id => scalar $cgi->cookie('CGISESSID'),
             }),
+            ( $params->{error} ? ( error => $params->{error} ) : () ),
         );
 
     } 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);
+        # Does this backend enable us to insert an availability stage and should
+        # we? If not, proceed as normal.
+        if (
+            # If the user has elected to continue with the request despite
+            # having viewed availability info, this flag will be set
+            C4::Context->preference("ILLCheckAvailability")
+              && !$params->{checked_availability}
+              && $request->_backend_capability( 'should_display_availability', $params )
+        ) {
+            # Establish which of the installed availability providers
+            # can service our metadata
+            my $availability = Koha::Illrequest::Availability->new($params);
+            my $services = $availability->get_services({
+                ui_context => 'staff'
+            });
+            if (scalar @{$services} > 0) {
+                # Modify our method so we use the correct part of the
+                # template
+                $op = 'availability';
+                $params->{method} = 'availability';
+                delete $params->{stage};
+                # Prepare the metadata we're sending them
+                my $metadata = $availability->prep_metadata($params);
+                $template->param(
+                    whole         => $params,
+                    metadata      => $metadata,
+                    services_json => scalar encode_json($services),
+                    services      => $services
+                );
+            } else {
+                # No services can process this metadata, so continue as normal
+                my $backend_result = $request->backend_create($params);
+                $template->param(
+                    whole   => $backend_result,
+                    request => $request
+                );
+                handle_commit_maybe($backend_result, $request);
+            }
+        } else {
+            my $backend_result = $request->backend_create($params);
+            $template->param(
+                whole   => $backend_result,
+                request => $request
+            );
+            handle_commit_maybe($backend_result, $request);
+        }
 
     } elsif ( $op eq 'migrate' ) {
         # We're in the process of migrating a request
@@ -98,24 +142,18 @@ if ( $backends_available ) {
                     whole   => $backend_result,
                     request => $new_request
                 );
+                $request = $new_request;
             } else {
-                # backend failure
-                $backend_result = {
-                    stage => 'commit',
-                    next  => 'illview',
-                    error => {
-                        message => 'Migrating to backedn does not support migrate',
-                        status => 'Migrating to backedn does not support migrate'
-                    }
-                };
-                $template->param(
-                    whole   => $backend_result,
-                    request => $request
-                );
+                # Backend failure, redirect back to illview
+                print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
+                      . '?method=illview'
+                      . '&illrequest_id='
+                      . $request->id
+                      . '&error=migrate_target' );
+                exit;
             }
         }
         else {
-            $request = Koha::Illrequests->find( $params->{illrequest_id} );
             $backend_result = $request->backend_migrate($params);
             $template->param(
                 whole   => $backend_result,
@@ -167,8 +205,8 @@ if ( $backends_available ) {
                 value   => {}
             };
             $template->param(
-                whole   => $backend_result,
-                request => $request
+                whole          => $backend_result,
+                request        => $request
             );
         } else {
             # Commit:
@@ -179,6 +217,10 @@ if ( $backends_available ) {
             $request->price_paid($params->{price_paid});
             $request->notesopac($params->{notesopac});
             $request->notesstaff($params->{notesstaff});
+            my $alias = (length $params->{status_alias} > 0) ?
+                $params->{status_alias} :
+                "-1";
+            $request->status_alias($alias);
             $request->store;
             my $backend_result = {
                 error   => 0,
@@ -239,19 +281,43 @@ if ( $backends_available ) {
             $request = Koha::Illrequests->find($params->{illrequest_id});
             $params->{current_branchcode} = C4::Context->mybranch;
             $backend_result = $request->generic_confirm($params);
+
             $template->param(
                 whole => $backend_result,
                 request => $request,
             );
+
+            # Prepare availability searching, if required
+            # Get the definition for the z39.50 plugin
+            if ( C4::Context->preference('ILLCheckAvailability') ) {
+                my $availability = Koha::Illrequest::Availability->new($request->metadata);
+                my $services = $availability->get_services({
+                    ui_context => 'partners',
+                    metadata => {
+                        name => 'ILL availability - z39.50'
+                    }
+                });
+                # Only pass availability searching stuff to the template if
+                # appropriate
+                if ( scalar @{$services} > 0 ) {
+                    my $metadata = $availability->prep_metadata($request->metadata);
+                    $template->param( metadata => $metadata );
+                    $template->param(
+                        services_json => scalar encode_json($services)
+                    );
+                    $template->param( services => $services );
+                }
+            }
+
             $template->param( error => $params->{error} )
                 if $params->{error};
         }
         catch {
             my $error;
-            if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
+            if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
                 $error = 'no_target_email';
             }
-            elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
+            elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
                 $error = 'no_library_email';
             }
             else {
@@ -267,23 +333,36 @@ if ( $backends_available ) {
 
         # handle special commit rules & update type
         handle_commit_maybe($backend_result, $request);
+    } elsif ( $op eq 'check_out') {
+        my $request = Koha::Illrequests->find($params->{illrequest_id});
+        my $backend_result = $request->check_out($params);
+        $template->param(
+            params  => $params,
+            whole   => $backend_result,
+            request => $request
+        );
     } elsif ( $op eq 'illlist') {
 
         # If we receive a pre-filter, make it available to the template
         my $possible_filters = ['borrowernumber'];
-        my $active_filters = [];
+        my $active_filters = {};
         foreach my $filter(@{$possible_filters}) {
             if ($params->{$filter}) {
-                push @{$active_filters},
-                    { name => $filter, value => $params->{$filter}};
+                # We shouldn't need to escape $filter here since we're using
+                # a whitelist, but just to be sure...
+                $active_filters->{uri_escape_utf8($filter)} =
+                    uri_escape_utf8(scalar $params->{$filter});
             }
         }
-        if (scalar @{$active_filters} > 0) {
-            $template->param(
-                prefilters => $active_filters
-            );
+        my @tpl_arr = ();
+        if (keys %{$active_filters}) {
+            foreach my $key (keys %{$active_filters}) {
+                push @tpl_arr, $key . "=" . $active_filters->{$key};
+            }
         }
-
+        $template->param(
+            prefilters => join("&", @tpl_arr)
+        );
     } elsif ( $op eq "save_comment" ) {
         die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
            session_id => scalar $cgi->cookie('CGISESSID'),