1 package Koha::REST::Plugin::Pagination;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Mojo::Base 'Mojolicious::Plugin';
24 Koha::REST::Plugin::Pagination
28 =head2 Mojolicious::Plugin methods
35 my ( $self, $app ) = @_;
39 =head3 add_pagination_headers
41 $c->add_pagination_headers();
42 $c->add_pagination_headers(
44 base_total => $base_total,
46 per_page => $per_page,
47 query_params => $query_params,
52 Adds RFC5988 compliant I<Link> headers for pagination to the response message carried
55 Additionally, it also adds the customer I<X-Total-Count> header containing the total results
56 count, and I<X-Base-Total-Count> header containing the total of the non-filtered results count.
58 Optionally accepts the any of the following parameters to override the values stored in the
59 stash by the I<search_rs> helper.
65 Total records for the search domain (e.g. all patron records, filtered only by visibility)
69 The requested page number, usually extracted from the request query.
70 See I<objects.search_rs> for more information.
74 The requested maximum results per page, usually extracted from the request query.
75 See I<objects.search_rs> for more information.
79 The request query, usually extracted from the request query and used to build the I<Link> headers.
80 See I<objects.search_rs> for more information.
84 Total records for the search with filters applied.
91 'add_pagination_headers' => sub {
92 my ( $c, $args ) = @_;
94 my $base_total = $args->{base_total} // $c->stash('koha.pagination.base_total');
95 my $req_page = $args->{page} // $c->stash('koha.pagination.page') // 1;
96 my $per_page = $args->{per_page} // $c->stash('koha.pagination.per_page') // C4::Context->preference('RESTdefaultPageSize') // 20;
97 my $params = $args->{query_params} // $c->stash('koha.pagination.query_params');
98 my $total = $args->{total} // $c->stash('koha.pagination.total');
101 if ( $per_page == -1 ) {
106 $pages = int $total / $per_page;
108 if $total % $per_page > 0;
113 if ( $per_page != -1 and $pages > 1 and $req_page > 1 ) { # Previous exists?
117 { page => $req_page - 1,
118 per_page => $per_page,
125 if ( $per_page != -1 and $pages > 1 and $req_page < $pages ) { # Next exists?
129 { page => $req_page + 1,
130 per_page => $per_page,
139 { page => 1, per_page => $per_page, rel => 'first', params => $params } );
142 { page => $pages, per_page => $per_page, rel => 'last', params => $params } );
145 foreach my $link (@links) {
146 $c->res->headers->add( 'Link' => $link );
149 # Add X-Total-Count header
150 $c->res->headers->add( 'X-Total-Count' => $total );
151 $c->res->headers->add( 'X-Base-Total-Count' => $base_total )
152 if defined $base_total;
158 =head3 dbic_merge_pagination
160 $filter = $c->dbic_merge_pagination({
163 page => $params->{_page},
164 per_page => $params->{_per_page}
168 Adds I<page> and I<rows> elements to the filter parameter.
173 'dbic_merge_pagination' => sub {
174 my ( $c, $args ) = @_;
175 my $filter = $args->{filter};
177 $filter->{page} = $args->{params}->{_page};
178 $filter->{rows} = $args->{params}->{_per_page};
185 =head2 Internal methods
189 my $link = _build_link( $c, { page => 1, per_page => 5, rel => 'prev' });
191 Returns a string, suitable for using in Link headers following RFC5988.
196 my ( $c, $args ) = @_;
198 my $params = $args->{params};
200 $params->{_page} = $args->{page};
201 $params->{_per_page} = $args->{per_page};
204 . $c->req->url->clone->query(
208 . $args->{rel} . '"';
210 # TODO: Find a better solution for this horrible (but needed) fix
211 $link =~ s|api/v1/app\.pl/||;