From b9970a8dbe51e6a631cf7712aff0ea690bfee679 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Mon, 29 Jun 2015 13:08:13 -0400
Subject: [PATCH] Added support for RefWorks and EndNote exports to Primo.

---
 .../VuFind/src/VuFind/RecordDriver/Primo.php  | 26 ++++----
 .../VuFindSearch/Backend/Primo/Connector.php  | 35 +++++++++-
 .../RecordDriver/Primo/export-endnote.phtml   | 66 +++++++++++++++++++
 .../RecordDriver/Primo/export-refworks.phtml  | 62 +++++++++++++++++
 4 files changed, 173 insertions(+), 16 deletions(-)
 create mode 100644 themes/root/templates/RecordDriver/Primo/export-endnote.phtml
 create mode 100644 themes/root/templates/RecordDriver/Primo/export-refworks.phtml

diff --git a/module/VuFind/src/VuFind/RecordDriver/Primo.php b/module/VuFind/src/VuFind/RecordDriver/Primo.php
index a7a8e9cce11..54b03c3cef7 100644
--- a/module/VuFind/src/VuFind/RecordDriver/Primo.php
+++ b/module/VuFind/src/VuFind/RecordDriver/Primo.php
@@ -123,27 +123,26 @@ class Primo extends SolrDefault
     }
 
     /**
-     * Get the title of the item that contains this record (i.e. MARC 773s of a
-     * journal).
+     * Get a full, free-form reference to the context of the item that contains this
+     * record (i.e. volume, year, issue, pages).
      *
      * @return string
      */
-    public function getContainerTitle()
+    public function getContainerReference()
     {
         $parts = explode(',', $this->getIsPartOf(), 2);
-        return isset($parts[0]) ? trim($parts[0]) : '';
+        return isset($parts[1]) ? trim($parts[1]) : '';
     }
 
     /**
-     * Get a full, free-form reference to the context of the item that contains this
-     * record (i.e. volume, year, issue, pages).
+     * Get the end page of the item that contains this record.
      *
      * @return string
      */
-    public function getContainerReference()
+    public function getContainerEndPage()
     {
-        $parts = explode(',', $this->getIsPartOf(), 2);
-        return isset($parts[1]) ? trim($parts[1]) : '';
+        return isset($this->fields['container_end_page'])
+            ? $this->fields['container_end_page'] : '';
     }
 
     /**
@@ -187,9 +186,8 @@ class Primo extends SolrDefault
     public function getSource()
     {
         $base = isset($this->fields['source']) ? $this->fields['source'] : '';
-        // Trim off unwanted image:
-        $parts = explode('<img', $base);
-        return $parts[0];
+        // Trim off unwanted image and any other tags:
+        return strip_tags($base);
     }
 
     /**
@@ -303,7 +301,7 @@ class Primo extends SolrDefault
      */
     public function exportDisabled($format)
     {
-        // Primo is not export-friendly; disable all formats.
-        return true;
+        // Support export for EndNote and RefWorks
+        return !in_array($format, ['EndNote', 'RefWorks']);
     }
 }
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Connector.php b/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Connector.php
index 66f4d808167..57501ec8fd2 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Connector.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Primo/Connector.php
@@ -438,7 +438,7 @@ class Connector implements \Zend\Log\LoggerAwareInterface
             $creator
                 = trim((string)$prefix->PrimoNMBib->record->display->creator);
             if (strlen($creator) > 0) {
-                $item['creator'] = explode(';', $creator);
+                $item['creator'] = array_map('trim', explode(';', $creator));
             }
             // subjects
             $subject
@@ -471,12 +471,13 @@ class Connector implements \Zend\Log\LoggerAwareInterface
             $item['language']
                 = (string)$prefix->PrimoNMBib->record->display->language;
             $item['source']
-                = (string)$prefix->PrimoNMBib->record->display->source;
+                = implode('; ', (array)$prefix->PrimoNMBib->record->display->source);
             $item['identifier']
                 = (string)$prefix->PrimoNMBib->record->display->identifier;
             $item['fulltext']
                 = (string)$prefix->PrimoNMBib->record->delivery->fulltext;
 
+            $item['issn'] = [];
             foreach ($prefix->PrimoNMBib->record->search->issn as $issn) {
                 $item['issn'][] = (string)$issn;
             }
@@ -493,6 +494,36 @@ class Connector implements \Zend\Log\LoggerAwareInterface
                 ? (string)$sear->LINKS->openurl
                 : (string)$sear->GETIT->attributes()->GetIt2;
 
