From 95471296a8d9c7c4dbb03de49134139d2e9ec206 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Lahmann?= <lahmann@ub.uni-leipzig.de>
Date: Thu, 22 Sep 2016 14:00:54 +0200
Subject: [PATCH] refs #8510: * implemented recorddriver method to get contents
 of Marc787 * implemented functionality to RecordLink view helper to search
 for a record identified by custom identifier and returning a link to this
 record

---
 .../finc/RecordDriver/SolrMarcFincTrait.php   |  56 +++++++++
 .../src/finc/View/Helper/Root/Factory.php     |  16 +++
 .../src/finc/View/Helper/Root/RecordLink.php  | 109 ++++++++++++++++++
 themes/finc/theme.config.php                  |   1 +
 4 files changed, 182 insertions(+)
 create mode 100644 module/finc/src/finc/View/Helper/Root/RecordLink.php

diff --git a/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php b/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php
index e656716220f..79e928540dd 100644
--- a/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php
+++ b/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php
@@ -1069,6 +1069,62 @@ trait SolrMarcFincTrait
         return $this->getFirstFieldValue('971', ['l']);
     }
 
+    /**
+     * Returns the contens of MARC 787 as an array using 787$i as associative key and
+     * having the array value with the key 'text' containing the contents of
+     * 787 $a{t} and the key 'link' containing a PPN to the mentioned record in
+     * 787 $a{t}.
+     *
+     * @return array|null
+     * @link https://intern.finc.info/issues/8510
+     */
+    public function getOtherRelationshipEntry()
+    {
+        $retval = [];
+        $defaultHeading = 'Note';
+
+        $fields = $this->getMarcRecord()->getFields('787');
+        if (!$fields) {
+            return null;
+        }
+        foreach ($fields as $field) {
+            // don't do anything unless we have something in $a
+            if ($a = $field->getSubfield('a')) {
+                // do we have a main entry heading?
+                if ($i = $field->getSubfield('i')) {
+                    // build the text to be displayed from subfields $a and/or $t
+                    $text = ($t = $field->getSubfield('t'))
+                        ? $a->getData() . ': ' . $t->getData()
+                        : $a->getData();
+
+                    // does a linked record exist
+                    $link = ($w = $field->getSubfield('w')) ? $w->getData() : '';
+
+                    // we expect the links to be ppns prefixed with an ISIL so strip
+                    // the ISIL
+                    $ppn = preg_replace(
+                        "/^\(([A-z])+\-([A-z0-9])+\)\s?/", "", $link
+                    );
+
+                    // let's use the main entry heading as associative key and push
+                    // the gathered content into the retval array
+                    $retval[$i->getData()][] = [
+                        'text' => $text,
+                        'link' => (!empty($ppn) ? $ppn : $link)
+                    ];
+                } else {
+                    // no main entry heading found, so push subfield a's content into
+                    // retval using the defaultHeading
+                    $retval[$defaultHeading][] = [
+                        'text' => $a->getData(),
+                        'link' => ''
+                    ];
+                }
+            }
+        }
+        return $retval;
+    }
+
     /**
      * Get an array of style/genre of a piece taken from the local data
      * of the Petrucci music library subfield 590a
diff --git a/module/finc/src/finc/View/Helper/Root/Factory.php b/module/finc/src/finc/View/Helper/Root/Factory.php
index e01d306fb5e..e4cef4206ed 100644
--- a/module/finc/src/finc/View/Helper/Root/Factory.php
+++ b/module/finc/src/finc/View/Helper/Root/Factory.php
@@ -74,6 +74,22 @@ class Factory
         );
     }
 
+    /**
+     * Construct the RecordLink helper.
+     *
+     * @param ServiceManager $sm Service manager.
+     *
+     * @return RecordLink
+     */
+    public static function getRecordLink(ServiceManager $sm)
+    {
+        return new RecordLink(
+            $sm->getServiceLocator()->get('VuFind\RecordRouter'),
+            $sm->getServiceLocator()->get('VuFind\RecordLoader'),
+            $sm->getServiceLocator()->get('VuFind\Search')
+        );
+    }
+
     /**
      * Construct the Record helper.
      *
diff --git a/module/finc/src/finc/View/Helper/Root/RecordLink.php b/module/finc/src/finc/View/Helper/Root/RecordLink.php
new file mode 100644
index 00000000000..9851ddf4c46
--- /dev/null
+++ b/module/finc/src/finc/View/Helper/Root/RecordLink.php
@@ -0,0 +1,109 @@
+<?php
+/**
+ * Record link view helper
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2010.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind
+ * @package  View_Helpers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @author   André Lahmann <lahmann@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+namespace finc\View\Helper\Root;
+use VuFindSearch\Query\Query as Query,
+    VuFind\Record\Loader as Loader,
+    VuFind\Record\Router as Router,
+    VuFindSearch\Service as SearchService;
+
+/**
+ * Record link view helper
+ *
+ * @category VuFind
+ * @package  View_Helpers
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @author   André Lahmann <lahmann@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     https://vufind.org/wiki/development Wiki
+ */
+class RecordLink extends \VuFind\View\Helper\Root\RecordLink
+{
+    /**
+     * Record router
+     *
+     * @var \VuFind\Record\Router
+     */
+    protected $router;
+
+    /**
+     * Record loader
+     *
+     * @var \VuFind\Record\Loader
+     */
+    protected $recordLoader;
+
+    /**
+     * Connection used when searching for fincid
+     *
+     * @var VuFindSearch\Service
+     */
+    protected $searchService;
+
+    /**
+     * Constructor
+     *
+     * @param \VuFind\Record\Router $router Record router
+     * @param \VuFind\Record\Loader $loader Record loader
+     * @param \VuFindSearch\Service $ss     Search service
+     */
+    public function __construct(Router $router, Loader $loader, SearchService $ss)
+    {
+        $this->router = $router;
+        $this->recordLoader = $loader;
+        $this->searchService = $ss;
+    }
+
+    /**
+     * Get the link to the record which is identified by $id in the Solr field $type.
+     * If multiple records are found the best guess is to return the URL to the first
+     * one. If none is found return null.
+     *
+     * @param string $id   Id identifying a specific record
+     * @param string $type Solr field to be searched, defaults to null (searching in
+     * any field)
+     *
+     * @return null|string Link to the found record, otherwise null
+     */
+    public function getRecordLink($id, $type = null)
+    {
+        try {
+            $query = $type . ':' . $id;
+            $result = $this->searchService->search('VuFind', new Query($query));
+            if (count($result) === 0) {
+                throw new \Exception(
+                    'Problem retrieving record with ' . $type . ":" . $id
+                );
+            }
+            return $this->getUrl(current($result->getRecords()));
+        } catch (\Exception $e) {
+            // logging etc won't help here, so do nothing
+        }
+        return null;
+    }
+}
diff --git a/themes/finc/theme.config.php b/themes/finc/theme.config.php
index 36bac408616..0f2f75214ec 100644
--- a/themes/finc/theme.config.php
+++ b/themes/finc/theme.config.php
@@ -12,6 +12,7 @@ return array(
         'factories' => array(
             'permission' => 'finc\View\Helper\Root\Factory::getPermission',
             'record' => 'finc\View\Helper\Root\Factory::getRecord',
+            'recordlink' => 'finc\View\Helper\Root\Factory::getRecordLink',
             'interlibraryloan' =>
                 'finc\View\Helper\Root\Factory::getInterlibraryLoanLink',
             'citation' => 'finc\View\Helper\Root\Factory::getCitation',
-- 
GitLab