From b0e977abea04602f6afc400ee20a36731af8d9b4 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Fri, 6 Aug 2021 18:50:35 +0000
Subject: [PATCH] Bug 28826: Add FacetOrder system preference

This bug adds a system preference to control ordering of facets and
adds the control to both Zebra and Elasticsearch

To test:
1 - Have a koha that can use both Zebra and ES
2 - Set 'displayFacetCount' to true
3 - Search in ES and Zebra
4 - Note facets in Zebra sorted alphabetically, ES by usage
5 - Apply patch, updatedatabase
6 - Search in ES and Zebra, facets are alphabetically sorted in both
7 - Find new syspref FacetOrder and set to 'by usage'
8 - Search in both engines, facets sorted by usage
---
 C4/Search.pm                                             | 5 ++++-
 Koha/SearchEngine/Elasticsearch/Search.pm                | 5 +++++
 .../atomicupdate/bug_28826_add_FacetSort_syspref.perl    | 9 +++++++++
 installer/data/mysql/mandatory/sysprefs.sql              | 1 +
 .../prog/en/modules/admin/preferences/searching.pref     | 9 +++++++++
 5 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 installer/data/mysql/atomicupdate/bug_28826_add_FacetSort_syspref.perl

diff --git a/C4/Search.pm b/C4/Search.pm
index 1ca987add2..503ce6ee09 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -586,7 +586,10 @@ sub getRecords {
     # This sorts the facets into alphabetical order
     if (@facets_loop) {
         foreach my $f (@facets_loop) {
-            $f->{facets} = [ sort { uc($a->{facet_label_value}) cmp uc($b->{facet_label_value}) } @{ $f->{facets} } ];
+            if( C4::Context->preference('FacetOrder') eq 'Alphabetical' ){
+                $f->{facets} =
+                    [ sort { uc($a->{facet_label_value}) cmp uc($b->{facet_label_value}) } @{ $f->{facets} } ];
+            }
         }
     }
 
diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm
index 3afcf75c37..e753220948 100644
--- a/Koha/SearchEngine/Elasticsearch/Search.pm
+++ b/Koha/SearchEngine/Elasticsearch/Search.pm
@@ -500,9 +500,14 @@ sub _convert_facets {
                 type_link_value => $type,
             };
         }
+        if( C4::Context->preference('FacetOrder') eq 'Alphabetical' ){
+            @{ $facet->{facets} } =
+                sort { $a->{facet_label_value} cmp $b->{facet_label_value} } @{ $facet->{facets} };
+        }
         push @facets, $facet if exists $facet->{facets};
     }
 
+
     @facets = sort { $a->{order} <=> $b->{order} } @facets;
     return \@facets;
 }
diff --git a/installer/data/mysql/atomicupdate/bug_28826_add_FacetSort_syspref.perl b/installer/data/mysql/atomicupdate/bug_28826_add_FacetSort_syspref.perl
new file mode 100644
index 0000000000..c69a0f9454
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_28826_add_FacetSort_syspref.perl
@@ -0,0 +1,9 @@
+$DBversion = 'XXX';
+if( CheckVersion( $DBversion ) ) {
+    $dbh->do(q{
+        INSERT IGNORE INTO systempreferences
+        ( variable, value, options, explanation, type ) VALUES
+        ('FacetOrder','Alphabetical','Alphabetical|Usage','Specify the order of facets within each category','Choice')
+    });
+    NewVersion( $DBversion, 28826, "Add system preference FacetOrder");
+}
diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql
index 6b66e42c14..9781c385bc 100644
--- a/installer/data/mysql/mandatory/sysprefs.sql
+++ b/installer/data/mysql/mandatory/sysprefs.sql
@@ -210,6 +210,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('ExtendedPatronAttributes','1',NULL,'Use extended patron IDs and attributes','YesNo'),
 ('FacetLabelTruncationLength','20',NULL,'Specify the facet max length in OPAC','Integer'),
 ('FacetMaxCount','20',NULL,'Specify the max facet count for each category','Integer'),
+('FacetOrder','Alphabetical','Alphabetical|Usage','Specify the order of facets within each category','Choice'),
 ('FailedLoginAttempts','','','Number of login attempts before lockout the patron account','Integer'),
 ('FallbackToSMSIfNoEmail', 0, 'Enable|Disable', 'Send messages by SMS if no patron email is defined', 'YesNo'),
 ('FeeOnChangePatronCategory','1','','If set, when a patron changes to a category with enrolment fee, a fee is charged','YesNo'),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref
index 25b3878307..5b9df1bc72 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref
@@ -231,6 +231,15 @@ Searching:
               class: integer
               default: 20
             - facets for each category.
+        -
+            - Sort facets
+            - pref: FacetOrder
+              type: choice
+              choices:
+                  Alphabetical: "alphabetically"
+                  Usage: "by usage count"
+              default: Alphabetical
+            - for each category.
         -
             - By default, show
             - pref: OPACnumSearchResults
-- 
2.20.1