Skip to content
Permalink
Browse files

Simplify return format from AuthorityRecommend: headings only.

  • Loading branch information...
demiankatz committed May 9, 2018
1 parent f715f68 commit daaa41100e8341d240589fa026592a5ffbaca440
@@ -29,7 +29,6 @@
namespace VuFind\Recommend;
use VuFindSearch\Backend\Exception\RequestErrorException;
use Zend\Http\Request;
use Zend\StdLib\Parameters;
/**
@@ -145,6 +144,8 @@ public function setConfig($settings)
* request.
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function init($params, $request)
{
@@ -217,16 +218,7 @@ protected function addUseForHeadings()
// 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 @@ protected function addSeeAlsoReferences()
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 @@ public function process($results)
*/
public function getRecommendations()
{
return $this->recommendations;
return array_unique($this->recommendations);
}
/**
@@ -324,22 +314,4 @@ public function getResults()
{
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;
}
}
@@ -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; ?>

0 comments on commit daaa411

Please sign in to comment.
You can’t perform that action at this time.