From 08d1f09dffcc8a177d92b2724544fc12ef3aed25 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Wed, 12 Sep 2018 21:49:37 +0300
Subject: [PATCH] Make MultiBackend use getStatuses of the ILS drivers. (#1227)

- More error tolerant and efficient than the previous loop.
- Resolves VUFIND-1245.
---
 .../src/VuFind/ILS/Driver/MultiBackend.php    | 51 +++++++++++++++----
 1 file changed, 41 insertions(+), 10 deletions(-)

diff --git a/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php b/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php
index db2d4bac3cc..eae0779b66a 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php
@@ -190,20 +190,51 @@ class MultiBackend extends AbstractBase implements \Zend\Log\LoggerAwareInterfac
      */
     public function getStatuses($ids)
     {
-        $items = [];
+        // Group records by source and request statuses from the drivers
+        $grouped = [];
         foreach ($ids as $id) {
-            try {
-                $items[] = $this->getStatus($id);
-            } catch (ILSException $e) {
-                $items[] = [
-                    [
-                        'id' => $id,
-                        'error' => 'An error has occurred'
-                    ]
+            $source = $this->getSource($id);
+            if (!isset($grouped[$source])) {
+                $driver = $this->getDriver($source);
+                $grouped[$source] = [
+                    'driver' => $driver,
+                    'ids' => []
                 ];
             }
+            $grouped[$source]['ids'][] = $id;
+        }
+
+        // Process each group
+        $results = [];
+        foreach ($grouped as $source => $current) {
+            // Get statuses only if a driver is configured for this source
+            if ($current['driver']) {
+                $localIds = array_map(
+                    function ($id) {
+                        return $this->getLocalId($id);
+                    },
+                    $current['ids']
+                );
+                try {
+                    $statuses = $current['driver']->getStatuses($localIds);
+                } catch (ILSException $e) {
+                    $statuses = array_map(
+                        function ($id) {
+                            return ['id' => $id, 'error' => 'An error has occurred'];
+                        },
+                        $localIds
+                    );
+                }
+                $statuses = array_map(
+                    function ($status) use ($source) {
+                        return $this->addIdPrefixes($status, $source);
+                    },
+                    $statuses
+                );
+                $results = array_merge($results, $statuses);
+            }
         }
-        return $items;
+        return $results;
     }
 
     /**
-- 
GitLab