From 07b825f7152cd6bc74c84d8464bb98a026079436 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 9 May 2018 12:32:10 -0400
Subject: [PATCH] Simplify return format from AuthorityRecommend: headings
 only.

---
 .../VuFind/Recommend/AuthorityRecommend.php   | 40 +++----------------
 .../Recommend/AuthorityRecommend.phtml        | 23 ++++++-----
 2 files changed, 18 insertions(+), 45 deletions(-)

diff --git a/module/VuFind/src/VuFind/Recommend/AuthorityRecommend.php b/module/VuFind/src/VuFind/Recommend/AuthorityRecommend.php
index 53e7935a0b7..9928edea855 100644
--- a/module/VuFind/src/VuFind/Recommend/AuthorityRecommend.php
+++ b/module/VuFind/src/VuFind/Recommend/AuthorityRecommend.php
@@ -29,7 +29,6 @@
 namespace VuFind\Recommend;
 
 use VuFindSearch\Backend\Exception\RequestErrorException;
-use Zend\Http\Request;
 use Zend\StdLib\Parameters;
 
 /**
@@ -145,6 +144,8 @@ class AuthorityRecommend implements RecommendInterface
      * request.
      *
      * @return void
+     *
+     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
      */
     public function init($params, $request)
     {
@@ -217,16 +218,7 @@ class AuthorityRecommend implements RecommendInterface
         // loop through records and assign id and headings to separate arrays defined
         // above
         foreach ($this->performSearch($params) as $result) {
-            // Extract relevant details:
-            $recordArray = [
-                'id' => $result->getUniqueID(),
-                'heading' => $result->getBreadcrumb()
-            ];
-
-            // check for duplicates before adding record to recordSet
-            if (!$this->inArrayR($recordArray['heading'], $this->recommendations)) {
-                array_push($this->recommendations, $recordArray);
-            }
+            $this->recommendations[] = $result->getBreadcrumb();
         }
     }
 
@@ -248,10 +240,8 @@ class AuthorityRecommend implements RecommendInterface
         foreach ($this->performSearch($params) as $result) {
             foreach ($result->getSeeAlso() as $seeAlso) {
                 // check for duplicates before adding record to recordSet
-                if (!$this->fuzzyCompare($seeAlso, $this->lookfor)
-                    && !$this->inArrayR($seeAlso, $this->recommendations)
-                ) {
-                    array_push($this->recommendations, ['heading' => $seeAlso]);
+                if (!$this->fuzzyCompare($seeAlso, $this->lookfor)) {
+                    $this->recommendations[] = $seeAlso;
                 }
             }
         }
@@ -312,7 +302,7 @@ class AuthorityRecommend implements RecommendInterface
      */
     public function getRecommendations()
     {
-        return $this->recommendations;
+        return array_unique($this->recommendations);
     }
 
     /**
@@ -324,22 +314,4 @@ class AuthorityRecommend implements RecommendInterface
     {
         return $this->results;
     }
-
-    /**
-     * Helper function to do recursive searches of multi-dimensional arrays.
-     *
-     * @param string $needle   Search term
-     * @param array  $haystack Multi-dimensional array
-     *
-     * @return bool
-     */
-    protected function inArrayR($needle, $haystack)
-    {
-        foreach ($haystack as $v) {
-            if ($needle == $v || (is_array($v) && $this->inArrayR($needle, $v))) {
-                return true;
-            }
-        }
-        return false;
-    }
 }
diff --git a/themes/bootstrap3/templates/Recommend/AuthorityRecommend.phtml b/themes/bootstrap3/templates/Recommend/AuthorityRecommend.phtml
index 94e1dbcdee6..9a74b529767 100644
--- a/themes/bootstrap3/templates/Recommend/AuthorityRecommend.phtml
+++ b/themes/bootstrap3/templates/Recommend/AuthorityRecommend.phtml
@@ -1,19 +1,20 @@
 <?php
     $data = $this->recommend->getRecommendations();
     $results = $this->recommend->getResults();
+    $displayQuery = $results->getParams()->getDisplayQuery();
+    $that = $this;
+    $callback = function ($heading) use ($results, $displayQuery, $that) {
+        // Generate a new search URL that replaces the user's current term
+        // with the authority term:
+        $url = $that->url($results->getOptions()->getSearchAction())
+            . $results->getUrlQuery()->replaceTerm($displayQuery, $heading);
+        return "<a href=\"$url\">" . $that->escapeHtml($heading) . '</a>';
+    };
+    $content = implode(', ', array_map($callback, $data));
 ?>
-<?php if (is_array($data) && !empty($data)): ?>
+<?php if (!empty($content)): ?>
   <div class="authoritybox">
     <div><strong><?=$this->transEsc('See also')?>:</strong></div>
-    <div>
-      <?php for ($i = 0; $i < count($data); $i++): ?>
-        <?php
-            // Generate a new search URL that replaces the user's current term with the authority term:
-            $url = $this->url($results->getOptions()->getSearchAction())
-                . $results->getUrlQuery()->replaceTerm($results->getParams()->getDisplayQuery(), $data[$i]['heading']);
-        ?>
-        <a href="<?=$url?>"><?=$this->escapeHtml($data[$i]['heading'])?></a><?php if ($i != count($data) - 1): ?>, <?php endif; ?>
-      <?php endfor; ?>
-    </div>
+    <div><?=$content?></div>
   </div>
 <?php endif; ?>
-- 
GitLab