From 0fc5f049f46c73102f856bfe0869e1d1037e83b8 Mon Sep 17 00:00:00 2001 From: Ivan Dziuba Date: Thu, 22 Apr 2021 18:22:41 -0400 Subject: [PATCH] Bug 27428: Show the number of records in the bibliographic record detail page with ElasticSearch. Tests: 1. Apply this patch; 2. In the Preference system - OPACDisplayRecordCount -> Show; 3. We can see the number of records next to search links in the bibliographic record detail page in the OPAC. Signed-off-by: Stina Hallin Signed-off-by: Solene Ngamga Signed-off-by: Petro Vashchuk --- Koha/SearchEngine/Elasticsearch/Browse.pm | 42 +++++++++ ...ber_records_next_to_search_links_opac.perl | 8 ++ installer/data/mysql/mandatory/sysprefs.sql | 1 + .../modules/admin/preferences/searching.pref | 8 ++ .../bootstrap/en/includes/doc-head-close.inc | 3 + .../bootstrap/en/modules/opac-detail.tt | 3 + .../bootstrap/js/opac-display-record-count.js | 88 +++++++++++++++++++ opac/svc/elasticsearch/opac-elasticsearch.pl | 51 +++++++++++ 8 files changed, 204 insertions(+) create mode 100644 installer/data/mysql/atomicupdate/bug_27428-number_records_next_to_search_links_opac.perl create mode 100644 koha-tmpl/opac-tmpl/bootstrap/js/opac-display-record-count.js create mode 100755 opac/svc/elasticsearch/opac-elasticsearch.pl diff --git a/Koha/SearchEngine/Elasticsearch/Browse.pm b/Koha/SearchEngine/Elasticsearch/Browse.pm index 85070730a7..5d4282ed2f 100644 --- a/Koha/SearchEngine/Elasticsearch/Browse.pm +++ b/Koha/SearchEngine/Elasticsearch/Browse.pm @@ -48,6 +48,7 @@ as "suggestible" in the database when indexing takes place. use base qw(Koha::SearchEngine::Elasticsearch); use Modern::Perl; +use JSON; =head2 browse @@ -162,6 +163,47 @@ sub _build_query { return $query; } +sub _build_query_count_record { + my ($self, $cgi_q, $key, $item) = @_; + my $query = { + query => { + match_phrase => { + $key => $item + } + } + }; + return $query; +} + +sub count_record { + my ($self, $cgi_q) = @_; + my @results_return; + my $decoded; + $cgi_q =~ s/\{\'/\{\"/g; + $cgi_q =~ s/\'\}/\"\}/g; + #$cgi_q =~ s/\'\:\'/\"\:\"}/g; + eval { + $decoded = JSON::XS::decode_json($cgi_q); + }; + if ($@) { + warn "Caught JSON::XS decode error: $_"; + } + + foreach my $item (@$decoded) + { + for my $key ( keys %$item ){ + my $elasticsearch = $self->get_elasticsearch(); + my $query = $self->_build_query_count_record($cgi_q, $key, $$item{$key}); + my $res = $elasticsearch->search( + index => $self->index_name, + body => $query + ); + push(@results_return, $res->{'hits'}->{'total'}); + } + } + return \@results_return; +} + 1; __END__ diff --git a/installer/data/mysql/atomicupdate/bug_27428-number_records_next_to_search_links_opac.perl b/installer/data/mysql/atomicupdate/bug_27428-number_records_next_to_search_links_opac.perl new file mode 100644 index 0000000000..5b112ef81c --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_27428-number_records_next_to_search_links_opac.perl @@ -0,0 +1,8 @@ +$DBversion = 'XXX'; # will be replaced by the RM +if( CheckVersion( $DBversion ) ) { + # you can use $dbh here like: + $dbh->do(q{INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES ('OPACDisplayRecordCount', '0', NULL, 'If enabled, show the number of records next to search links in the bibliographic record detail page in the OPAC.', 'YesNo')}); + + # Always end with this (adjust the bug info) + NewVersion( $DBversion, 27428, "Show the number of records in the detail page in the OPAC"); +} diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index 044361b6ad..64e870e206 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -456,6 +456,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('OPACdefaultSortOrder','dsc','asc|dsc|za|az','Specify the default sort order','Choice'), ('OPACdidyoumean','',NULL,'Did you mean? configuration for the OPAC. Do not change, as this is controlled by /cgi-bin/koha/admin/didyoumean.pl.','Free'), ('OPACDisplay856uAsImage','OFF','OFF|Details|Results|Both','Display the URI in the 856u field as an image, the corresponding OPACXSLT option must be on','Choice'), +('OPACDisplayRecordCount','0',NULL,'If enabled, show the number of records next to search links in the bibliographic record detail page in the OPAC.','YesNo'), ('OpacExportOptions','bibtex,dc,marcxml,marc8,utf8,marcstd,mods,ris,isbd','','Define export options available on OPAC detail page.','multiple'), ('OPACFallback', 'prog', 'bootstrap|prog', 'Define the fallback theme for the OPAC interface.', 'Themes'), ('OpacFavicon','','','Enter a complete URL to an image to replace the default Koha favicon on the OPAC','free'), 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 93084ee641..5d6d98ba3a 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 @@ -326,3 +326,11 @@ Searching: - pref: LibrisURL class: url - "Please only change this if you are sure it needs changing." + - + - pref: OPACDisplayRecordCount + type: boolean + default: 0 + choices: + 1: Show + 0: "Don't show" + - the number of records next to search links in the bibliographic record detail page in the OPAC. \ No newline at end of file diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/doc-head-close.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/doc-head-close.inc index 402633854b..11eb263d73 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/doc-head-close.inc +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/doc-head-close.inc @@ -50,6 +50,9 @@ [% Branches.GetBranchSpecificCSS( Branches.GetLoggedInBranchcode() || default_branch) | $raw %] [% END %] +[% IF ( Koha.Preference('OPACDisplayRecordCount') ) %] + [% Asset.js("js/opac-display-record-count.js") | $raw %] +[% END %] [% IF SCO_login %] [% SET SCOUserCSS = Koha.Preference('SCOUserCSS') %] [% IF SCOUserCSS %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt index 789244f9d3..3eb56d460e 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt @@ -1454,6 +1454,9 @@ [% END %]