From 8c81cd742bf955506776eb2211e9e1a73be15780 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 25 Jun 2020 09:41:34 -0400
Subject: [PATCH] Add configuration to filter EDS item display. (#1654)

---
 config/vufind/EDS.ini                         | 21 +++++++
 module/VuFind/src/VuFind/RecordDriver/EDS.php | 59 ++++++++++++++++---
 .../templates/RecordDriver/EDS/core.phtml     |  2 +-
 .../RecordDriver/EDS/result-list.phtml        |  2 +-
 4 files changed, 73 insertions(+), 11 deletions(-)

diff --git a/config/vufind/EDS.ini b/config/vufind/EDS.ini
index bfd8f9bbad5..5eab9bf26bd 100644
--- a/config/vufind/EDS.ini
+++ b/config/vufind/EDS.ini
@@ -267,3 +267,24 @@ auto_submit = true
 ;AllFields = Eds:rawqueries
 ;TI = Eds:holdings
 AU = None
+
+; This section can be used to filter out unwanted items from display in any context.
+[ItemGlobalFilter]
+; This repeatable setting can be used to filter out values based on their labels:
+;excludeLabel[] = 'Availability'
+; This repeatable setting can be used to filter out values using group codes:
+;excludeGroup[] = 'URL'
+
+; Similar to [ItemGlobalFilter] but applied only for record view.
+[ItemCoreFilter]
+; This repeatable setting can be used to filter out values based on their labels:
+;excludeLabel[] = 'Availability'
+; This repeatable setting can be used to filter out values using group codes:
+;excludeGroup[] = 'URL'
+
+; Similar to [ItemGlobalFilter] but applied only for search result view.
+[ItemResultListFilter]
+; This repeatable setting can be used to filter out values based on their labels:
+;excludeLabel[] = 'Availability'
+; This repeatable setting can be used to filter out values using group codes:
+;excludeGroup[] = 'URL'
diff --git a/module/VuFind/src/VuFind/RecordDriver/EDS.php b/module/VuFind/src/VuFind/RecordDriver/EDS.php
index 5f4c020a961..22218822caa 100644
--- a/module/VuFind/src/VuFind/RecordDriver/EDS.php
+++ b/module/VuFind/src/VuFind/RecordDriver/EDS.php
@@ -193,22 +193,63 @@ class EDS extends DefaultRecord
             && ('1' == $this->fields['FullText']['Text']['Availability']);
     }
 
+    /**
+     * Support method for getItems, used to apply filters.
+     *
+     * @param array  $item    Item to check
+     * @param string $context The context in which items are being retrieved
+     * (used for context-sensitive filtering)
+     *
+     * @return bool
+     */
+    protected function itemIsExcluded($item, $context)
+    {
+        // Create a list of config sections to check, based on context:
+        $sections = ['ItemGlobalFilter'];
+        switch ($context) {
+        case 'result-list':
+            $sections[] = 'ItemResultListFilter';
+            break;
+        case 'core':
+            $sections[] = 'ItemCoreFilter';
+            break;
+        }
+        // Check to see if anything is filtered:
+        foreach ($sections as $section) {
+            $currentConfig = isset($this->recordConfig->$section)
+                ? $this->recordConfig->$section->toArray() : [];
+            $badLabels = (array)($currentConfig['excludeLabel'] ?? []);
+            $badGroups = (array)($currentConfig['excludeGroup'] ?? []);
+            if (in_array($item['Label'], $badLabels)
+                || in_array($item['Group'], $badGroups)
+            ) {
+                return true;
+            }
+        }
+        // If we got this far, no filter was applied:
+        return false;
+    }
+
     /**
      * Get the items of the record.
      *
+     * @param string $context The context in which items are being retrieved
+     * (used for context-sensitive filtering)
+     *
      * @return array
      */
-    public function getItems()
+    public function getItems($context = null)
     {
         $items = [];
-        if (isset($this->fields['Items']) && !empty($this->fields['Items'])) {
-            foreach ($this->fields['Items'] as $item) {
-                $items[] = [
-                    'Label' => $item['Label'] ?? '',
-                    'Group' => $item['Group'] ?? '',
-                    'Data'  => isset($item['Data'])
-                        ? $this->toHTML($item['Data'], $item['Group']) : ''
-                ];
+        foreach ($this->fields['Items'] ?? [] as $item) {
+            $nextItem = [
+                'Label' => $item['Label'] ?? '',
+                'Group' => $item['Group'] ?? '',
+                'Data'  => isset($item['Data'])
+                    ? $this->toHTML($item['Data'], $item['Group']) : ''
+            ];
+            if (!$this->itemIsExcluded($nextItem, $context)) {
+                $items[] = $nextItem;
             }
         }
         return $items;
diff --git a/themes/bootstrap3/templates/RecordDriver/EDS/core.phtml b/themes/bootstrap3/templates/RecordDriver/EDS/core.phtml
index 7e03a4e0f95..2149bdb45bd 100644
--- a/themes/bootstrap3/templates/RecordDriver/EDS/core.phtml
+++ b/themes/bootstrap3/templates/RecordDriver/EDS/core.phtml
@@ -1,6 +1,6 @@
 <?php $this->headLink()->appendStylesheet('EDS.css'); ?>
 <?php
-    $items = $this->driver->getItems();
+    $items = $this->driver->getItems('core');
     $dbLabel = $this->driver->getDbLabel();
     $thumb = $this->driver->getThumbnail('medium');
     $pubType = $this->driver->getPubType();
diff --git a/themes/bootstrap3/templates/RecordDriver/EDS/result-list.phtml b/themes/bootstrap3/templates/RecordDriver/EDS/result-list.phtml
index dca553c6de9..f6cacbc0f3a 100644
--- a/themes/bootstrap3/templates/RecordDriver/EDS/result-list.phtml
+++ b/themes/bootstrap3/templates/RecordDriver/EDS/result-list.phtml
@@ -28,7 +28,7 @@
   <?php endif; ?>
   <div class="media-body">
     <div class="result-body">
-      <?php $items = $this->driver->getItems();
+      <?php $items = $this->driver->getItems('result-list');
         if (isset($items) && !empty($items)):
           foreach ($items as $item):
             if (!empty($item)): ?>
-- 
GitLab