From ff4a68ed9c1fbc078d8c5d1ec38a84095dc5d7f4 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 4 May 2020 10:38:08 -0400
Subject: [PATCH] Fix problem with PDFs failing to load. - The preferred ebook
 format parameter must be passed when requesting full text to ensure the
 correct link is returned.

---
 .../VuFind/Controller/EdsrecordController.php | 36 +++++++++++--------
 .../src/VuFindSearch/Backend/EDS/Backend.php  | 10 ++++--
 .../src/VuFindSearch/Backend/EDS/Base.php     |  5 +--
 3 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/module/VuFind/src/VuFind/Controller/EdsrecordController.php b/module/VuFind/src/VuFind/Controller/EdsrecordController.php
index 9b0569007d0..4ab9fb259fc 100644
--- a/module/VuFind/src/VuFind/Controller/EdsrecordController.php
+++ b/module/VuFind/src/VuFind/Controller/EdsrecordController.php
@@ -29,6 +29,7 @@ namespace VuFind\Controller;
 
 use Laminas\ServiceManager\ServiceLocatorInterface;
 use VuFind\Exception\Forbidden as ForbiddenException;
+use VuFindSearch\ParamBag;
 
 /**
  * EDS Record Controller
@@ -57,14 +58,18 @@ class EdsrecordController extends AbstractRecord
     }
 
     /**
-     * Action to display ePub.
+     * Redirect to an eBook.
+     *
+     * @param string $format Format of eBook to request from API.
+     * @param string $method Record driver method to use to obtain target URL.
      *
      * @return mixed
      */
-    public function epubAction()
+    protected function redirectToEbook($format, $method)
     {
-        $driver = $this->loadRecord();
-        //if the user is a guest, redirect them to the login screen.
+        $params = new ParamBag(['ebookpreferredformat' => $format]);
+        $driver = $this->loadRecord($params, true);
+        // If the user is a guest, redirect them to the login screen.
         $auth = $this->getAuthorizationService();
         if (!$auth->isGranted('access.EDSExtendedResults')) {
             if (!$this->getUser()) {
@@ -72,7 +77,17 @@ class EdsrecordController extends AbstractRecord
             }
             throw new ForbiddenException('Access denied.');
         }
-        return $this->redirect()->toUrl($driver->getEpubLink());
+        return $this->redirect()->toUrl($driver->tryMethod($method));
+    }
+
+    /**
+     * Action to display ePub.
+     *
+     * @return mixed
+     */
+    public function epubAction()
+    {
+        return $this->redirectToEbook('ebook-epub', 'getEpubLink');
     }
 
     /**
@@ -82,16 +97,7 @@ class EdsrecordController extends AbstractRecord
      */
     public function pdfAction()
     {
-        $driver = $this->loadRecord();
-        //if the user is a guest, redirect them to the login screen.
-        $auth = $this->getAuthorizationService();
-        if (!$auth->isGranted('access.EDSExtendedResults')) {
-            if (!$this->getUser()) {
-                return $this->forceLogin();
-            }
-            throw new ForbiddenException('Access denied.');
-        }
-        return $this->redirect()->toUrl($driver->getPdfLink());
+        return $this->redirectToEbook('ebook-pdf', 'getPdfLink');
     }
 
     /**
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Backend.php b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Backend.php
index d2cf32b607f..978c4e34557 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Backend.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Backend.php
@@ -273,10 +273,16 @@ class Backend extends AbstractBackend
                 );
             }
             list($dbId, $an) = $parts;
-            $hlTerms = (null != $params)
+            $hlTerms = (null !== $params)
                 ? $params->get('highlightterms') : null;
+            $extras = [];
+            if (null !== $params
+                && ($eBookFormat = $params->get('ebookpreferredformat'))
+            ) {
+                $extras['ebookpreferredformat'] = $eBookFormat;
+            }
             $response = $this->client->retrieve(
-                $an, $dbId, $authenticationToken, $sessionToken, $hlTerms
+                $an, $dbId, $authenticationToken, $sessionToken, $hlTerms, $extras
             );
         } catch (ApiException $e) {
             // if the auth or session token was invalid, try once more
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Base.php b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Base.php
index 373ccffc0f0..e7a0d70cb46 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Base.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/EDS/Base.php
@@ -173,16 +173,17 @@ abstract class Base
      * @param string $sessionToken        Session token
      * @param string $highlightTerms      Comma separated list of terms to highlight
      * in the retrieved record responses
+     * @param array  $extraQueryParams    Extra query string parameters
      *
      * @return array    The requested record
      */
     public function retrieve($an, $dbId, $authenticationToken, $sessionToken,
-        $highlightTerms = null
+        $highlightTerms = null, $extraQueryParams = []
     ) {
         $this->debugPrint(
             "Get Record. an: $an, dbid: $dbId, $highlightTerms: $highlightTerms"
         );
-        $qs = ['an' => $an, 'dbid' => $dbId];
+        $qs = $extraQueryParams + ['an' => $an, 'dbid' => $dbId];
         if (null != $highlightTerms) {
             $qs['highlightterms'] = $highlightTerms;
         }
-- 
GitLab