Permalink
Browse files

Added mechanism for applying default filters.

- Resolves VUFIND-505.
  • Loading branch information...
1 parent 7f12e45 commit 4635fc56be193d47d6dc2e4a6ee8ad9866188361 @demiankatz demiankatz committed May 16, 2014
View
@@ -33,6 +33,11 @@ bulkSize = 20
; controls its default state: on (true) or off (false).
retain_filters_by_default = true
+; The filters listed below will be applied to all new searches by default. Omit
+; this setting to have no default filters applied. These differ from hidden
+; filters because they are visible in the UI and may be removed by the user.
+;default_filters[] = "rtype:Books"
+
; Primo has a fixed cap on how many results you can page through. Even though
; it may report more results than this number, you can't actually access results
; past the limit. This setting tells VuFind where to cut off its paging mechanism.
View
@@ -53,6 +53,12 @@ snippets = true
; controls its default state: on (true) or off (false).
retain_filters_by_default = true
+; The filters listed below will be applied to all new searches by default. Omit
+; this setting to have no default filters applied. These differ from hidden
+; filters because they are visible in the UI and may be removed by the user.
+;default_filters[] = "IsFullText:true"
+;default_filters[] = "excludeNewspapers:true"
+
; Summon has a fixed cap on how many results you can page through. Even though
; it may report more results than this number, you can't actually access results
; past the limit. This setting tells VuFind where to cut off its paging mechanism.
@@ -65,6 +65,12 @@ snippets = true
; controls its default state: on (true) or off (false).
retain_filters_by_default = true
+; The filters listed below will be applied to all new searches by default. Omit
+; this setting to have no default filters applied. These differ from hidden
+; filters because they are visible in the UI and may be removed by the user.
+;default_filters[] = "format:Book"
+;default_filters[] = "institution:MyInstitution"
+
[Cache]
; This controls whether the parsed searchspecs.yaml file will be stored to
; improve search performance; legal options are APC (use APC cache), File (store
@@ -54,6 +54,7 @@
protected $basicHandlers = array();
protected $specialAdvancedFacets = '';
protected $retainFiltersByDefault = true;
+ protected $defaultFilters = array();
// Available limit options
protected $defaultLimit = 20;
@@ -553,6 +554,16 @@ public function getLastView()
}
/**
+ * Get default filters to apply to an empty search.
+ *
+ * @return array
+ */
+ public function getDefaultFilters()
+ {
+ return $this->defaultFilters;
+ }
+
+ /**
* Should filter settings be retained across searches by default?
*
* @return bool
@@ -88,6 +88,13 @@ class Params implements ServiceLocatorAwareInterface
protected $serviceLocator;
/**
+ * Are default filters applied?
+ *
+ * @var bool
+ */
+ protected $defaultsApplied = false;
+
+ /**
* Constructor
*
* @param \VuFind\Search\Base\Options $options Options to use
@@ -1291,6 +1298,20 @@ protected function initFilters($request)
}
}
+ // If we don't have the special flag indicating that defaults have
+ // been applied, and if we do have defaults, apply them:
+ if ($request->get('dfApplied')) {
+ $this->defaultsApplied = true;
+ } else {
+ $defaults = $this->getOptions()->getDefaultFilters();
+ if (!empty($defaults)) {
+ foreach ($defaults as $current) {
+ $this->addFilter($current);
+ }
+ $this->defaultsApplied = true;
+ }
+ }
+
// Handle range filters:
$this->initRangeFilters($request);
}
@@ -1387,6 +1408,13 @@ public function deminify($minified)
$this->filterList = $minified->f;
$this->searchType = $minified->ty;
+ // Deminified searches will always have defaults already applied;
+ // we don't want to accidentally manipulate them further.
+ $defaults = $this->getOptions()->getDefaultFilters();
+ if (!empty($defaults)) {
+ $this->defaultsApplied = true;
+ }
+
// Search terms, we need to expand keys
$this->query = QueryAdapter::deminify($minified->t);
}
@@ -1580,4 +1608,14 @@ protected function initFacetList($facetList, $facetSettings, $cfgFile = 'facets'
}
return true;
}
+
+ /**
+ * Are default filters applied?
+ *
+ * @return bool
+ */
+ public function hasDefaultsApplied()
+ {
+ return $this->defaultsApplied;
+ }
}
@@ -84,6 +84,10 @@ public function __construct(\VuFind\Config\PluginManager $configLoader)
$this->retainFiltersByDefault
= $searchSettings->General->retain_filters_by_default;
}
+ if (isset($searchSettings->General->default_filters)) {
+ $this->defaultFilters = $searchSettings->General->default_filters
+ ->toArray();
+ }
// Result limit:
if (isset($searchSettings->General->result_limit)) {
@@ -84,6 +84,10 @@ public function __construct(\VuFind\Config\PluginManager $configLoader)
$this->retainFiltersByDefault
= $searchSettings->General->retain_filters_by_default;
}
+ if (isset($searchSettings->General->default_filters)) {
+ $this->defaultFilters = $searchSettings->General->default_filters
+ ->toArray();
+ }
if (isset($searchSettings->Basic_Searches)) {
foreach ($searchSettings->Basic_Searches as $key => $value) {
$this->basicHandlers[$key] = $value;
@@ -106,6 +106,10 @@ public function __construct(\VuFind\Config\PluginManager $configLoader)
$this->retainFiltersByDefault
= $searchSettings->General->retain_filters_by_default;
}
+ if (isset($searchSettings->General->default_filters)) {
+ $this->defaultFilters = $searchSettings->General->default_filters
+ ->toArray();
+ }
if (isset($searchSettings->General->result_limit)) {
$this->resultLimit = $searchSettings->General->result_limit;
}
@@ -222,6 +222,9 @@ protected function getParamArray()
$params['shard'] = $shards;
}
}
+ if ($this->params->hasDefaultsApplied()) {
+ $params['dfApplied'] = 1;
+ }
return $params;
}
@@ -45,6 +45,8 @@ function filterAll(element, formId) {
}
$("#" + formId + " :input[type='checkbox'][name='filter[]']")
.attr('checked', element.checked);
+ $("#" + formId + " :input[type='checkbox'][name='dfApplied']")
+ .attr('checked', element.checked);
}
function extractParams(str) {
@@ -18,6 +18,7 @@
'searchClassId' => $this->params->getsearchClassId(),
'checkboxFilters' => $this->params->getCheckboxFacets(),
'filterList' => $this->params->getFilters(),
+ 'hasDefaultsApplied' => $this->params->hasDefaultsApplied(),
'selectedShards' => $this->params->getSelectedShards()
)
);
@@ -12,9 +12,10 @@
if (isset($this->saved) && is_object($this->saved)) {
$searchDetails = $this->saved->getParams()->getQuery();
$groups = $searchDetails->getQueries();
+ $hasDefaultsApplied = $this->saved->getParams()->hasDefaultsApplied();
$searchFilters = $this->saved->getParams()->getFilterList();
} else {
- $searchDetails = $searchFilters = $groups = false;
+ $hasDefaultsApplied = $searchDetails = $searchFilters = $groups = false;
}
// Set up Javascript:
@@ -135,6 +136,9 @@
</div>
<div class="<?=$this->layoutClass('sidebar')?>">
+ <? if ($hasDefaultsApplied): ?>
+ <input type="hidden" name="dfApplied" value="1" />
+ <? endif ?>
<? if (!empty($searchFilters)): ?>
<div class="filterList">
<h3><?=$this->transEsc("adv_search_filters")?><br/><span>(<?=$this->transEsc("adv_search_select_all")?> <input type="checkbox" checked="checked" onclick="filterAll(this, 'advSearchForm');" />)</span></h3>
@@ -18,6 +18,7 @@
'searchClassId' => $this->params->getsearchClassId(),
'checkboxFilters' => $this->params->getCheckboxFacets(),
'filterList' => $this->params->getFilters(),
+ 'hasDefaultsApplied' => $this->params->hasDefaultsApplied(),
'selectedShards' => $this->params->getSelectedShards()
)
);
@@ -77,7 +77,7 @@
isset($this->checkboxFilters) && is_array($this->checkboxFilters) ? $this->checkboxFilters : array()
);
?>
- <? if (!empty($filterDetails)): ?>
+ <? if ($hasDefaultsApplied || !empty($filterDetails)): ?>
<? $defaultFilterState = $options->getRetainFilterSetting() ? ' checked="checked"' : ''; ?>
<div class="keepFilters">
<input type="checkbox"<?=$defaultFilterState?> id="searchFormKeepFilters"/> <label for="searchFormKeepFilters"><?=$this->transEsc("basic_search_keep_filters")?></label>
@@ -86,6 +86,11 @@
<input id="<?=$this->escapeHtml($current['id'])?>" type="checkbox"<?=$defaultFilterState?> name="filter[]" value="<?=$this->escapeHtml($current['value'])?>" />
<label for="<?=$this->escapeHtml($current['id'])?>"><?=$this->escapeHtml($current['value'])?></label>
<? endforeach; ?>
+ <? if ($hasDefaultsApplied): ?>
+ <!-- this is a hidden element that flags whether or not default filters have been applied;
+ it is intentionally unlabeled, as users are not meant to manipulate it directly. -->
+ <input id="dfApplied" type="checkbox" name="dfApplied" value="1" />
+ <? endif; ?>
</div>
</div>
<? endif; ?>
@@ -18,6 +18,7 @@
'searchClassId' => $this->params->getsearchClassId(),
'checkboxFilters' => $this->params->getCheckboxFacets(),
'filterList' => $this->params->getFilters(),
+ 'hasDefaultsApplied' => $this->params->hasDefaultsApplied(),
'selectedShards' => $this->params->getSelectedShards()
)
);
@@ -13,9 +13,10 @@
if (isset($this->saved) && is_object($this->saved)) {
$searchDetails = $this->saved->getParams()->getQuery();
$groups = $searchDetails->getQueries();
+ $hasDefaultsApplied = $this->saved->getParams()->hasDefaultsApplied();
$searchFilters = $this->saved->getParams()->getFilterList();
} else {
- $searchDetails = $searchFilters = $groups = false;
+ $hasDefaultsApplied = $searchDetails = $searchFilters = $groups = false;
}
// Set up Javascript:
@@ -58,6 +59,9 @@
</div>
<div class="<?=$this->layoutClass('sidebar')?>">
+ <? if ($hasDefaultsApplied): ?>
+ <input type="hidden" name="dfApplied" value="1" />
+ <? endif ?>
<? if (!empty($searchFilters)): ?>
<div class="filterList">
<h4><?=$this->transEsc("adv_search_filters")?></h4>
@@ -18,6 +18,7 @@
'searchClassId' => $this->params->getsearchClassId(),
'checkboxFilters' => $this->params->getCheckboxFacets(),
'filterList' => $this->params->getFilters(),
+ 'hasDefaultsApplied' => $this->params->hasDefaultsApplied(),
'selectedShards' => $this->params->getSelectedShards()
)
);
@@ -69,17 +69,22 @@
isset($this->checkboxFilters) && is_array($this->checkboxFilters) ? $this->checkboxFilters : array()
);
?>
- <? if (!empty($filterDetails)): ?>
+ <? if ($hasDefaultsApplied || !empty($filterDetails)): ?>
<? $defaultFilterState = $options->getRetainFilterSetting() ? ' checked="checked"' : ''; ?>
<label class="checkbox">
<input onChange="$('.applied-filter').click()" type="checkbox"<?=$defaultFilterState?> id="searchFormKeepFilters"/>
<?=$this->transEsc("basic_search_keep_filters")?>
</label>
<div class="hidden">
<? foreach ($filterDetails as $current): ?>
- <input id="<?=$this->escapeHtml($current['id'])?>" type="checkbox"<?=$defaultFilterState?> name="filter[]" value="<?=$this->escapeHtml($current['value'])?>" />
+ <input class="applied-filter" id="<?=$this->escapeHtml($current['id'])?>" type="checkbox"<?=$defaultFilterState?> name="filter[]" value="<?=$this->escapeHtml($current['value'])?>" />
<label for="<?=$this->escapeHtml($current['id'])?>"><?=$this->escapeHtml($current['value'])?></label>
<? endforeach; ?>
+ <? if ($hasDefaultsApplied): ?>
+ <!-- this is a hidden element that flags whether or not default filters have been applied;
+ it is intentionally unlabeled, as users are not meant to manipulate it directly. -->
+ <input class="applied-filter" id="dfApplied" type="checkbox" name="dfApplied" value="1" />
+ <? endif; ?>
</div>
<? endif; ?>
<?

0 comments on commit 4635fc5

Please sign in to comment.