From 944a9178e90d28b472ffea8e5a8fa5c14cc2f715 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) Signed-off-by: Tomas Cohen Arazi --- Koha/REST/Plugin/Objects.pm | 20 ++++++++----------- Koha/REST/Plugin/Pagination.pm | 4 +++- Koha/REST/V1/Patrons.pm | 17 ++++++++-------- koha-tmpl/intranet-tmpl/prog/js/datatables.js | 3 +++ .../opac-tmpl/bootstrap/js/datatables.js | 3 +++ 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index 0f08a2d37ae..36f647833a2 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -124,19 +124,15 @@ 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, - }); - } - else { - $c->add_pagination_headers({ - total => $objects->count, - params => $args, - }); - } + $c->add_pagination_headers( + { + total => ($objects->is_paged ? $objects->pager->total_entries : $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 911aa43aafc..90d220c1cf9 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/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 5eba4bdee34..b37452ec82c 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -82,14 +82,15 @@ sub list { if $restricted; my $patrons = $patrons_rs->search( $filtered_params, $attributes ); - if ( $patrons_rs->is_paged ) { - $c->add_pagination_headers( - { - total => $patrons->pager->total_entries, - params => $args, - } - ); - } + my $total = $patrons_rs->search->count; + + $c->add_pagination_headers( + { + total => ($patrons->is_paged ? $patrons->pager->total_entries : $patrons->count), + base_total => $total, + params => $args, + } + ); return $c->render( status => 200, openapi => $patrons->to_api ); } diff --git a/koha-tmpl/intranet-tmpl/prog/js/datatables.js b/koha-tmpl/intranet-tmpl/prog/js/datatables.js index 13db0e495ca..69fc5a998f8 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 ) { diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js b/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js index f8b1633de86..47f24e28292 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/datatables.js @@ -207,6 +207,9 @@ jQuery.extend( jQuery.fn.dataTableExt.oSort, { 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.30.0