From 59ca295eb2afac19c378eda16c3f2321418fcded Mon Sep 17 00:00:00 2001
From: Theodoros Theodoropoulos <sbujam@users.noreply.github.com>
Date: Wed, 12 Oct 2016 19:49:09 +0300
Subject: [PATCH] Default pickup location behavior (#820)

* Set pickupLocation=homebranch(es) when neither defaultPickupLocation nor pickupLocation[] is set in KohaILSDI.ini
---
 config/vufind/KohaILSDI.ini                   |  3 ++
 .../src/VuFind/ILS/Driver/KohaILSDI.php       | 39 +++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/config/vufind/KohaILSDI.ini b/config/vufind/KohaILSDI.ini
index d4561db6811..f191e4c9c6b 100755
--- a/config/vufind/KohaILSDI.ini
+++ b/config/vufind/KohaILSDI.ini
@@ -43,6 +43,9 @@ extraHoldFields = comments:pickUpLocation:requiredByDate
 ; available. The default of 'false' will force users to pick a pickup
 ; location. By setting this to a Koha location code (e.g. '"MAIN"'),
 ; Vufind will default to that location.
+; If no defaultPickUpLocation and no pickupLocations are defined,
+; the driver will try to use the actual holdingbranch(es) of the item/title 
+; as a fallback.
 defaultPickUpLocation = "MAIN"
 
 ; branchcodes for libraries avalaible as pickup locations
diff --git a/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php b/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php
index 1e3f82a19ff..0cb28f7ebd8 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/KohaILSDI.php
@@ -463,6 +463,45 @@ class KohaILSDI extends \VuFind\ILS\Driver\AbstractBase implements
             if (!$this->db) {
                 $this->initDb();
             }
+            if (!$this->pickupEnableBranchcodes) {
+                // No defaultPickupLocation is defined in config 
+                // AND no pickupLocations are defined either
+                if (isset($holdDetails['item_id']) && (empty($holdDetails['level'])
+                    || $holdDetails['level'] == 'item')
+                ) {
+                    // We try to get the actual branchcode the item is found at
+                    $item_id = $holdDetails['item_id'];
+                    $sql = "SELECT holdingbranch
+                            FROM items
+                            WHERE itemnumber=($item_id)";
+                    try {
+                        $sqlSt = $this->db->prepare($sql);
+                        $sqlSt->execute();
+                        $this->pickupEnableBranchcodes = $sqlSt->fetch();
+                    } catch (PDOException $e) {
+                            $this->debug('Connection failed: ' . $e->getMessage());
+                            throw new ILSException($e->getMessage());
+                    }
+                } elseif (!empty($holdDetails['level'])
+                    && $holdDetails['level'] == 'title'
+                ) {
+                    // We try to get the actual branchcodes the title is found at
+                    $id = $holdDetails['id'];
+                    $sql = "SELECT DISTINCT holdingbranch
+                            FROM items
+                            WHERE biblionumber=($id)";
+                    try {
+                        $sqlSt = $this->db->prepare($sql);
+                        $sqlSt->execute();
+                        foreach ($sqlSt->fetchAll() as $row) {
+                            $this->pickupEnableBranchcodes[] = $row['holdingbranch'];
+                        }
+                    } catch (PDOException $e) {
+                            $this->debug('Connection failed: ' . $e->getMessage());
+                            throw new ILSException($e->getMessage());
+                    }
+                }
+            }
             $branchcodes = "'" . implode(
                 "','", $this->pickupEnableBranchcodes
             ) . "'";
-- 
GitLab