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
Branches
Tags
No related merge requests found
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
namespace VuFind\Recommend; namespace VuFind\Recommend;
use VuFindSearch\Backend\Exception\RequestErrorException; use VuFindSearch\Backend\Exception\RequestErrorException;
use Zend\Http\Request;
use Zend\StdLib\Parameters; use Zend\StdLib\Parameters;
/** /**
...@@ -145,6 +144,8 @@ class AuthorityRecommend implements RecommendInterface ...@@ -145,6 +144,8 @@ class AuthorityRecommend implements RecommendInterface
* request. * request.
* *
* @return void * @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/ */
public function init($params, $request) public function init($params, $request)
{ {
...@@ -217,16 +218,7 @@ class AuthorityRecommend implements RecommendInterface ...@@ -217,16 +218,7 @@ class AuthorityRecommend implements RecommendInterface
// loop through records and assign id and headings to separate arrays defined // loop through records and assign id and headings to separate arrays defined
// above // above
foreach ($this->performSearch($params) as $result) { foreach ($this->performSearch($params) as $result) {
// Extract relevant details: $this->recommendations[] = $result->getBreadcrumb();
$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);
}
} }
} }
...@@ -248,10 +240,8 @@ class AuthorityRecommend implements RecommendInterface ...@@ -248,10 +240,8 @@ class AuthorityRecommend implements RecommendInterface
foreach ($this->performSearch($params) as $result) { foreach ($this->performSearch($params) as $result) {
foreach ($result->getSeeAlso() as $seeAlso) { foreach ($result->getSeeAlso() as $seeAlso) {
// check for duplicates before adding record to recordSet // check for duplicates before adding record to recordSet
if (!$this->fuzzyCompare($seeAlso, $this->lookfor) if (!$this->fuzzyCompare($seeAlso, $this->lookfor)) {
&& !$this->inArrayR($seeAlso, $this->recommendations) $this->recommendations[] = $seeAlso;
) {
array_push($this->recommendations, ['heading' => $seeAlso]);
} }
} }
} }
...@@ -312,7 +302,7 @@ class AuthorityRecommend implements RecommendInterface ...@@ -312,7 +302,7 @@ class AuthorityRecommend implements RecommendInterface
*/ */
public function getRecommendations() public function getRecommendations()
{ {
return $this->recommendations; return array_unique($this->recommendations);
} }
/** /**
...@@ -324,22 +314,4 @@ class AuthorityRecommend implements RecommendInterface ...@@ -324,22 +314,4 @@ class AuthorityRecommend implements RecommendInterface
{ {
return $this->results; 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 <?php
$data = $this->recommend->getRecommendations(); $data = $this->recommend->getRecommendations();
$results = $this->recommend->getResults(); $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 class="authoritybox">
<div><strong><?=$this->transEsc('See also')?>:</strong></div> <div><strong><?=$this->transEsc('See also')?>:</strong></div>
<div> <div><?=$content?></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> </div>
<?php endif; ?> <?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