From 758088a6fb502f231aba5fb15041d581aad84e27 Mon Sep 17 00:00:00 2001
From: Robert Lange <robert.lange@uni-leipzig.de>
Date: Mon, 8 Mar 2021 17:14:03 +0100
Subject: [PATCH] refs #19292 [finc] simplify record icons

* move code for determination of record icon class type from template record-icon to record view helper
* combine access icon and text
** add parameter addText to method getRecordIcon to return icon with text

co-authored by: Dorian Merz <merz@ub.uni-leipzig.de>
* some refactoring of cover loading logic
** include missing records
** introduce another helper function
** adapt template
** also refs #19382_b
---
 .../finc/src/finc/View/Helper/Root/Record.php | 46 +++++++++++++++----
 .../DefaultRecord/record-icon-text.phtml      | 17 ++-----
 .../DefaultRecord/record-icon.phtml           | 22 +++------
 .../DefaultRecord/result-list.phtml           |  1 -
 .../RecordDriver/SolrAI/result-list.phtml     |  1 -
 themes/finc/templates/record/cover.phtml      |  2 +-
 .../templates/record/coverReplacement.phtml   |  2 +-
 7 files changed, 48 insertions(+), 43 deletions(-)

diff --git a/module/finc/src/finc/View/Helper/Root/Record.php b/module/finc/src/finc/View/Helper/Root/Record.php
index 847bec98d2c..63545be5c92 100644
--- a/module/finc/src/finc/View/Helper/Root/Record.php
+++ b/module/finc/src/finc/View/Helper/Root/Record.php
@@ -2,7 +2,7 @@
 /**
  * Record driver view helper
  *
- * PHP version 5
+ * PHP version 7
  *
  * Copyright (C) Villanova University 2010.
  *
@@ -103,27 +103,34 @@ class Record extends \VuFind\View\Helper\Root\Record
     /**
      * Render a (list of) record icons.
      *
-     * @param string $tpl Define alternative template for record icon. Default:
-     *                      record-icon.phtml
+     * @param string $tpl Define alternative template for record icon. Default: record-icon.phtml
+     * @param bool $addText Return additional access / format. Default: true
      *
      * @return string
      */
-    public function getRecordIcon($tpl = 'record-icon')
+    public function getRecordIcon(string $tpl = 'record-icon', bool $addText = true): string
     {
-        return $this->renderTemplate($tpl . '.phtml');
+        $iconType = $this->getRecordIconType();
+        $iconClass = $this->getRecordIconClass($iconType);
+        return $this->renderTemplate($tpl . '.phtml', ['iconClass' => $iconClass])
+            . ($addText ? $this->getRecordIconText($iconType,$iconClass) : '');
     }
 
     /**
      * Get the CSS class used to properly render an icon for given value
      *
-     * @param string $value (identifier) Value to convert into CSS class
+     * @param string $value (identifier) Value to convert into CSS class. Default: null
      * @param string $classfile Define alternative file for icon class without
      *                              suffix. Default: record-icon-class.phtml
      *
      * @return string
      */
