Skip to content

Commit

Permalink
Add support for pre-filtering new items.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Dec 15, 2023
1 parent 9f67265 commit 244830c
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 70 deletions.
3 changes: 3 additions & 0 deletions config/vufind/searches.ini
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,9 @@ ranges = 1,5,30
result_pages = 10
; The default sort to use (if different from the normally configured default).
;default_sort = year
; To include pre-filtering facet options, set to the name of a valid facet context
; (e.g. HomePage or Advanced).
;facet_context = Advanced
; This setting can be used to automatically apply filter queries to the New Item
; results. For example, you might want to filter out certain content types. You
; can add multiple filters by repeating the "filter[] =" line, or you can skip
Expand Down
33 changes: 24 additions & 9 deletions module/VuFind/src/VuFind/Controller/AbstractSolrSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

namespace VuFind\Controller;

use Laminas\View\Model\ViewModel;

use function in_array;

/**
Expand All @@ -47,20 +49,19 @@ class AbstractSolrSearch extends AbstractSearch
use Feature\RecordVersionsSearchTrait;

/**
* Handle an advanced search
* Set up facet details in the view (for use in advanced search and similar).
*
* @return mixed
* @param ViewModel $view View model to update
* @param string $list Name of facet list to retrieve
*
* @return void
*/
public function advancedAction()
protected function addFacetDetailsToView(ViewModel $view, $list = 'Advanced'): void
{
// Standard setup from base class:
$view = parent::advancedAction();

// Set up facet information:
$facets = $this->serviceLocator
->get(\VuFind\Search\FacetCache\PluginManager::class)
->get($this->searchClassId)
->getList('Advanced');
->getList($list);
$view->hierarchicalFacets
= $this->getHierarchicalFacets($view->options->getFacetsIni());
$view->hierarchicalFacetsSortOptions
Expand All @@ -69,10 +70,24 @@ public function advancedAction()
);
$view->facetList = $this->processAdvancedFacets(
$facets,
$view->saved,
$view->saved ?? false,
$view->hierarchicalFacets,
$view->hierarchicalFacetsSortOptions
);
}

/**
* Handle an advanced search
*
* @return mixed
*/
public function advancedAction()
{
// Standard setup from base class:
$view = parent::advancedAction();

// Set up facet information:
$this->addFacetDetailsToView($view);
$specialFacets = $this->parseSpecialFacetsSetting(
$view->options->getSpecialAdvancedFacets()
);
Expand Down
10 changes: 10 additions & 0 deletions module/VuFind/src/VuFind/Controller/Plugin/NewItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ public function getDefaultSort(): ?string
return $this->config->default_sort ?? null;
}

/**
* Get facet section setting (null for none).
*
* @return ?string
*/
public function getFacetContext(): ?string
{
return $this->config->facet_context ?? null;
}

