From 53b0081e9f53e2c85cf2da603be2581b237be8d1 Mon Sep 17 00:00:00 2001
From: Dorian Merz <merz@ub.uni-leipzig.de>
Date: Wed, 26 May 2021 11:14:20 +0200
Subject: [PATCH] refs #16914 [finc] simplify record link rendering

* do not check for existence of linked record on every call
* add fallback seacrh in case of calling a missing record
---
 .../src/finc/Controller/RecordController.php  | 26 ++++++++++++++++
 .../src/finc/View/Helper/Root/RecordLink.php  | 30 +++++++++++++++----
 .../data-hierarchyParentTitle.phtml           |  2 +-
 .../SolrAI/data-containerTitle.phtml          |  2 +-
 4 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/module/finc/src/finc/Controller/RecordController.php b/module/finc/src/finc/Controller/RecordController.php
index 7e3702e24fc..ee9b3d49e75 100644
--- a/module/finc/src/finc/Controller/RecordController.php
+++ b/module/finc/src/finc/Controller/RecordController.php
@@ -113,4 +113,30 @@ class RecordController extends \VuFind\Controller\RecordController implements
         // No followup info found?  Send back to record view:
         return $this->redirectToRecord();
     }
+
+    /**
+     * Home (default) action -- forward to requested (or default) tab.
+     *
+     * @return mixed
+     */
+    public function homeAction()
+    {
+        if ($this->params()->fromQuery('catchMissing', null)) {
+            try {
+                return parent::homeAction();
+            } catch (\VuFind\Exception\RecordMissing $ex) {
+                $lookfor = $this->params()->fromQuery('catchMissing_lookfor','');
+                if (empty($lookfor)) throw $ex;
+                $type = $this->params()->fromQuery('catchMissing_searchType','Allfields');
+                $link = $this->redirect()->toRoute(
+                    'search-results',
+                    [],
+                    ['query' => compact('lookfor','type')]
+                );
+                return $link;
+            }
+        }
+
+        return parent::homeAction();
+    }
 }
diff --git a/module/finc/src/finc/View/Helper/Root/RecordLink.php b/module/finc/src/finc/View/Helper/Root/RecordLink.php
index 12e27d4066f..aedb06a3acd 100644
--- a/module/finc/src/finc/View/Helper/Root/RecordLink.php
+++ b/module/finc/src/finc/View/Helper/Root/RecordLink.php
@@ -150,13 +150,31 @@ class RecordLink extends \VuFind\View\Helper\Root\RecordLink
      *
      * @return string
      */
-    public function getUrl($driver)
+    public function getUrl(
+        $driver, $catchMissingLookfor = '', $catchMissingSearchType = 'AllFields'
+    )
     {
-        try {
-            return $this->getTabUrl($driver);
-        } catch (RecordMissingException $exception) {
-            // return default result on Missing Record
-            // throw all other Exceptions
+        if (is_object($driver)) {
+            try {
+                return $this->getTabUrl($driver);
+            } catch (RecordMissingException $exception) {
+                // return default result on Missing Record
+                // throw all other Exceptions
+            }
+        } elseif (is_string($driver)) {
+            $ids = explode('|',$driver);
+            $id = array_shift($ids);
+            if ($id === 'Solr') $id = array_shift($ids);
+            $params['id'] = $id;
+            if (!empty($catchMissingLookfor)) {
+                $options['query'] = [
+                    'catchMissing' => true,
+                    'catchMissing_lookfor' => $catchMissingLookfor,
+                    'catchMissing_searchType' => $catchMissingSearchType
+                ];
+            }
+            $urlHelper = $this->getView()->plugin('url');
+            return $urlHelper('record',$params,$options ?? []);
         }
         return "";
     }
diff --git a/themes/finc/templates/RecordDriver/DefaultRecord/data-hierarchyParentTitle.phtml b/themes/finc/templates/RecordDriver/DefaultRecord/data-hierarchyParentTitle.phtml
index fc59049c3a4..c926289b579 100644
--- a/themes/finc/templates/RecordDriver/DefaultRecord/data-hierarchyParentTitle.phtml
+++ b/themes/finc/templates/RecordDriver/DefaultRecord/data-hierarchyParentTitle.phtml
@@ -3,7 +3,7 @@
   <?php $hierarchyParentId = $this->driver->tryMethod('getHierarchyParentID'); ?>
   <?php foreach ($data as $key => $title): ?>
     <?php if (isset($hierarchyParentId[$key])): ?>
-      <a href="<?=$this->recordLink()->getUrl($hierarchyParentId[$key]);?>"><?=$this->escapeHtml($title)?></a>
+      <a href="<?=$this->recordLink()->getUrl($hierarchyParentId[$key],$hierarchyParentId[$key],'ParentID');?>"><?=$this->escapeHtml($title)?></a>
     <?php else: ?>
       <?=$this->escapeHtml($title)?>
     <?php endif; ?>
diff --git a/themes/finc/templates/RecordDriver/SolrAI/data-containerTitle.phtml b/themes/finc/templates/RecordDriver/SolrAI/data-containerTitle.phtml
index f271e5a3854..7e3472c48c4 100644
--- a/themes/finc/templates/RecordDriver/SolrAI/data-containerTitle.phtml
+++ b/themes/finc/templates/RecordDriver/SolrAI/data-containerTitle.phtml
@@ -10,7 +10,7 @@
 
   // try container id as link - VuFind native behaviour
   if ($containerID) {
-    $journalLink = $this->recordLink()->getUrl("$containerSource|$containerID");
+    $journalLink = $this->recordLink()->getUrl("$containerSource|$containerID",$data,'JournalTitle');
   } // try to link via issn - finc adapted behaviour
   elseif (!empty($issns)) {
     $journalLink = $this->record($this->driver)->getLink('isn', $issns);
-- 
GitLab