From b45fc3c92317405f495aabc735027a94e889a189 Mon Sep 17 00:00:00 2001
From: Dorian Merz <merz@ub.uni-leipzig.de>
Date: Fri, 3 Aug 2018 14:59:57 +0200
Subject: [PATCH] refs #13765:

* enables retrieval of availability info via Bibliotheca mediennummer
* all changes marked as deprecated
---
 module/finc/config/module.config.php          |  2 +
 module/finc/src/finc/ILS/Driver/Factory.php   | 30 ++++++
 module/finc/src/finc/ILS/Driver/FincILS.php   |  8 +-
 module/finc/src/finc/ILS/Driver/FincTheca.php | 91 +++++++++++++++++++
 .../finc/RecordDriver/SolrMarcFincTrait.php   | 22 +++++
 5 files changed, 151 insertions(+), 2 deletions(-)
 create mode 100644 module/finc/src/finc/ILS/Driver/FincTheca.php

diff --git a/module/finc/config/module.config.php b/module/finc/config/module.config.php
index 2d330d37d22..b6b91aa270e 100644
--- a/module/finc/config/module.config.php
+++ b/module/finc/config/module.config.php
@@ -36,6 +36,8 @@ $config = [
                 'factories' => [
                     'fincils' => 'finc\ILS\Driver\Factory::getFincILS',
                     'paia' => 'finc\ILS\Driver\Factory::getPAIA',
+                    //finctheca is deprecated: Remove when Bibliotheca support ends
+                    'finctheca' => 'finc\ILS\Driver\Factory::GetFincTheca'
                 ],
             ],
             'recommend' => [
diff --git a/module/finc/src/finc/ILS/Driver/Factory.php b/module/finc/src/finc/ILS/Driver/Factory.php
index da1a515edde..5d05e83ef7d 100644
--- a/module/finc/src/finc/ILS/Driver/Factory.php
+++ b/module/finc/src/finc/ILS/Driver/Factory.php
@@ -95,4 +95,34 @@ class Factory
         return $paia;
     }
 
+    /**
+     * @deprecated Remove when Bibliotheca support ends
+     * @param ServiceManager $sm
+     * @return FincTheca
+     */
+    public static function getFincTheca(ServiceManager $sm)
+    {
+        $factory = new \ProxyManager\Factory\LazyLoadingValueHolderFactory($sm->getServiceLocator()->get('VuFind\ProxyConfig'));
+
+        $callback = function (& $wrapped, $proxy) use ($sm) {
+            $wrapped = $sm->getServiceLocator()->get('ZfcRbac\Service\AuthorizationService');
+
+            $proxy->setProxyInitializer(null);
+        };
+
+        $fl = new FincTheca(
+            $sm->getServiceLocator()->get('VuFind\DateConverter'),
+            $sm->getServiceLocator()->get('VuFind\SessionManager'),
+            $sm->getServiceLocator()->get('VuFind\RecordLoader'),
+            $sm->getServiceLocator()->get('VuFind\Search'),
+            $sm->getServiceLocator()->get('VuFind\Config')->get('config'),
+            $factory->createProxy('ZfcRbac\Service\AuthorizationService', $callback)
+        );
+
+        $fl->setCacheStorage(
+            $sm->getServiceLocator()->get('VuFind\CacheManager')->getCache('object')
+        );
+
+        return $fl;
+    }
 }
\ No newline at end of file
diff --git a/module/finc/src/finc/ILS/Driver/FincILS.php b/module/finc/src/finc/ILS/Driver/FincILS.php
index 3095a85dfe6..8b3102edb35 100644
--- a/module/finc/src/finc/ILS/Driver/FincILS.php
+++ b/module/finc/src/finc/ILS/Driver/FincILS.php
@@ -405,6 +405,10 @@ class FincILS extends PAIA implements LoggerAwareInterface
         }
     }
 
+    protected function doGetStatus($id) {
+        return parent::getStatus($id);
+    }
+
     /**
      * Get Statuses
      *
@@ -1344,7 +1348,7 @@ class FincILS extends PAIA implements LoggerAwareInterface
      *
      * @return \VuFind\RecordDriver\AbstractBase
      */
-    private function _getRecord($id)
+    protected function _getRecord($id)
     {
         return $this->recordLoader->load($id);
     }
