From 63183059dd975c2f5265fa4c7d2bc59b6ad1a252 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 24 Oct 2023 11:12:56 +0200 Subject: [PATCH] Bug 34913: Adjust item search Signed-off-by: Jonathan Druart Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- catalogue/itemsearch.pl | 29 +++++++++---------- .../prog/en/modules/catalogue/itemsearch.tt | 26 +++++++---------- .../en/modules/catalogue/itemsearch_json.tt | 6 ++-- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/catalogue/itemsearch.pl b/catalogue/itemsearch.pl index 854aced970..975b96a70b 100755 --- a/catalogue/itemsearch.pl +++ b/catalogue/itemsearch.pl @@ -19,7 +19,7 @@ use Modern::Perl; use CGI; -use JSON qw( to_json ); +use JSON qw( to_json from_json ); use C4::Auth qw( get_template_and_user ); use C4::Circulation qw( barcodedecode ); @@ -34,7 +34,6 @@ use Koha::ItemTypes; use Koha::Libraries; my $cgi = CGI->new; -my %params = $cgi->Vars; my $format = $cgi->param('format'); my $template_name = 'catalogue/itemsearch.tt'; @@ -43,11 +42,11 @@ if (defined $format and $format eq 'json') { $template_name = 'catalogue/itemsearch_json.tt'; # Map DataTables parameters with 'regular' parameters - $cgi->param('rows', scalar $cgi->param('iDisplayLength')); - $cgi->param('page', (scalar $cgi->param('iDisplayStart') / scalar $cgi->param('iDisplayLength')) + 1); - my @columns = split /,/, scalar $cgi->param('sColumns'); - $cgi->param('sortby', $columns[ scalar $cgi->param('iSortCol_0') ]); - $cgi->param('sortorder', scalar $cgi->param('sSortDir_0')); + $cgi->param('rows', scalar $cgi->param('length')); + $cgi->param('page', (scalar $cgi->param('start') / scalar $cgi->param('length')) + 1); + my @columns = @{ from_json $cgi->param('columns') }; + $cgi->param('sortby', $columns[ $cgi->param('order[0][column]') ]->{name}); + $cgi->param('sortorder', scalar $cgi->param('order[0][dir]')); my @f = $cgi->multi_param('f'); my @q = $cgi->multi_param('q'); @@ -60,16 +59,16 @@ if (defined $format and $format eq 'json') { push @q, '' if @q == 0; my @op = $cgi->multi_param('op'); my @c = $cgi->multi_param('c'); - my $iColumns = $cgi->param('iColumns'); - foreach my $i (0 .. ($iColumns - 1)) { - my $sSearch = $cgi->param("sSearch_$i"); - if (defined $sSearch and $sSearch ne '') { - my @words = split /\s+/, $sSearch; + for my $column (@columns) { + my $search = $column->{search}->{value}; + my $column_name = $column->{name}; + if (defined $search and $search ne '') { + my @words = split /\s+/, $search; foreach my $word (@words) { - push @f, $columns[$i]; + push @f, $column_name; push @c, 'and'; - if ( grep { $_ eq $columns[$i] } qw( ccode homebranch holdingbranch location itype notforloan itemlost onloan ) ) { + if ( grep { $_ eq $column_name } qw( ccode homebranch holdingbranch location itype notforloan itemlost onloan ) ) { push @q, "$word"; push @op, '='; } else { @@ -285,7 +284,7 @@ if ( defined $format ) { print "$line\n" unless $line =~ m|^\s*$|; } } elsif ($format eq 'json') { - $template->param(sEcho => scalar $cgi->param('sEcho')); + $template->param(draw => scalar $cgi->param('draw')); output_with_http_headers $cgi, $cookie, $template->output, 'json'; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt index 6613a97609..7c8ad9f17c 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch.tt @@ -472,23 +472,19 @@ $('#results').dataTable($.extend(true, {}, dataTablesDefaults, { "destroy": true, - "serverSide": true, + "serverSide": true, "processing": true, - "ajax": '/cgi-bin/koha/catalogue/itemsearch.pl', - 'fnServerData': function(sSource, aoData, fnCallback) { - aoData.push( { 'name': 'format', 'value': 'json' } ); - for (i in params) { - aoData.push(params[i]); - } - $.ajax({ - 'dataType': 'json', - 'type': 'POST', - 'url': sSource, - 'data': aoData, - 'success': function(json){ - fnCallback(json); + "ajax": { + url: '/cgi-bin/koha/catalogue/itemsearch.pl', + type: 'POST', + data: function ( d ) { + for (i in params) { + d[params[i].name] = params[i].value; } - }); + d.format = 'json'; + d.columns = JSON.stringify( d.columns ); + return d; + } }, 'dom': '<"top pager"ilp>t<"bottom pager"ip>r', 'order': [[1, 'asc']], diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch_json.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch_json.tt index fdf7ad6322..f0df4fe458 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch_json.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/itemsearch_json.tt @@ -1,7 +1,7 @@ { - "sEcho": [% sEcho | html %], - "iTotalRecords": [% total_rows | html %], - "iTotalDisplayRecords": [% total_rows | html %], + "draw": [% draw | html %], + "recordsTotal": [% total_rows | html %], + "recordsFiltered": [% total_rows | html %], "data": [ [%- FOREACH item IN results -%] [%- INCLUDE 'catalogue/itemsearch_item.json.inc' item = item, user = user -%] -- 2.39.5