-    public function getRecordIconClass($value, $classfile = 'record-icon-class')
+    public function getRecordIconClass(string $value = null, string $classfile = 'record-icon-class'): string
     {
+        if ($value === null) {
+            $value = $this->getRecordIconType();
+        }
+
         return $this->renderTemplate(
             $classfile . '.phtml',
             ['value' => $value]
@@ -137,13 +144,34 @@ class Record extends \VuFind\View\Helper\Root\Record
      *
      * @return string
      */
-    public function getRecordIconText($classfile = 'record-icon-text')
+    public function getRecordIconText(string $iconType = null, string $iconClass = null, $classfile = 'record-icon-text'): string
     {
+        if (null === $iconType) {
+            $iconType = $this->getRecordIconType();
+        }
+        if (null === $iconClass) {
+            $iconClass = $this->getRecordIconClass($iconType);
+        }
         return $this->renderTemplate(
-            $classfile . '.phtml'
+            $classfile . '.phtml', compact('iconType','iconClass')
         );
     }
 
+    function getRecordIconType() {
+
+        /* block copied from record-icon - RL */
+        $recordType = $this->driver->getRecordType();
+        switch ($recordType) {
+            case 'lido':
+                return "object";
+            case 'missing':
+            case 'marcfincpda':
+                return $recordType;
+            default:
+                return implode('', $this->driver->getFacetAvail());
+        }
+    }
+
     /**
      * Returns if style based icons should be shown (if covers are disabled!)
      *
diff --git a/themes/finc/templates/RecordDriver/DefaultRecord/record-icon-text.phtml b/themes/finc/templates/RecordDriver/DefaultRecord/record-icon-text.phtml
index 50bd4d1b6e9..997f06c9234 100644
--- a/themes/finc/templates/RecordDriver/DefaultRecord/record-icon-text.phtml
+++ b/themes/finc/templates/RecordDriver/DefaultRecord/record-icon-text.phtml
@@ -1,20 +1,9 @@
 <!-- finc: recordDriver - DefaultRecord - record-icon-text  -->
-<?php
-  /* block copied from record-icon - RL */
-  $recordType = $this->driver->getRecordType();
-  if ($recordType == "marcfincpda") {
-    $type = $recordType;
-  } elseif ($recordType == "lido") {
-    $type = "object";
-  } else {
-    $type = implode('', $this->driver->getFacetAvail());
-  }
-?>
-<?php if ($iconClass = $this->record($this->driver)->getRecordIconClass($type)): ?>
-  <span class="access-icon hidden-print">
+<?php if ($iconClass): ?>
+  <div class="access-icon hidden-print">
     <span>
       <?=$this->translate("($iconClass)")?>
     </span>
-  </span>
+  </div>
 <?php endif; ?>
 <!-- finc: recordDriver - DefaultRecord - record-icon-text - END -->
\ No newline at end of file
diff --git a/themes/finc/templates/RecordDriver/DefaultRecord/record-icon.phtml b/themes/finc/templates/RecordDriver/DefaultRecord/record-icon.phtml
index b36f126e35c..bfa3afa3183 100644
--- a/themes/finc/templates/RecordDriver/DefaultRecord/record-icon.phtml
+++ b/themes/finc/templates/RecordDriver/DefaultRecord/record-icon.phtml
@@ -1,18 +1,8 @@
 <!-- finc: recordDriver - DefaultRecord - record-icon  -->
-<?php
-  /* finc: template is NOT identical with de_15 - RL */
-  $recordType = $this->driver->getRecordType();
-  if ($recordType == "marcfincpda") {
-    $type = $recordType;
-  } elseif ($recordType == "lido") {
-    $type = "object";
-  } else {
-    $type = implode('', $this->driver->getFacetAvail());
-  }
-  $iconClass = $this->record($this->driver)->getRecordIconClass($type);
-?>
-<span class="access-icon hidden-print">
-  <i aria-hidden="true" class="fa <?=$iconClass?>"></i>
-  <?php /* separate access text from icon - moved to cover.phtml */ ?>
-</span>
+<?php if (!empty($iconClass)): ?>
+  <span class="access-icon hidden-print">
+    <i aria-hidden="true" class="fa <?=$iconClass?>"></i>
+    <?php /* separate access text from icon - moved to cover.phtml */ ?>
+  </span>
+<?php endif; ?>
 <!-- finc: recordDriver - DefaultRecord - record-icon - END -->
\ No newline at end of file
diff --git a/themes/finc/templates/RecordDriver/DefaultRecord/result-list.phtml b/themes/finc/templates/RecordDriver/DefaultRecord/result-list.phtml
index 767f185304e..878760060d4 100644
--- a/themes/finc/templates/RecordDriver/DefaultRecord/result-list.phtml
+++ b/themes/finc/templates/RecordDriver/DefaultRecord/result-list.phtml
@@ -18,7 +18,6 @@ if ($cover):
   <?php ob_start(); ?>
   <div class="media-<?=$thumbnailAlignment?> record-icon">
     <?=$this->record($this->driver)->getRecordIcon()?>
-    <?=$this->record($this->driver)->getRecordIconText()?>
   </div>
   <?php $thumbnail = ob_get_contents(); ?>
   <?php ob_end_clean(); ?>
diff --git a/themes/finc/templates/RecordDriver/SolrAI/result-list.phtml b/themes/finc/templates/RecordDriver/SolrAI/result-list.phtml
index 16be9bd855b..0b8dc222334 100644
--- a/themes/finc/templates/RecordDriver/SolrAI/result-list.phtml
+++ b/themes/finc/templates/RecordDriver/SolrAI/result-list.phtml
@@ -16,7 +16,6 @@ if ($cover):
   <?php ob_start(); ?>
   <div class="media-<?=$thumbnailAlignment?> record-icon">
     <?=$this->record($this->driver)->getRecordIcon()?>
-    <?=$this->record($this->driver)->getRecordIconText()?>
   </div>
   <?php $thumbnail = ob_get_contents(); ?>
   <?php ob_end_clean(); ?>
diff --git a/themes/finc/templates/record/cover.phtml b/themes/finc/templates/record/cover.phtml
index ccc96247aef..e557d2210f7 100644
--- a/themes/finc/templates/record/cover.phtml
+++ b/themes/finc/templates/record/cover.phtml
@@ -10,7 +10,7 @@
   <?php if ($this->link): ?>
   </a>
   <script>
-    registerCoverForModal($('#<?=$coverId?>'), '<?=$this->link?>');
+    registerCoverForModal($('#<?=$coverId?>'), '<?=$cover?>');
   </script>
   <?php endif; ?>
 <?php elseif ($cover === false): ?>
diff --git a/themes/finc/templates/record/coverReplacement.phtml b/themes/finc/templates/record/coverReplacement.phtml
index d74b4f1f8f5..d26b8d7bcb9 100644
--- a/themes/finc/templates/record/coverReplacement.phtml
+++ b/themes/finc/templates/record/coverReplacement.phtml
@@ -1 +1 @@
-<?= $this->record($this->driver)->getRecordIcon() ?>
\ No newline at end of file
+<?= $this->record($this->driver)->getRecordIcon('record-icon', false) ?>
\ No newline at end of file
-- 
GitLab