From 3a0e14b617a1dbe958f5afa8e3b21da075faac07 Mon Sep 17 00:00:00 2001
From: Hammat Wele <hammat.wele@inlibro.com>
Date: Wed, 8 Nov 2023 01:10:01 +0000
Subject: [PATCH] Bug 27428: (follow-up) perltidy, change the atomic file to
 the newer format, moving system preference to Features section and Fixing
 opac-display-record-count.js

Signed-off-by: David Nind <david@davidnind.com>
---
 Koha/SearchEngine/Elasticsearch/Browse.pm     | 30 ++++------
 ...ber_records_next_to_search_links_opac.perl |  8 ---
 ...umber_records_next_to_search_links_opac.pl | 18 ++++++
 .../bootstrap/js/opac-display-record-count.js | 20 +------
 opac/svc/elasticsearch/opac-elasticsearch.pl  | 56 ++++++++++---------
 5 files changed, 61 insertions(+), 71 deletions(-)
 delete mode 100644 installer/data/mysql/atomicupdate/bug_27428-number_records_next_to_search_links_opac.perl
 create mode 100755 installer/data/mysql/atomicupdate/bug_27428-number_records_next_to_search_links_opac.pl

diff --git a/Koha/SearchEngine/Elasticsearch/Browse.pm b/Koha/SearchEngine/Elasticsearch/Browse.pm
index 14e1d1b0f9..98052fea0a 100644
--- a/Koha/SearchEngine/Elasticsearch/Browse.pm
+++ b/Koha/SearchEngine/Elasticsearch/Browse.pm
@@ -164,14 +164,8 @@ sub _build_query {
 }
 
 sub _build_query_count_record {
-    my ($self, $cgi_q, $key, $item) = @_;
-    my $query = {
-        query => {
-            match_phrase => {
-                $key  => $item
-            }
-        }
-    };
+    my ( $self, $cgi_q, $key, $item ) = @_;
+    my $query = { query => { match_phrase => { $key => $item } } };
     return $query;
 }
 
@@ -184,29 +178,27 @@ Return an arrayref of record counts
 =cut
 
 sub count_record {
-    my ($self, $cgi_q) = @_;
+    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);
-    };
+    eval { $decoded = JSON::XS::decode_json($cgi_q); };
     if ($@) {
         warn "Caught JSON::XS decode error: $_";
     }
 
-    foreach my $item (@$decoded)
-    {
-        for my $key ( keys %$item ){
+    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(
+            my $query         = $self->_build_query_count_record( $cgi_q, $key, $$item{$key} );
+            my $res           = $elasticsearch->search(
                 index => $self->index_name,
-                body => $query
+                body  => $query
             );
-        push(@results_return, $res->{'hits'}->{'total'});
+            push( @results_return, $res->{'hits'}->{'total'} );
         }
     }
     return \@results_return;
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
deleted file mode 100644
index 5b112ef81c..0000000000
--- a/installer/data/mysql/atomicupdate/bug_27428-number_records_next_to_search_links_opac.perl
+++ /dev/null
@@ -1,8 +0,0 @@
-$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/atomicupdate/bug_27428-number_records_next_to_search_links_opac.pl b/installer/data/mysql/atomicupdate/bug_27428-number_records_next_to_search_links_opac.pl
new file mode 100755
index 0000000000..ff5e8b3fe9
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_27428-number_records_next_to_search_links_opac.pl
@@ -0,0 +1,18 @@
+use Modern::Perl;
+
+return {
+    bug_number  => "27428",
+    description => "A single line description",
+    up          => sub {
+        my ($args) = @_;
+        my ( $dbh, $out ) = @$args{qw(dbh out)};
+
+        # Do you stuffs here
+        $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')}
+        );
+
+        # Print useful stuff here
+        say $out "Added new system preference 'OPACDisplayRecordCount'";
+    },
+};
\ No newline at end of file
diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/opac-display-record-count.js b/koha-tmpl/opac-tmpl/bootstrap/js/opac-display-record-count.js
index 7fc68f19f0..efefb4b2fc 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/js/opac-display-record-count.js
+++ b/koha-tmpl/opac-tmpl/bootstrap/js/opac-display-record-count.js
@@ -1,11 +1,7 @@
 var url_request = '/cgi-bin/koha/svc/elasticsearch/opac-elasticsearch.pl?q=';
 
-//const url_param = '_search?filter_path=hits.total&source_content_type=application/json'
-/* Regex pour tous les URL avec un recherche */
 var regex_es_link = "/cgi-bin/koha/opac-search.pl?q=";
-/* key API */
 var key_display = 'nb_notice';
