From de0620ebfad142cf94b62be020e30bb2fb1ba4ef Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Tue, 24 Sep 2013 12:02:04 -0400 Subject: [PATCH] Added ORed facet support to SideFacets. - Progress on VUFIND-177 - Thanks to Daniel Aineah and Tuan Nguyen for Solr development work --- config/vufind/Summon.ini | 4 ++++ config/vufind/facets.ini | 4 ++++ .../src/VuFind/Recommend/SideFacets.php | 20 ++++++++++++++++++- .../VuFind/src/VuFind/Search/Solr/Params.php | 19 +++++++----------- .../src/VuFind/Search/Summon/Params.php | 3 ++- .../src/VuFind/Search/UrlQueryHelper.php | 2 +- .../templates/Recommend/SideFacets.phtml | 2 +- .../Recommend/SideFacets-dialog.phtml | 2 +- 8 files changed, 39 insertions(+), 17 deletions(-) diff --git a/config/vufind/Summon.ini b/config/vufind/Summon.ini index 1020a27546e..f50757c17dd 100644 --- a/config/vufind/Summon.ini +++ b/config/vufind/Summon.ini @@ -111,6 +111,10 @@ top_cols = 3 ; all facets, use a comma-separated list to show for some of the facets, set ; to false or omit to disable "exclude" links ;exclude = * +; Should we OR together facets rather than ANDing them? Set to * for +; all facets, use a comma-separated list to apply to some of the facets, set +; to false or omit to disable ORed facets. +;orFacets = * ; These settings affect the way the facets are displayed [Facet_Settings] diff --git a/config/vufind/facets.ini b/config/vufind/facets.ini index f4118677fe7..94076851eb7 100644 --- a/config/vufind/facets.ini +++ b/config/vufind/facets.ini @@ -51,6 +51,10 @@ top_cols = 3 ; all facets, use a comma-separated list to show for some of the facets, set ; to false or omit to disable "exclude" links ;exclude = * +; Should we OR together facets rather than ANDing them? Set to * for +; all facets, use a comma-separated list to apply to some of the facets, set +; to false or omit to disable ORed facets. +;orFacets = * ; The author home screen has different facets [Author] diff --git a/module/VuFind/src/VuFind/Recommend/SideFacets.php b/module/VuFind/src/VuFind/Recommend/SideFacets.php index d325199bf09..aa6c005ecb9 100644 --- a/module/VuFind/src/VuFind/Recommend/SideFacets.php +++ b/module/VuFind/src/VuFind/Recommend/SideFacets.php @@ -62,6 +62,13 @@ class SideFacets implements RecommendInterface */ protected $excludableFacets = array(); + /** + * Facets that are "ORed" instead of "ANDed." + * + * @var array + */ + protected $orFacets = array(); + /** * Checkbox facet configuration * @@ -128,6 +135,17 @@ class SideFacets implements RecommendInterface } } + // Which facets are ORed? + if (isset($config->Results_Settings->orFacets)) { + if ($config->Results_Settings->orFacets === '*') { + $this->orFacets = array_keys($this->mainFacets); + } else { + $this->orFacets = array_map( + 'trim', explode(',', $config->Results_Settings->orFacets) + ); + } + } + // Get a list of fields that should be displayed as date ranges rather than // standard facet lists. if (isset($config->SpecialFacets->dateRange)) { @@ -165,7 +183,7 @@ class SideFacets implements RecommendInterface { // Turn on side facets in the search results: foreach ($this->mainFacets as $name => $desc) { - $params->addFacet($name, $desc); + $params->addFacet($name, $desc, in_array($name, $this->orFacets)); } foreach ($this->checkboxFacets as $name => $desc) { $params->addCheckboxFacet($name, $desc); diff --git a/module/VuFind/src/VuFind/Search/Solr/Params.php b/module/VuFind/src/VuFind/Search/Solr/Params.php index beb53f61d73..f0933dfb390 100644 --- a/module/VuFind/src/VuFind/Search/Solr/Params.php +++ b/module/VuFind/src/VuFind/Search/Solr/Params.php @@ -119,7 +119,8 @@ class Params extends \VuFind\Search\Base\Params } } foreach ($orFilters as $field => $parts) { - $filterQuery[] = $field . ':(' . implode(' OR ', $parts) . ')'; + $filterQuery[] = '{!tag=' . $field . '_filter}' . $field + . ':(' . implode(' OR ', $parts) . ')'; } return $filterQuery; } @@ -136,6 +137,9 @@ class Params extends \VuFind\Search\Base\Params if (!empty($this->facetConfig)) { $facetSet['limit'] = $this->facetLimit; foreach ($this->facetConfig as $facetField => $facetName) { + if ($this->getFacetOperator($facetField) == 'OR') { + $facetField = '{!ex=' . $facetField . '_filter}' . $facetField; + } $facetSet['field'][] = $facetField; } if ($this->facetOffset != null) { @@ -288,17 +292,8 @@ class Params extends \VuFind\Search\Base\Params */ public function initBasicFacets() { - $config = $this->getServiceLocator()->get('VuFind\Config')->get('facets'); - if (isset($config->ResultsTop)) { - foreach ($config->ResultsTop as $key => $value) { - $this->addFacet($key, $value); - } - } - if (isset($config->Results)) { - foreach ($config->Results as $key => $value) { - $this->addFacet($key, $value); - } - } + $this->initFacetList('ResultsTop', 'Results_Settings'); + $this->initFacetList('Results', 'Results_Settings'); } /** diff --git a/module/VuFind/src/VuFind/Search/Summon/Params.php b/module/VuFind/src/VuFind/Search/Summon/Params.php index 7a8b9969206..6f6392c6901 100644 --- a/module/VuFind/src/VuFind/Search/Summon/Params.php +++ b/module/VuFind/src/VuFind/Search/Summon/Params.php @@ -191,7 +191,8 @@ class Params extends \VuFind\Search\Base\Params // if not, override them with defaults. $parts = explode(',', $facet); $facetName = $parts[0]; - $facetMode = isset($parts[1]) ? $parts[1] : 'and'; + $defaultMode = ($this->getFacetOperator($facet) == 'OR') ? 'or' : 'and'; + $facetMode = isset($parts[1]) ? $parts[1] : $defaultMode; $facetPage = isset($parts[2]) ? $parts[2] : 1; $facetLimit = isset($parts[3]) ? $parts[3] : $defaultFacetLimit; $facetParams = "{$facetMode},{$facetPage},{$facetLimit}"; diff --git a/module/VuFind/src/VuFind/Search/UrlQueryHelper.php b/module/VuFind/src/VuFind/Search/UrlQueryHelper.php index 275bdb97cc5..dd119ec5174 100644 --- a/module/VuFind/src/VuFind/Search/UrlQueryHelper.php +++ b/module/VuFind/src/VuFind/Search/UrlQueryHelper.php @@ -242,7 +242,7 @@ class UrlQueryHelper public function addFacet($field, $value, $operator = 'AND') { // Facets are just a special case of filters: - $prefix = ($operator == 'NOT') ? '-' : ''; + $prefix = ($operator == 'NOT') ? '-' : ($operator == 'OR' ? '~' : ''); return $this->addFilter($prefix . $field . ':"' . $value . '"'); } diff --git a/themes/blueprint/templates/Recommend/SideFacets.phtml b/themes/blueprint/templates/Recommend/SideFacets.phtml index 5eeb7962fd1..2755db07470 100644 --- a/themes/blueprint/templates/Recommend/SideFacets.phtml +++ b/themes/blueprint/templates/Recommend/SideFacets.phtml @@ -66,7 +66,7 @@ <dd><?=$this->escapeHtml($thisFacet['displayText'])?> <img src="<?=$this->imageLink('silk/tick.png')?>" alt="Selected"/></dd> <? else: ?> <dd> - <a href="<?=$this->currentPath().$results->getUrlQuery()->addFacet($title, $thisFacet['value'])?>"><?=$this->escapeHtml($thisFacet['displayText'])?></a> (<?=$this->escapeHtml($thisFacet['count'])?>) + <a href="<?=$this->currentPath().$results->getUrlQuery()->addFacet($title, $thisFacet['value'], $thisFacet['operator'])?>"><?=$this->escapeHtml($thisFacet['displayText'])?></a> (<?=$this->escapeHtml($thisFacet['count'])?>) <? if ($allowExclude): ?> <a href="<?=$this->currentPath().$results->getUrlQuery()->addFacet($title, $thisFacet['value'], 'NOT')?>"><?=$this->transEsc('exclude_facet')?></a> <? endif; ?> diff --git a/themes/jquerymobile/templates/Recommend/SideFacets-dialog.phtml b/themes/jquerymobile/templates/Recommend/SideFacets-dialog.phtml index 9247bc0ee41..447c9458c26 100644 --- a/themes/jquerymobile/templates/Recommend/SideFacets-dialog.phtml +++ b/themes/jquerymobile/templates/Recommend/SideFacets-dialog.phtml @@ -18,7 +18,7 @@ <? if ($thisFacet['isApplied']): ?> <li data-icon="check" class="checked"><a href="" data-rel="back"><?=$this->escapeHtml($thisFacet['displayText'])?></a> <span class="ui-li-count"><?=$this->escapeHtml($thisFacet['count'])?></span></li> <? else: ?> - <li><a rel="external" href="<?=$this->currentPath().$results->getUrlQuery()->addFacet($title, $thisFacet['value'])?>"><?=$this->escapeHtml($thisFacet['displayText'])?></a> <span class="ui-li-count"><?=$this->escapeHtml($thisFacet['count'])?></span></li> + <li><a rel="external" href="<?=$this->currentPath().$results->getUrlQuery()->addFacet($title, $thisFacet['value'], $thisFacet['operator'])?>"><?=$this->escapeHtml($thisFacet['displayText'])?></a> <span class="ui-li-count"><?=$this->escapeHtml($thisFacet['count'])?></span></li> <? endif; ?> <? endforeach; ?> </ul> -- GitLab