diff --git a/module/VuFind/src/VuFind/Recommend/AuthorityRecommend.php b/module/VuFind/src/VuFind/Recommend/AuthorityRecommend.php index 53e7935a0b72090a1b6f3cdefb2bbe98a96d6c94..9928edea8552ec7cccaaec258c4e909ad4ade236 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 94e1dbcdee6c78251403a7036a10f8c28430bbc1..9a74b529767e1c665ab44e50e981cb507e25a696 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; ?>