/**
* Get fund list
*
Expand Down
9 changes: 8 additions & 1 deletion module/VuFind/src/VuFind/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,20 @@ public function newitemAction()
return $this->forwardTo('Search', 'NewItemResults');
}

return $this->createViewModel(
$view = $this->createViewModel(
[
'defaultSort' => $this->newItems()->getDefaultSort(),
'fundList' => $this->newItems()->getFundList(),
'ranges' => $this->newItems()->getRanges(),
]
);
if ($facetContext = $this->newItems()->getFacetContext()) {
$view->options = $this->serviceLocator
->get(\VuFind\Search\Options\PluginManager::class)
->get($this->searchClassId);
$this->addFacetDetailsToView($view, $facetContext);
}
return $view;
}

/**
Expand Down
60 changes: 60 additions & 0 deletions themes/bootstrap3/templates/search/advanced/solr-facets.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php if (!empty($this->facetList) || !empty($this->checkboxFacets)): ?>
<fieldset class="solr-facets">
<legend><?=$this->transEsc('Limit To')?></legend>
<?php if (!empty($this->checkboxFacets)): ?>
<?=$this->render('search/advanced/checkbox-filters.phtml')?>
<?php endif; ?>
<div class="solr-facet-container">
<?php foreach ($this->facetList as $field => $list): ?>
<div class="solr-adv-facet">
<label for="limit_<?=$this->escapeHtmlAttr(str_replace(' ', '', $field))?>"><?=$this->transEsc($list['label'])?>:</label>
<select class="form-control" id="limit_<?=$this->escapeHtmlAttr(str_replace(' ', '', $field))?>" name="filter[]" multiple="multiple" size="10">
<?php if (is_array($this->hierarchicalFacets) && in_array($field, $this->hierarchicalFacets)): ?>
<?php foreach ($list['list'] as $value): ?>
<?php $display = str_pad('', 4 * $value['level'] * 6, '&nbsp;', STR_PAD_LEFT) . $this->escapeHtml($value['displayText']); ?>
<option value="<?=$this->escapeHtmlAttr($this->operatorToFieldPrefix($value['operator']) . $field . ':"' . $value['value'] . '"')?>"<?=(isset($value['selected']) && $value['selected']) ? ' selected="selected"' : ''?>><?=$display?></option>
<?php endforeach; ?>
<?php else: ?>
<?php
// Sort the current facet list alphabetically and filter items to
// the top if they appear in the config; we'll use this data
// along with the foreach below to display facet options in the
// correct order.
$conf = $this->options->limitOrderOverride($field);
$conf = array_flip($conf);
$sorted = [];
$filtered = [];
$filterKeys = [];
foreach ($list['list'] as $i => $value) {
if (!empty($value['value'] && !empty($value['displayText']))) {
if (isset($conf[$value['value']])) {
$filtered[$i] = $value['displayText'];
$filterKeys[$value['displayText']] = $conf[$value['value']];
} else {
$sorted[$i] = $value['displayText'];
}
}
}
$this->sorter()->natsort($sorted);

// Order filtered items according to how they appear in the config.
uasort($filtered, function ($a, $b) use ($filterKeys) {
return $filterKeys[$a] <=> $filterKeys[$b];
});

// Combine filtered and sorted arrays so that the items in the config
// appear in order at the top and all other items appear afterwards
// sorted by natcasesort.
$sorted = $filtered + $sorted;
?>
<?php foreach ($sorted as $i => $display): ?>
<?php $value = $list['list'][$i]; ?>
<option value="<?=$this->escapeHtmlAttr(($value['operator'] == 'OR' ? '~' : '') . $field . ':"' . $value['value'] . '"')?>"<?=(isset($value['selected']) && $value['selected']) ? ' selected="selected"' : ''?>><?=$this->escapeHtml($display)?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</div>
<?php endforeach; ?>
</div>
</fieldset>
<?php endif; ?>
61 changes: 1 addition & 60 deletions themes/bootstrap3/templates/search/advanced/solr.phtml
Original file line number Diff line number Diff line change
@@ -1,63 +1,4 @@
<?php if (!empty($this->facetList) || !empty($this->checkboxFacets)): ?>
<fieldset class="solr-facets">
<legend><?=$this->transEsc('Limit To')?></legend>
<?php if (!empty($this->checkboxFacets)): ?>
<?=$this->render('search/advanced/checkbox-filters.phtml')?>
<?php endif; ?>
<div class="solr-facet-container">
<?php foreach ($this->facetList as $field => $list): ?>
<div class="solr-adv-facet">
<label for="limit_<?=$this->escapeHtmlAttr(str_replace(' ', '', $field))?>"><?=$this->transEsc($list['label'])?>:</label>
<select class="form-control" id="limit_<?=$this->escapeHtmlAttr(str_replace(' ', '', $field))?>" name="filter[]" multiple="multiple" size="10">
<?php if (is_array($this->hierarchicalFacets) && in_array($field, $this->hierarchicalFacets)): ?>
<?php foreach ($list['list'] as $value): ?>
<?php $display = str_pad('', 4 * $value['level'] * 6, '&nbsp;', STR_PAD_LEFT) . $this->escapeHtml($value['displayText']); ?>
<option value="<?=$this->escapeHtmlAttr($this->operatorToFieldPrefix($value['operator']) . $field . ':"' . $value['value'] . '"')?>"<?=(isset($value['selected']) && $value['selected']) ? ' selected="selected"' : ''?>><?=$display?></option>
<?php endforeach; ?>
<?php else: ?>
<?php
// Sort the current facet list alphabetically and filter items to
// the top if they appear in the config; we'll use this data
// along with the foreach below to display facet options in the
// correct order.
$conf = $this->options->limitOrderOverride($field);
$conf = array_flip($conf);
$sorted = [];
$filtered = [];
$filterKeys = [];
foreach ($list['list'] as $i => $value) {
if (!empty($value['value'] && !empty($value['displayText']))) {
if (isset($conf[$value['value']])) {
$filtered[$i] = $value['displayText'];
$filterKeys[$value['displayText']] = $conf[$value['value']];
} else {
$sorted[$i] = $value['displayText'];
}
}
}
$this->sorter()->natsort($sorted);

// Order filtered items according to how they appear in the config.
uasort($filtered, function ($a, $b) use ($filterKeys) {
return $filterKeys[$a] <=> $filterKeys[$b];
});

// Combine filtered and sorted arrays so that the items in the config
// appear in order at the top and all other items appear afterwards
// sorted by natcasesort.
$sorted = $filtered + $sorted;
?>
<?php foreach ($sorted as $i => $display): ?>
<?php $value = $list['list'][$i]; ?>
<option value="<?=$this->escapeHtmlAttr(($value['operator'] == 'OR' ? '~' : '') . $field . ':"' . $value['value'] . '"')?>"<?=(isset($value['selected']) && $value['selected']) ? ' selected="selected"' : ''?>><?=$this->escapeHtml($display)?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</div>
<?php endforeach; ?>
</div>
</fieldset>
<?php endif; ?>
<?=$this->render('search/advanced/solr-facets.phtml')?>
<?php if (isset($this->illustratedLimit)): ?>
<fieldset class="solr">
<legend><?=$this->transEsc('Illustrated')?>:</legend>
Expand Down
1 change: 1 addition & 0 deletions themes/bootstrap3/templates/search/newitem.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<?php endforeach; ?>
</div>
</div>
<?=$this->render('search/advanced/solr-facets.phtml')?>
<?php if (is_array($this->fundList) && !empty($this->fundList)): ?>
<div class="form-group">
<label class="control-label" for="newitem_department"><?=$this->transEsc('Department')?>:</label>
Expand Down

0 comments on commit 244830c

Please sign in to comment.