Skip to content

Commit

Permalink
Combined: Display noresults recommendations in box or side
Browse files Browse the repository at this point in the history
  • Loading branch information
maccabeelevine committed Dec 12, 2023
1 parent e09f921 commit 76b6ce3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
17 changes: 14 additions & 3 deletions module/VuFind/src/VuFind/Controller/CombinedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,21 +339,32 @@ protected function adjustQueryForSettings($settings, $searchType = null)
// Override the search type:
$query->type = $searchType;

// Always leave noresults active (useful for 0-hit searches).
// Display or hide top based on include_recommendations setting.
// Display or hide side based on include_recommendations_side setting.
$recommendOverride = [];
$noRecommend = [];

// Display or hide top based on include_recommendations setting.
if (is_array($settings['include_recommendations'] ?? false)) {
$recommendOverride['top'] = $settings['include_recommendations'];
} else {
$noRecommend[] = 'top';
}

// Display or hide side based on include_recommendations_side setting.
if (is_array($settings['include_recommendations_side'] ?? false)) {
$recommendOverride['side'] = $settings['include_recommendations_side'];
} else {
$noRecommend[] = 'side';
}

// Display or hide no results recommendations, based on
// include_recommendations_noresults setting (to display them in the bento box) or
// include_recommendations_noresults_side setting (to display them in the sidebar).
if (is_array($settings['include_recommendations_noresults'] ?? false)) {
$recommendOverride['noresults'] = $settings['include_recommendations_noresults'];
} elseif (is_array($settings['include_recommendations_noresults_side'] ?? false)) {
$recommendOverride['noresults_side'] = $settings['include_recommendations_noresults_side'];
}

$query->recommendOverride = $recommendOverride;
$query->noRecommend = count($noRecommend) ? implode(',', $noRecommend) : false;
}
Expand Down
34 changes: 34 additions & 0 deletions themes/bootstrap3/templates/combined/results-list.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,40 @@
<?=$this->recommend($current)?>
<?php endif; ?>
<?php endforeach; ?>
<?php foreach ($results->getRecommendations('noresults_side') as $current): ?>
<?php if (!in_array($current, $top)):
$recommendationClass = str_replace('\\', '_', $current::class);
$recommendationJs = <<<JS
function addRecommendation_$recommendationClass() {
const recommendationContainer = document.querySelector('#search-sidebar .recommendation_container__$recommendationClass');
if (!recommendationContainer) {
console.error("Cannot find recommendation container for $recommendationClass");
return;
}
const recommendation = document.getElementById('recommendation__$recommendationClass');
if (!recommendation) {
console.error("Cannot find recommendation for $recommendationClass");
return;
}
recommendationContainer.append(recommendation);
recommendation.style.display = 'inherit';
}
if (document.readyState != 'loading') {
addRecommendation_$recommendationClass();
} else {
document.addEventListener('DOMContentLoaded', addRecommendation_$recommendationClass);
}
JS;
?>
<?=$this->inlineScript(\Laminas\View\Helper\HeadScript::SCRIPT, $recommendationJs, 'SET')?>
<div id="recommendation__<?=$recommendationClass?>" style="display: none;">
<?=$this->recommend($current)?>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
<?php
$viewType = in_array('list', array_keys($params->getViewList()))
Expand Down

0 comments on commit 76b6ce3

Please sign in to comment.