From 9440a64a6310d40049fc5f833feddca0176910c0 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Fri, 13 Mar 2020 21:12:10 +0200
Subject: [PATCH] Make holdings retrieval preserve any custom array elements.
 (#1590)

---
 module/VuFind/src/VuFind/ILS/Connection.php  |  24 +++--
 module/VuFind/src/VuFind/ILS/Logic/Holds.php | 106 +++++++++----------
 2 files changed, 69 insertions(+), 61 deletions(-)

diff --git a/module/VuFind/src/VuFind/ILS/Connection.php b/module/VuFind/src/VuFind/ILS/Connection.php
index c924d2bb9d4..8035a8490cf 100644
--- a/module/VuFind/src/VuFind/ILS/Connection.php
+++ b/module/VuFind/src/VuFind/ILS/Connection.php
@@ -1056,13 +1056,23 @@ class Connection implements TranslatorAwareInterface, LoggerAwareInterface
         $holdings = $this->__call('getHolding', [$id, $patron, $finalOptions]);
 
         // Return all the necessary details:
-        return [
-            'total' => $holdings['total'] ?? count($holdings),
-            'holdings' => $holdings['holdings'] ?? $holdings,
-            'electronic_holdings' => $holdings['electronic_holdings'] ?? [],
-            'page' => $finalOptions['page'],
-            'itemLimit' => $finalOptions['itemLimit'],
-        ];
+        if (!isset($holdings['holdings'])) {
+            $holdings = [
+                'total' => count($holdings),
+                'holdings' => $holdings,
+                'electronic_holdings' => [],
+            ];
+        } else {
+            if (!isset($holdings['total'])) {
+                $holdings['total'] = count($holdings['holdings']);
+            }
+            if (!isset($holdings['electronic_holdings'])) {
+                $holdings['electronic_holdings'] = [];
+            }
+        }
+        $holdings['page'] = $finalOptions['page'];
+        $holdings['itemLimit'] = $finalOptions['itemLimit'];
+        return $holdings;
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/ILS/Logic/Holds.php b/module/VuFind/src/VuFind/ILS/Logic/Holds.php
index 7de0258743d..64f9994b130 100644
--- a/module/VuFind/src/VuFind/ILS/Logic/Holds.php
+++ b/module/VuFind/src/VuFind/ILS/Logic/Holds.php
@@ -173,70 +173,68 @@ class Holds
      * Public method for getting item holdings from the catalog and selecting which
      * holding method to call
      *
-     * @param string $id  A Bib ID
-     * @param array  $ids A list of Source Records (if catalog is for a consortium)
+     * @param string $id      A Bib ID
+     * @param array  $ids     A list of Source Records (if catalog is for a
+     * consortium)
+     * @param array  $options Optional options to pass on to getHolding()
      *
      * @return array A sorted results set
      */
-    public function getHoldings($id, $ids = null)
+    public function getHoldings($id, $ids = null, $options = [])
     {
-        $holdings = [];
+        if (!$this->catalog) {
+            return [];
+        }
+        // Retrieve stored patron credentials; it is the responsibility of the
+        // controller and view to inform the user that these credentials are
+        // needed for hold data.
+        try {
+            $patron = $this->ilsAuth->storedCatalogLogin();
+
+            // Does this ILS Driver handle consortial holdings?
+            $config = $this->catalog->checkFunction(
+                'Holds', compact('id', 'patron')
+            );
+        } catch (ILSException $e) {
+            $patron = false;
+            $config = [];
+        }
 
-        // Get Holdings Data
-        if ($this->catalog) {
-            // Retrieve stored patron credentials; it is the responsibility of the
-            // controller and view to inform the user that these credentials are
-            // needed for hold data.
-            try {
-                $patron = $this->ilsAuth->storedCatalogLogin();
-
-                // Does this ILS Driver handle consortial holdings?
-                $config = $this->catalog->checkFunction(
-                    'Holds', compact('id', 'patron')
-                );
-            } catch (ILSException $e) {
-                $patron = false;
-                $config = [];
-            }
+        if (isset($config['consortium']) && $config['consortium'] == true) {
+            $result = $this->catalog->getConsortialHoldings(
+                $id, $patron ? $patron : null, $ids
+            );
+        } else {
+            $result = $this->catalog
+                ->getHolding($id, $patron ? $patron : null, $options);
+        }
 
-            if (isset($config['consortium']) && $config['consortium'] == true) {
-                $result = $this->catalog->getConsortialHoldings(
-                    $id, $patron ? $patron : null, $ids
-                );
-            } else {
-                $result = $this->catalog->getHolding($id, $patron ? $patron : null);
-            }
+        $grb = 'getRequestBlocks'; // use variable to shorten line below:
+        $blocks
+            = $patron && $this->catalog->checkCapability($grb, compact('patron'))
+            ? $this->catalog->getRequestBlocks($patron) : false;
+
+        $mode = $this->catalog->getHoldsMode();
 
-            $grb = 'getRequestBlocks'; // use variable to shorten line below:
-            $blocks
-                = $patron && $this->catalog->checkCapability($grb, compact('patron'))
-                ? $this->catalog->getRequestBlocks($patron) : false;
+        if ($mode == "disabled") {
+            $holdings = $this->standardHoldings($result);
+        } elseif ($mode == "driver") {
+            $holdings = $this->driverHoldings($result, $config, !empty($blocks));
+        } else {
+            $holdings = $this->generateHoldings($result, $mode, $config);
+        }
 
-            $mode = $this->catalog->getHoldsMode();
+        $holdings = $this->processStorageRetrievalRequests(
+            $holdings, $id, $patron, !empty($blocks)
+        );
+        $holdings = $this->processILLRequests(
+            $holdings, $id, $patron, !empty($blocks)
+        );
 
-            if ($mode == "disabled") {
-                $holdings = $this->standardHoldings($result);
-            } elseif ($mode == "driver") {
-                $holdings = $this->driverHoldings($result, $config, !empty($blocks));
-            } else {
-                $holdings = $this->generateHoldings($result, $mode, $config);
-            }
+        $result['blocks'] = $blocks;
+        $result['holdings'] = $this->formatHoldings($holdings);
 
-            $holdings = $this->processStorageRetrievalRequests(
-                $holdings, $id, $patron, !empty($blocks)
-            );
-            $holdings = $this->processILLRequests(
-                $holdings, $id, $patron, !empty($blocks)
-            );
-        }
-        return [
-            'blocks' => $blocks,
-            'total' => $result['total'],
-            'page' => $result['page'],
-            'itemLimit' => $result['itemLimit'],
-            'holdings' => $this->formatHoldings($holdings),
-            'electronic_holdings' => $result['electronic_holdings'] ?? [],
-        ];
+        return $result;
     }
 
     /**
-- 
GitLab