From 55013bb699002972144fda996ef626164040c07e Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 6 Jan 2021 16:34:23 +0100 Subject: [PATCH] Bug 27353: Set X-Base-Total-Count header for REST API We already set X-Total-Count to the total number of filtered rows, but we don't have the total number of non-filtered rows. Test plan: This is easy to test on top of bug 27352 or bug 27251, apply them if not pushed yet. 1. Create 40 items with public notes = "xxx" for biblionumber=4 then, using Postman (or whatever you prefer): http://kohadev-intra.mydnsname.org:8081/api/v1/biblios/4/items?_page=1&_per_page=20&q=[{"me.public_notes"%3A{"like"%3A"%25x%25"}}]&_match=contains Check the headers and confirm you see X-Total-Count=40 and X-Base-Total-Count=44 2. go to /cgi-bin/koha/tools/quotes.pl You see "Showing 1 to 20 of 28 entries" Search "he" Showing 1 to 20 of 22 entries (filtered from 28 total entries) --- Koha/REST/Plugin/Objects.pm | 23 ++++++++++++------- Koha/REST/Plugin/Pagination.pm | 4 +++- koha-tmpl/intranet-tmpl/prog/js/datatables.js | 3 +++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index fec1fe794a..35742dc1ff 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -114,18 +114,25 @@ sub register { } # Perform search my $objects = $result_set->search( $filtered_params, $attributes ); + my $total = $result_set->search->count; if ($objects->is_paged) { - $c->add_pagination_headers({ - total => $objects->pager->total_entries, - params => $args, - }); + $c->add_pagination_headers( + { + total => $objects->pager->total_entries, + base_total => $total, + params => $args, + } + ); } else { - $c->add_pagination_headers({ - total => $objects->count, - params => $args, - }); + $c->add_pagination_headers( + { + total => $objects->count, + base_total => $total, + params => $args, + } + ); } return $objects->to_api({ embed => $embed }); diff --git a/Koha/REST/Plugin/Pagination.pm b/Koha/REST/Plugin/Pagination.pm index 911aa43aaf..90d220c1cf 100644 --- a/Koha/REST/Plugin/Pagination.pm +++ b/Koha/REST/Plugin/Pagination.pm @@ -50,7 +50,7 @@ sub register { Adds a Link header to the response message $c carries, following RFC5988, including the following relation types: 'prev', 'next', 'first' and 'last'. -It also adds X-Total-Count, containing the total results count. +It also adds X-Total-Count containing the total results count, and X-Base-Total-Count containing the total of the non-filtered results count. If page size is omitted, it defaults to the value of the RESTdefaultPageSize syspref. @@ -61,6 +61,7 @@ If page size is omitted, it defaults to the value of the RESTdefaultPageSize sys my ( $c, $args ) = @_; my $total = $args->{total}; + my $base_total = $args->{base_total}; my $req_page = $args->{params}->{_page} // 1; my $per_page = $args->{params}->{_per_page} // C4::Context->preference('RESTdefaultPageSize') // 20; @@ -114,6 +115,7 @@ If page size is omitted, it defaults to the value of the RESTdefaultPageSize sys # Add X-Total-Count header $c->res->headers->add( 'X-Total-Count' => $total ); + $c->res->headers->add( 'X-Base-Total-Count' => $base_total ); return $c; } ); diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js index 13db0e495c..69fc5a998f 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/datatables.js +++ b/koha-tmpl/intranet-tmpl/prog/js/datatables.js @@ -544,6 +544,9 @@ jQuery.fn.dataTable.ext.errMode = function(settings, note, message) { json.recordsTotal = total; json.recordsFiltered = total; } + if(total = this._xhr.getResponseHeader('x-base-total-count')) { + json.recordsTotal = total; + } return JSON.stringify(json); }, 'data': function( data, settings ) { -- 2.20.1