@@ -1435,7 +1439,7 @@ class FincILS extends PAIA implements LoggerAwareInterface
      *
      * @return string $ilsRecordId
      */
-    private function _getILSRecordId($id, $ilsIdentifier = null)
+    protected function _getILSRecordId($id, $ilsIdentifier = null)
     {
         // override ilsIdentifier with the ilsIdentifier set in ILS driver config
         if ($ilsIdentifier == null) {
diff --git a/module/finc/src/finc/ILS/Driver/FincTheca.php b/module/finc/src/finc/ILS/Driver/FincTheca.php
new file mode 100644
index 00000000000..9ffc16537cf
--- /dev/null
+++ b/module/finc/src/finc/ILS/Driver/FincTheca.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace finc\ILS\Driver;
+
+/**
+ * Class FincTheca
+ * @deprecated Remove when Bibliotheca support ends
+ * @package finc\ILS\Driver
+ */
+class FincTheca extends FincILS {
+
+    private $identifier_type = 'ppn';
+    private $daiaIdPrefixBase;
+
+    public function init()
+    {
+        parent::init();
+        if (isset($this->config['DAIA']['daiaIdPrefixBase'])) {
+            $this->daiaIdPrefixBase = $this->config['DAIA']['daiaIdPrefixBase'];
+        }
+    }
+
+    public function getStatus($id)
+    {
+        $result = [];
+        try {
+            $result = parent::getStatus($id);
+        } catch (\Exception $e) {
+
+        }
+        if (empty($result)) {
+            $result = $this->getStatusViaMediennummer($id);
+        }
+        return $result;
+    }
+
+    /**
+     * @param $record_id String Finc-ID of record, used to retrieve Mediennummer
+     * @return array|mixed
+     */
+    protected function getStatusViaMediennummer($record_id) {
+        $result = [];
+        if ($ilsRecordId = $this->_getRecord($record_id)->tryMethod('getMediennummer')) {
+            $this->identifier_type = 'mediennr';
+            $result = parent::doGetStatus($ilsRecordId);
+            foreach($result as &$item) {
+                //fix-up IDs
+                $item['id'] = $record_id;
+            }
+        }
+        return $result;
+    }
+
+    public function getStatuses($ids)
+    {
+        $results = parent::getStatuses($ids);
+        if (count($results) < count($ids)) {
+            //some records had no availability info
+            $missing = array_flip($ids);
+            foreach ($results as $items) {
+                $item = current($items);
+                if (isset($missing[$item['id']])) unset($missing[$item['id']]);
+            }
+            foreach (array_keys($missing) as $missing_id) {
+                $results[] = $this->getStatusViaMediennummer($missing_id);
+            }
+        }
+        return $results;
+    }
+
+    protected function generateURI($id)
+    {
+        if (isset($this->daiaIdPrefixBase)) {
+            return
+                $this->daiaIdPrefixBase
+                .':'.$this->identifier_type
+                .':'.$id;
+        }
+        return parent::generateMultiURIs($id);
+    }
+
+    protected function getItemBarcode($item)
+    {
+        $matches = [];
+        if (preg_match('/^'.$this->daiaIdPrefixBase.':'.$this->identifier_type.':\w+:(\w+)$/',$item['id'],$matches)) {
+            return $matches[1];
+        }
+        return null;
+    }
+
+}
\ No newline at end of file
diff --git a/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php b/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php
index a9cd5074ba2..2fe54de4004 100644
--- a/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php
+++ b/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php
@@ -2073,4 +2073,26 @@ trait SolrMarcFincTrait
     {
         return $this->getFieldArray('384');
     }
+
+    /**
+     * @deprecated Remove when Bibliotheca support ends
+     * @returns items internal Bibliotheca-ID called "Mediennummer"
+     */
+    public function getMediennummer() {
+        // loop through all existing LocalMarcFieldOfLibrary
+        if ($fields = $this->getMarcRecord()->getFields(
+            $this->getLocalMarcFieldOfLibrary())
+        ) {
+            foreach($fields as $field) {
+                // return the first occurance of $m
+                $field = $field->getSubfield('a');
+                if ($field) {
+                    $matches = [];
+                    if (preg_match('/\w+$/',$field->getData(),$matches)) {
+                        return $matches[0];
+                    }
+                }
+            }
+        }
+    }
 }
-- 
GitLab