+            // Container data
+            $addata = $prefix->PrimoNMBib->record->addata;
+            $item['container_title'] = (string)$addata->jtitle;
+            $item['container_volume'] = (string)$addata->volume;
+            $item['container_issue'] = (string)$addata->issue;
+            $item['container_start_page'] = (string)$addata->spage;
+            $item['container_end_page'] = (string)$addata->epage;
+            foreach ($addata->eissn as $eissn) {
+                if (!in_array((string)$eissn, $item['issn'])) {
+                    $item['issn'][] = (string)$eissn;
+                }
+            }
+            foreach ($addata->issn as $issn) {
+                if (!in_array((string)$issn, $item['issn'])) {
+                    $item['issn'][] = (string)$issn;
+                }
+            }
+
+            // Remove dash-less ISSNs if there are corresponding dashed ones
+            // (We could convert dash-less ISSNs to dashed ones, but try to stay
+            // true to the metadata)
+            $callback = function ($issn) use ($item) {
+                return strlen($issn) != 8
+                    || !in_array(
+                        substr($issn, 0, 4) . '-' . substr($issn, 4),
+                        $item['issn']
+                    );
+            };
+            $item['issn'] = array_values(array_filter($item['issn'], $callback));
+
             $item['fullrecord'] = $prefix->PrimoNMBib->record->asXml();
             $items[] = $item;
         }
diff --git a/themes/root/templates/RecordDriver/Primo/export-endnote.phtml b/themes/root/templates/RecordDriver/Primo/export-endnote.phtml
new file mode 100644
index 00000000000..9d1fe45b77b
--- /dev/null
+++ b/themes/root/templates/RecordDriver/Primo/export-endnote.phtml
@@ -0,0 +1,66 @@
+<?
+// Convert Primo format to EndNote format:
+$formats = $this->driver->tryMethod('getFormats');
+if (is_array($formats) && !empty($formats[0])) {
+    $format = $formats[0];
+} else {
+    $format = $formats;
+}
+$endnoteFormat = 'Generic';
+
+switch ($format) {
+case 'Article':
+    $endnoteFormat = 'Electronic Article';
+    break;
+case 'Book Chapter':
+    $endnoteFormat = 'Book Section';
+    break;
+case 'Book':
+    $endnoteFormat = 'Book';
+    break;
+case 'Audio':
+case 'Audio Video':
+case 'Video':
+    $endnoteFormat = 'Audiovisual Material';
+    break;
+case 'Conference Proceeding':
+    $endnoteFormat = 'Conference Proceedings';
+    break;
+case 'Database':
+    // No suitable mapping
+    break;
+case 'Dissertation':
+    $endnoteFormat = 'Thesis';
+    break;
+case 'Image':
+    // No suitable mapping
+    break;
+case 'Journal':
+    $endnoteFormat = 'Journal Article';
+    break;
+case 'Legal_Document':
+    // No suitable mapping
+    break;
+case 'Newspaper Article':
+    // If there is issue in the record, use 'Journal Article'. Otherwise
+    // EndNote might use issue as the start page.
+    if ($this->driver->tryMethod('getContainerIssue')) {
+        $endnoteFormat = 'Journal Article';
+    } else {
+        $endnoteFormat = 'Newspaper Article';
+    }
+    break;
+case 'Reference_Entry':
+    // No suitable mapping
+    break;
+case 'Score':
+    // No suitable mapping
+    break;
+case 'Website':
+    // No suitable mapping
+    break;
+}
+
+$this->overrideFormats = [$endnoteFormat];
+// Use the default template, but override the formats:
+echo $this->render('RecordDriver/AbstractBase/export-endnote.phtml');
diff --git a/themes/root/templates/RecordDriver/Primo/export-refworks.phtml b/themes/root/templates/RecordDriver/Primo/export-refworks.phtml
new file mode 100644
index 00000000000..290aef66043
--- /dev/null
+++ b/themes/root/templates/RecordDriver/Primo/export-refworks.phtml
@@ -0,0 +1,62 @@
+<?
+// Convert Primo format to RefWorks format:
+$formats = $this->driver->tryMethod('getFormats');
+if (is_array($formats) && !empty($formats[0])) {
+    $format = $formats[0];
+} else {
+    $format = $formats;
+}
+$refworksFormat = 'Generic';
+
+switch ($format) {
+case 'Article':
+    $refworksFormat = 'Journal Article';
+    break;
+case 'Book Chapter':
+    $refworksFormat = 'Book, Section';
+    break;
+case 'Book':
+    $refworksFormat = 'Book, Whole';
+    break;
+case 'Audio':
+    $refworksFormat = 'Sound Recording';
+    break;
+case 'Audio Video':
+case 'Video':
+    $refworksFormat = 'Video/ DVD';
+    break;
+case 'Conference Proceeding':
+    $refworksFormat = 'Conference Proceedings';
+    break;
+case 'Database':
+    // No suitable mapping
+    break;
+case 'Dissertation':
+    $refworksFormat = 'Dissertation/Thesis';
+    break;
+case 'Image':
+    // No suitable mapping
+    break;
+case 'Journal':
+    $refworksFormat = 'Journal, Electronic';
+    break;
+case 'Legal Document':
+    // No suitable mapping
+    break;
+case 'Newspaper Article':
+    $refworksFormat = 'Newspaper Article';
+    break;
+case 'Reference Entry':
+    // No suitable mapping
+    break;
+case 'Score':
+    $refworksFormat = 'Music Score';
+    break;
+case 'Website':
+    $refworksFormat = 'Web Page';
+    break;
+}
+
+$this->overrideFormats = [$refworksFormat];
+// Use the default template, but override the formats:
+echo $this->render('RecordDriver/AbstractBase/export-refworks.phtml');
-- 
GitLab