From 0ada62138654dcea50b73fc8d937556148c5dccd Mon Sep 17 00:00:00 2001
From: Josef Moravec <josef.moravec@gmail.com>
Date: Tue, 17 Dec 2019 21:27:03 +0100
Subject: [PATCH] Add ISMN and NBN identifiers capability to cover loader
 (#1451)

---
 .../src/VuFind/Content/AbstractCover.php      | 18 +++++++-
 .../src/VuFind/Controller/CoverController.php |  2 +
 module/VuFind/src/VuFind/Cover/Loader.php     | 32 ++++++++++++++-
 .../src/VuFind/RecordDriver/DefaultRecord.php | 30 +++++++++++++-
 .../VuFind/RecordDriver/MarcAdvancedTrait.php | 41 +++++++++++++++++++
 5 files changed, 120 insertions(+), 3 deletions(-)

diff --git a/module/VuFind/src/VuFind/Content/AbstractCover.php b/module/VuFind/src/VuFind/Content/AbstractCover.php
index 39a6e7b26b7..4df69cabe75 100644
--- a/module/VuFind/src/VuFind/Content/AbstractCover.php
+++ b/module/VuFind/src/VuFind/Content/AbstractCover.php
@@ -52,6 +52,13 @@ abstract class AbstractCover
      */
     protected $supportsIssn = false;
 
+    /**
+     * Does this plugin support ISMNs?
+     *
+     * @var bool
+     */
+    protected $supportsIsmn = false;
+
     /**
      * Does this plugin support OCLC numbers?
      *
@@ -66,6 +73,13 @@ abstract class AbstractCover
      */
     protected $supportsUpc = false;
 
+    /**
+     * Does this plugin support national bibliographies number?
+     *
+     * @var bool
+     */
+    protected $supportsNbn = false;
+
     /**
      * Are we allowed to cache images from this source?
      *
@@ -95,8 +109,10 @@ abstract class AbstractCover
         return
             ($this->supportsIsbn && isset($ids['isbn']))
             || ($this->supportsIssn && isset($ids['issn']))
+            || ($this->supportsIsmn && isset($ids['ismn']))
             || ($this->supportsOclc && isset($ids['oclc']))
-            || ($this->supportsUpc && isset($ids['upc']));
+            || ($this->supportsUpc && isset($ids['upc']))
+            || ($this->supportsNbn && isset($ids['nbn']));
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/Controller/CoverController.php b/module/VuFind/src/VuFind/Controller/CoverController.php
index c4d3b677068..6e8e0aa7f90 100644
--- a/module/VuFind/src/VuFind/Controller/CoverController.php
+++ b/module/VuFind/src/VuFind/Controller/CoverController.php
@@ -99,6 +99,8 @@ class CoverController extends \Zend\Mvc\Controller\AbstractActionController
             'upc' => $params()->fromQuery('upc'),
             'recordid' => $params()->fromQuery('recordid'),
             'source' => $params()->fromQuery('source'),
+            'nbn' => $params()->fromQuery('nbn'),
+            'ismn' => $params()->fromQuery('ismn'),
         ];
     }
 
diff --git a/module/VuFind/src/VuFind/Cover/Loader.php b/module/VuFind/src/VuFind/Cover/Loader.php
index 1ca347798e5..c521b77da49 100644
--- a/module/VuFind/src/VuFind/Cover/Loader.php
+++ b/module/VuFind/src/VuFind/Cover/Loader.php
@@ -30,6 +30,7 @@ namespace VuFind\Cover;
 
 use VuFind\Content\Covers\PluginManager as ApiManager;
 use VuFindCode\ISBN;
+use VuFindCode\ISMN;
 
 /**
  * Book Cover Generator
@@ -121,6 +122,20 @@ class Loader extends \VuFind\ImageLoader
      */
     protected $upc = null;
 
+    /**
+     * User National bibliography number parameter
+     *
+     * @var array
+     */
+    protected $nbn = null;
+
+    /**
+     * User ISMN parameter
+     *
+     * @var ISMN
+     */
+    protected $ismn = null;
+
     /**
      * User record id number parameter
      *
@@ -232,6 +247,8 @@ class Loader extends \VuFind\ImageLoader
             'upc' => null,
             'recordid' => null,
             'source' => null,
+            'nbn' => null,
+            'ismn' => null,
         ];
     }
 
@@ -268,6 +285,7 @@ class Loader extends \VuFind\ImageLoader
     protected function storeSanitizedSettings($settings)
     {
         $this->isbn = new ISBN($settings['isbn']);
+        $this->ismn = new ISMN($settings['ismn']);
         if (!empty($settings['issn'])) {
             $rawissn = preg_replace('/[^0-9X]/', '', strtoupper($settings['issn']));
             $this->issn = substr($rawissn, 0, 8);
@@ -278,6 +296,7 @@ class Loader extends \VuFind\ImageLoader
         $this->upc = $settings['upc'];
         $this->recordid = $settings['recordid'];
         $this->source = $settings['source'];
+        $this->nbn = $settings['nbn'];
         $this->type = preg_replace('/[^a-zA-Z]/', '', $settings['type']);
         $this->size = $settings['size'];
     }
@@ -289,7 +308,8 @@ class Loader extends \VuFind\ImageLoader
      * contain any or all of these keys: 'isbn' (ISBN), 'size' (requested size),
      * 'type' (content type), 'title' (title of book, for dynamic covers), 'author'
      * (author of book, for dynamic covers), 'callnumber' (unique ID, for dynamic
-     * covers), 'issn' (ISSN), 'oclc' (OCLC number), 'upc' (UPC number).
+     * covers), 'issn' (ISSN), 'oclc' (OCLC number), 'upc' (UPC number),
+     * 'nbn' (national bibliography number), 'ismn' (ISMN).
      *
      * @return void
      */
@@ -347,6 +367,10 @@ class Loader extends \VuFind\ImageLoader
             return $this->getCachePath($this->size, 'OCLC' . $ids['oclc']);
         } elseif (isset($ids['upc'])) {
             return $this->getCachePath($this->size, 'UPC' . $ids['upc']);
+        } elseif (isset($ids['nbn'])) {
+            return $this->getCachePath($this->size, 'NBN' . $ids['nbn']);
+        } elseif (isset($ids['ismn'])) {
+            return $this->getCachePath($this->size, 'ISMN' . $ids['ismn']->get13());
         } elseif (isset($ids['recordid']) && isset($ids['source'])) {
             return $this->getCachePath(
                 $this->size,
@@ -376,6 +400,12 @@ class Loader extends \VuFind\ImageLoader
         if ($this->upc && strlen($this->upc) > 0) {
             $ids['upc'] = $this->upc;
         }
+        if ($this->nbn && strlen($this->nbn) > 0) {
+            $ids['nbn'] = $this->nbn;
+        }
+        if ($this->ismn && $this->ismn->isValid()) {
+            $ids['ismn'] = $this->ismn;
+        }
         if ($this->recordid && strlen($this->recordid) > 0) {
             $ids['recordid'] = $this->recordid;
         }
diff --git a/module/VuFind/src/VuFind/RecordDriver/DefaultRecord.php b/module/VuFind/src/VuFind/RecordDriver/DefaultRecord.php
index 3b7dbfa3e8d..ddd3a3e842c 100644
--- a/module/VuFind/src/VuFind/RecordDriver/DefaultRecord.php
+++ b/module/VuFind/src/VuFind/RecordDriver/DefaultRecord.php
@@ -321,6 +321,27 @@ class DefaultRecord extends AbstractBase
         return empty($nums) ? false : $nums[0];
     }
 
+    /**
+     * Get just the first listed national bibliography number (or false if none
+     * available).
+     *
+     * @return mixed
+     */
+    public function getCleanNBN()
+    {
+        return false;
+    }
+
+    /**
+     * Get just the base portion of the first listed ISMN (or false if no ISSMs).
+     *
+     * @return mixed
+     */
+    public function getCleanISMN()
+    {
+        return false;
+    }
+
     /**
      * Get the main corporate authors (if any) for the record.
      *
@@ -1230,10 +1251,17 @@ class DefaultRecord extends AbstractBase
         if ($upc = $this->getCleanUPC()) {
             $arr['upc'] = $upc;
         }
+        if ($nbn = $this->getCleanNBN()) {
+            $arr['nbn'] = $nbn['nbn'];
+        }
+        if ($ismn = $this->getCleanISMN()) {
+            $arr['ismn'] = $ismn;
+        }
+
         // If an ILS driver has injected extra details, check for IDs in there
         // to fill gaps:
         if ($ilsDetails = $this->getExtraDetail('ils_details')) {
-            foreach (['isbn', 'issn', 'oclc', 'upc'] as $key) {
+            foreach (['isbn', 'issn', 'oclc', 'upc', 'nbn', 'ismn'] as $key) {
                 if (!isset($arr[$key]) && isset($ilsDetails[$key])) {
                     $arr[$key] = $ilsDetails[$key];
                 }
diff --git a/module/VuFind/src/VuFind/RecordDriver/MarcAdvancedTrait.php b/module/VuFind/src/VuFind/RecordDriver/MarcAdvancedTrait.php
index 85b5bfada82..0c7aaedb0ac 100644
--- a/module/VuFind/src/VuFind/RecordDriver/MarcAdvancedTrait.php
+++ b/module/VuFind/src/VuFind/RecordDriver/MarcAdvancedTrait.php
@@ -905,4 +905,45 @@ trait MarcAdvancedTrait
     {
         return $this->getFieldArray('035', 'a', true);
     }
+
+    /**
+     * Return first ISMN found for this record, or false if no one fonund
+     *
+     * @return mixed
+     */
+    public function getCleanISMN()
+    {
+        $fields024 = $this->getMarcRecord()->getFields('024');
+        $ismn = null;
+        foreach ($fields024 as $field) {
+            if ($field->getIndicator(1) == 2) {
+                $ismn = $field->getSubfield('a')->getData();
+                break;
+            }
+        }
+        return $ismn ?? false;
+    }
+
+    /**
+     * Return first national bibliography number found, or false if not found
+     *
+     * @return mixed
+     */
+    public function getCleanNBN()
+    {
+        $field = $this->getMarcRecord()->getField('015');
+        $nbn = false;
+        if ($field) {
+            $subfields = $this->getSubfieldArray($field, ['a', '7'], false);
+            if (!empty($subfields)) {
+                $nbn = [
+                    'nbn' => $subfields[0],
+                ];
+                if (isset($subfields[1])) {
+                    $nbn['source'] = $subfields[1];
+                }
+            }
+        }
+        return $nbn;
+    }
 }
-- 
GitLab