Skip to content
Snippets Groups Projects
Commit 07b825f7 authored by Demian Katz's avatar Demian Katz
Browse files

Simplify return format from AuthorityRecommend: headings only.

parent 2da565cf
No related merge requests found
......@@ -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;
}
}
<?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; ?>
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment