From ef40ba17aef792df332a11f7e0269c5684d913ac Mon Sep 17 00:00:00 2001 From: Nick Clemens 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 Signed-off-by: David Nind Signed-off-by: Martin Renvoize Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall --- 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 8c546ecfab..237564c26a 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 ff56ad1174..92d3172bc0 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -502,9 +502,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 6d0e8ec6c3..1ec4580507 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -208,6 +208,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 ebc82bdf8f..1130c2cf29 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.39.5