From 815608a349106b157d19a43ee65a10a6484c1a17 Mon Sep 17 00:00:00 2001 From: Ere Maijala <ere.maijala@helsinki.fi> Date: Fri, 9 Feb 2018 13:56:39 +0200 Subject: [PATCH] SierraRest: Add support for API v4 features that allow fetching pickup locations and adding comments when placing a hold. (#1114) --- config/vufind/SierraRest.ini | 17 +++--- .../src/VuFind/ILS/Driver/SierraRest.php | 58 ++++++++++++++++++- 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/config/vufind/SierraRest.ini b/config/vufind/SierraRest.ini index 4c9460a7c34..c702bcb5dc3 100644 --- a/config/vufind/SierraRest.ini +++ b/config/vufind/SierraRest.ini @@ -21,11 +21,11 @@ redirect_uri = "http://localhost/vufind/MyResearch/SierraAuth" ; This section is used to define library codes and named values which are used by the ; system to indicate the location at which a hold is to be collected. Sierra REST API -; does not currently support retrieval of pickup locations, so filling this section -; is mandatory for holds to work. -[pickUpLocations] -m01 = "Main Library" -m02 = "Branch Library" +; only supports retrieval of pickup locations starting at v4, so filling this section +; is mandatory for holds to work if only an older API version is available. +;[pickUpLocations] +;m01 = "Main Library" +;m02 = "Branch Library" ; This section controls hold behavior; note that you must also ensure that Holds are ; enabled in the [Catalog] section of config.ini in order to take advantage of these @@ -45,9 +45,10 @@ HMACKeys = item_id:holdtype:level ; e.g. 0:1:0 will set a "not required after" date of 1 month from the current date defaultRequiredDate = 0:1:0 -; extraHoldFields - A colon-separated list used to display extra visible fields in the -; place holds form. Supported values are "requiredByDate" and "pickUpLocation" -extraHoldFields = requiredByDate:pickUpLocation +; extraHoldFields - A colon-separated list used to display extra visible fields in +; the place holds form. Supported values are "requiredByDate", "pickUpLocation" and +; "comments". Comments are only supported with Sierra API v4 onwards. +extraHoldFields = requiredByDate:pickUpLocation:comments ; A Pick Up Location Code used to pre-select the pick up location drop down list and ; provide a default option if others are not available. Must be one of the following: diff --git a/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php b/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php index b7ff9cfb3f4..65e9d87b6c1 100644 --- a/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php +++ b/module/VuFind/src/VuFind/ILS/Driver/SierraRest.php @@ -877,7 +877,41 @@ class SierraRest extends AbstractBase implements TranslatorAwareInterface, return $locations; } - return []; + $result = $this->makeRequest( + ['v4', 'branches', 'pickupLocations'], + [ + 'limit' => 10000, + 'offset' => 0, + 'fields' => 'code,name', + 'language' => $this->getTranslatorLocale() + ], + 'GET', + $patron + ); + if (isset($result['code'])) { + // An error was returned + $this->error( + "Request for pickup locations returned error code: {$result['code']}" + . ", HTTP status: {$result['httpStatus']}, name: {$result['name']}" + ); + throw new ILSException('Problem with Sierra REST API.'); + } + if (empty($result)) { + return []; + } + + $locations = []; + foreach ($result as $entry) { + $locations[] = [ + 'locationID' => $entry['code'], + 'locationDisplay' => $this->translateLocation( + ['code' => $entry['code'], 'name' => $entry['name']] + ) + ]; + } + + usort($locations, [$this, 'pickupLocationSortFunction']); + return $locations; } /** @@ -997,9 +1031,12 @@ class SierraRest extends AbstractBase implements TranslatorAwareInterface, 'Y-m-d', $holdDetails['requiredBy'] ) ]; + if ($comment) { + $request['note'] = $comment; + } $result = $this->makeRequest( - ['v3', 'patrons', $patron['id'], 'holds', 'requests'], + [$comment ? 'v4' : 'v3', 'patrons', $patron['id'], 'holds', 'requests'], json_encode($request), 'POST', $patron @@ -1800,6 +1837,23 @@ class SierraRest extends AbstractBase implements TranslatorAwareInterface, return $result; } + /** + * Pickup location sort function + * + * @param array $a First pickup location record to compare + * @param array $b Second pickup location record to compare + * + * @return int + */ + protected function pickupLocationSortFunction($a, $b) + { + $result = strcmp($a['locationDisplay'], $b['locationDisplay']); + if ($result == 0) { + $result = $a['locationID'] - $b['locationID']; + } + return $result; + } + /** * Is the selected pickup location valid for the hold? * -- GitLab