From 437f017d9b766d2f9a03ab24ddbe42dcd578a623 Mon Sep 17 00:00:00 2001
From: Frank Morgner <morgnerf@ub.uni-leipzig.de>
Date: Fri, 5 Jun 2015 09:53:23 -0400
Subject: [PATCH] add sorting (by index) for single facets

---
 config/vufind/facets.ini                      | 10 ++++-
 .../VuFind/src/VuFind/Search/Solr/Params.php  | 41 ++++++++++++++++++-
 2 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/config/vufind/facets.ini b/config/vufind/facets.ini
index 980fde39a4c..42e38222625 100644
--- a/config/vufind/facets.ini
+++ b/config/vufind/facets.ini
@@ -85,6 +85,14 @@ top_cols = 3
 ; Do we want any facets to be collapsed by default?
 ;collapsedFacets = *
 
+; This can be used to sort specific facet fields alphabetically by index value
+; (which normally results in alphabetical order).
+; Please note: This sorts natively in the Solr index using untranslated values,
+; so if you are using facet translation, your values may not always display in
+; the expected order.
+;sorted_by_index[] = building;
+;sorted_by_index[] = institution;
+
 ; The author home screen has different facets
 [Author]
 topic_facet = "Related Subjects"
@@ -161,4 +169,4 @@ visual_facets = "callnumber-first,topic_facet"
 
 ; If you rename a facet field, you can map the old value to a new value in this
 ; section to ensure that legacy URLs continue to function.
-[LegacyFields]
\ No newline at end of file
+[LegacyFields]
diff --git a/module/VuFind/src/VuFind/Search/Solr/Params.php b/module/VuFind/src/VuFind/Search/Solr/Params.php
index b85c7950a25..a0c9212fc32 100644
--- a/module/VuFind/src/VuFind/Search/Solr/Params.php
+++ b/module/VuFind/src/VuFind/Search/Solr/Params.php
@@ -67,6 +67,13 @@ class Params extends \VuFind\Search\Base\Params
      */
     protected $facetSort = null;
 
+    /**
+     * Sorting order of single facet by index
+     *
+     * @var array
+     */
+    protected $facetSortedByIndex = null;
+
     /**
      * Fields for visual faceting
      *
@@ -83,7 +90,6 @@ class Params extends \VuFind\Search\Base\Params
     public function __construct($options, \VuFind\Config\PluginManager $configLoader)
     {
         parent::__construct($options, $configLoader);
-
         // Use basic facet limit by default, if set:
         $config = $configLoader->get('facets');
         if (isset($config->Results_Settings->facet_limit)
@@ -91,6 +97,13 @@ class Params extends \VuFind\Search\Base\Params
         ) {
             $this->setFacetLimit($config->Results_Settings->facet_limit);
         }
+        if (isset($config->Results_Settings->sorted_by_index)
+            && count($config->Results_Settings->sorted_by_index) > 0
+        ) {
+            $this->setIndexSortedFacets(
+                $config->Results_Settings->sorted_by_index->toArray()
+            );
+        }
     }
 
     /**
@@ -144,6 +157,7 @@ class Params extends \VuFind\Search\Base\Params
     {
         // Build a list of facets we want from the index
         $facetSet = [];
+
         if (!empty($this->facetConfig)) {
             $facetSet['limit'] = $this->facetLimit;
             foreach (array_keys($this->facetConfig) as $facetField) {
@@ -167,6 +181,9 @@ class Params extends \VuFind\Search\Base\Params
                 // so making this explicit ensures consistent behavior.
                 $facetSet['sort'] = ($this->facetLimit > 0) ? 'count' : 'index';
             }
+            if ($this->indexSortedFacets != null) {
+                $facetSet['indexSortedFacets'] = $this->indexSortedFacets;
+            }
         }
         return $facetSet;
     }
@@ -239,6 +256,18 @@ class Params extends \VuFind\Search\Base\Params
         $this->facetSort = $s;
     }
 
+    /**
+     * Set Index Facet Sorting
+     *
+     * @param array $s the facets sorted by index
+     *
+     * @return void
+     */
+    public function setIndexSortedFacets(array $s)
+    {
+        $this->indexSortedFacets = $s;
+    }
+
     /**
      * Initialize facet settings for the specified configuration sections.
      *
@@ -446,8 +475,16 @@ class Params extends \VuFind\Search\Base\Params
         $facets = $this->getFacetSettings();
         if (!empty($facets)) {
             $backendParams->add('facet', 'true');
+
+            if (isset($facets['indexSortedFacets'])) {
+                foreach ($facets['indexSortedFacets'] as $field) {
+                    $backendParams->add("f.{$field}.facet.sort", 'index');
+                }
+                unset($facets['indexSortedFacets']);
+            }
+
             foreach ($facets as $key => $value) {
-                $backendParams->add("facet.{$key}", $value);
+                    $backendParams->add("facet.{$key}", $value);
             }
             $backendParams->add('facet.mincount', 1);
         }
-- 
GitLab