From 53a20e3afbace8750258fc2110fa5c8b3b355f82 Mon Sep 17 00:00:00 2001
From: Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
Date: Wed, 21 Jun 2017 17:02:09 +0200
Subject: [PATCH] Introduce delegation to backends for unmediated workflows.

* Koha/Illrequest.pm (backend_create): Add dispatch.

https://bugs.koha-community.org/show_bug.cgi?id=18837
---
 Koha/Illrequest.pm | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)

diff --git a/Koha/Illrequest.pm b/Koha/Illrequest.pm
index d91be8e5b1..b2df69df40 100644
--- a/Koha/Illrequest.pm
+++ b/Koha/Illrequest.pm
@@ -171,13 +171,15 @@ passing $args along with the invocation, and return its return value.
 sub _backend_capability {
     my ( $self, $name, $args ) = @_;
     my $capability = 0;
+    # See if capability is defined in backend.
     try {
         $capability = $self->_backend->capabilities($name);
     } catch {
         return 0;
     };
-    if ( $capability ) {
-        return $capability($args);
+    # Try to invoke it.
+    if ( $capability && ref($capability) eq 'CODE' ) {
+        return &{$capability}($args);
     } else {
         return 0;
     }
@@ -538,23 +540,16 @@ sub backend_create {
     # ...Updating status!
     $self->status('QUEUED')->store unless ( $permitted );
 
-    # FIXME: Fix Unmediated ILLs!
-    # Handle Unmediated ILLs
-    # if ( C4::Context->preference("UnmediatedILL") && $permitted
-    #       # XXX: Why && result manual?
-    #       && $result->{manual} ) {
-    #     # FIXME: Also carry out privilege checks
-    #     my ( $response, $new_rq ) = $self->place_request; # WTF?
-    #     if ( $response ) {
-    #         $result->{value}->{request} = $new_rq;
-    #         return $result;
-    #     } else {
-    #         die "Placing the request failed.";
-    #     }
-    # } else {
-    #     $result->{value}->{request} = $request;
-    #     return $result;
-    # }
+    ## Handle Unmediated ILLs
+
+    # For the unmediated workflow we only need to delegate to our backend. If
+    # that backend supports unmediateld_ill, it will do its thing and return a
+    # proper response.  If it doesn't then _backend_capability returns 0, so
+    # we keep the current result.
+    if ( C4::Context->preference("ILLModuleUnmediated") && $permitted ) {
+        my $unmediated_result = $self->_backend_capability('unmediated_ill', $args);
+        $result = $unmediated_result if $unmediated_result;
+    }
 
     return $self->expandTemplate($result);
 }
-- 
2.13.4