-/* idx de recherche */
 var es_builder = {
     'an' : 'koha-auth-number',
     'Provider' : 'provider',
@@ -13,8 +9,6 @@ var es_builder = {
     'au' : 'author',
     'su,complete-subfield' : 'subject'
 }
-/* On remplace tous les caractères suivants  */
-//https://www.obkb.com/dcljr/charstxt.html
 var not_symbols = {
     '\'':'%27',
     '.':'%2E',
@@ -46,21 +40,15 @@ var not_symbols = {
 
 function NumberResultsRecord(){
 
-    var regex = 'a[href*="${' + regex_es_link + '}"]';
+    var regex = 'a[href*="' + regex_es_link + '"]';
     var all_link = $(regex);
-    /* array json data to server */
     var json_data=[];
 
     $.each( all_link, function( i, val ) {
-        var url = '';
         var str = val.href;
-        /* on prend param de q= */
         str = decodeURIComponent(str.split('?q=').pop());
-        /* split query for fields for search [0] */
-		var es_search = str.split(':');
-        /* Remplacement pour un requete */
-		var str_search = str.substring(str.indexOf(":") + 1);
-		/* [0] this is field for search */
+        var es_search = str.split(':');
+        var str_search = str.substring(str.indexOf(":") + 1);
 		str_search = str_search.replace(/[&\/\\#,+()$~%.'":;*?<>{}]/g, function(matched){
 			return not_symbols[matched];
         });
@@ -74,10 +62,8 @@ function NumberResultsRecord(){
     type: 'GET',
     url: url_request + JSON.stringify(json_data) + '&key=' + key_display,
     success: function (data) {
-        //consoleconsole.log(data);
         jQuery.each( all_link, function( i, val ) {
             $(val).after("&nbsp;<span style=\"color:black\"><b>["+data[i]+"]</b></span>");
-            //console.log(val);
         });
     },
     error: function (data) {
diff --git a/opac/svc/elasticsearch/opac-elasticsearch.pl b/opac/svc/elasticsearch/opac-elasticsearch.pl
index 85018550cc..d8d8d22ab5 100755
--- a/opac/svc/elasticsearch/opac-elasticsearch.pl
+++ b/opac/svc/elasticsearch/opac-elasticsearch.pl
@@ -9,43 +9,45 @@ use CGI::Session;
 use Koha::SearchEngine::Elasticsearch::Browse;
 
 my $browser = Koha::SearchEngine::Elasticsearch::Browse->new( { index => 'biblios' } );
-my $cgi = CGI->new;
+my $cgi     = CGI->new;
 my $session = CGI::Session->new();
 
-$session->param(-name=>'q', -value=>$cgi->param("q"));
-$session->param(-name=>'key', -value=>$cgi->param("key"));
+$session->param( -name => 'q',   -value => $cgi->param("q") );
+$session->param( -name => 'key', -value => $cgi->param("key") );
 $session->expire('+1h');
 
 my $iskey = 0;
 
 #Chose GET in key parametre
-if ($session->param("key") eq "nb_notice") {
-  $iskey = 1;
-  my $ses = NFKD( $session->param("q") );
-  $ses =~ s/\p{NonspacingMark}//g;
-  my $res = $browser->count_record($ses);
-  print $cgi->header("application/json");
-  print to_json($res);
+if ( $session->param("key") eq "nb_notice" ) {
+    $iskey = 1;
+    my $ses = NFKD( $session->param("q") );
+    $ses =~ s/\p{NonspacingMark}//g;
+    my $res = $browser->count_record($ses);
+    print $cgi->header("application/json");
+    print to_json($res);
 }
 
-if ($iskey == 0) {
-  response404JSON();
+if ( $iskey == 0 ) {
+    response404JSON();
 }
 
 sub response404JSON {
-  my $res = CGI->new;
-  my $json = JSON->new->utf8;
-  my $header_type = "application/json";
-  my $header_status = "404";
-  my $output = $json->encode({
-    "error" => "No data",
-    "description" => "Bad request",
-  });
-  print $res->header(
-    -type => $header_type,
-    -charset => "utf-8",
-    -status => $header_status
-  );
-  print $output;
-  print "\n";
+    my $res           = CGI->new;
+    my $json          = JSON->new->utf8;
+    my $header_type   = "application/json";
+    my $header_status = "404";
+    my $output        = $json->encode(
+        {
+            "error"       => "No data",
+            "description" => "Bad request",
+        }
+    );
+    print $res->header(
+        -type    => $header_type,
+        -charset => "utf-8",
+        -status  => $header_status
+    );
+    print $output;
+    print "\n";
 }
-- 
2.34.1