Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display side recommendations with combined search #3135

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/vufind/combined.ini
Expand Up @@ -28,6 +28,11 @@
; specified recommendations will be loaded into the top
; area of the column. If set to false, recommendations
; will be suppressed (default).
; include_recommendations_side = If set to an array of recommendation settings
; (as per searches.ini), the specified recommendations will be loaded into
; the side column. If set to false, side recommendations will be suppressed
; (default). Adding the same recommendation module to multiple sections is
; unsupported.
; filter = One or more filters to apply to search results displayed in the column.
; Use multiple "filter[] = ..." lines if multiple filters are needed.
; hiddenFilter = One or more hidden filters to apply to search results displayed in
Expand Down
37 changes: 27 additions & 10 deletions module/VuFind/src/VuFind/Controller/CombinedController.php
Expand Up @@ -225,6 +225,17 @@ public function resultsAction()
$settings = $this->serviceLocator->get(\VuFind\Config\PluginManager::class)
->get('config');

// Identify if any modules use include_recommendations_side.
$columnSideRecommendations = [];
$recommendationManager = $this->serviceLocator->get(\VuFind\Recommend\PluginManager::class);
foreach ($config as $subconfig) {
foreach ($subconfig['include_recommendations_side'] ?? [] as $recommendation) {
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
$recommendationModuleName = strtok($recommendation, ':');
$recommendationModule = $recommendationManager->get($recommendationModuleName);
$columnSideRecommendations[] = str_replace('\\', '_', $recommendationModule::class);
}
}

// Build view model:
return $this->createViewModel(
[
Expand All @@ -237,6 +248,7 @@ public function resultsAction()
'supportsCart' => $supportsCart,
'supportsCartOptions' => $supportsCartOptions,
'showBulkOptions' => $settings->Site->showBulkOptions ?? false,
'columnSideRecommendations' => $columnSideRecommendations,
]
);
}
Expand Down Expand Up @@ -325,18 +337,23 @@ protected function adjustQueryForSettings($settings, $searchType = null)
// Override the search type:
$query->type = $searchType;

// Always leave noresults active (useful for 0-hit searches) and
// side inactive (no room to display) but display or hide top based
// on include_recommendations setting.
if ($settings['include_recommendations'] ?? false) {
$query->noRecommend = 'side';
if (is_array($settings['include_recommendations'])) {
$query->recommendOverride
= ['top' => $settings['include_recommendations']];
}
// 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 = [];
if (is_array($settings['include_recommendations'] ?? false)) {
$recommendOverride['top'] = $settings['include_recommendations'];
} else {
$noRecommend[] = 'top';
}
if (is_array($settings['include_recommendations_side'] ?? false)) {
$recommendOverride['side'] = $settings['include_recommendations_side'];
} else {
$query->noRecommend = 'top,side';
$noRecommend[] = 'side';
}
$query->recommendOverride = $recommendOverride;
$query->noRecommend = count($noRecommend) ? implode(',', $noRecommend) : false;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions themes/bootstrap3/templates/combined/results-list.phtml
Expand Up @@ -68,6 +68,28 @@
<?php foreach (($top = $results->getRecommendations('top')) as $current): ?>
<?=$this->recommend($current)?>
<?php endforeach; ?>
<?php foreach (($side = $results->getRecommendations('side')) as $current):
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
$recommendationClass = str_replace('\\', '_', $current::class);
$recommendationJs = <<<JS
function addRecommendation_$recommendationClass() {
const recommendationContainer = document.querySelector('#search-sidebar .recommendation_container__$recommendationClass');
const recommendation = document.getElementById('recommendation__$recommendationClass');
recommendationContainer.append(recommendation);
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
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 endforeach; ?>
<?=
$this->context()->renderInContext(
'search/controls/showing.phtml',
Expand Down
18 changes: 17 additions & 1 deletion themes/bootstrap3/templates/combined/results.phtml
Expand Up @@ -73,7 +73,23 @@
'showBulkOptions' => $this->showBulkOptions,
];
?>
<?=$this->context($this)->renderInContext('combined/stack-' . $placement . '.phtml', $viewParams); ?>

<?php if (!empty($columnSideRecommendations)): ?>
<div class="<?=$this->layoutClass('mainbody')?>">
<?=$this->context($this)->renderInContext('combined/stack-' . $placement . '.phtml', $viewParams); ?>
</div>

<div class="<?=$this->layoutClass('sidebar')?>" id="search-sidebar">
<?php foreach ($columnSideRecommendations as $columnSideRecommendation): ?>
<div class="recommendation_container__<?=$columnSideRecommendation?>">
<?php // Content is added via JS in results-list.phtml. ?>
</div>
<?php endforeach; ?>
</div>
<?php else: ?>
<?=$this->context($this)->renderInContext('combined/stack-' . $placement . '.phtml', $viewParams); ?>
<?php endif; ?>

<?php $recs = $combinedResults->getRecommendations('bottom'); ?>
<?php if (!empty($recs)): ?>
<div>
Expand Down