Bug 26922: Regression tests
[koha.git] / Koha / REST / Plugin / Pagination.pm
blob911aa43aafcf548f9e91f3f159d948ff7bf87bef
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>.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Plugin';
22 =head1 NAME
24 Koha::REST::Plugin::Pagination
26 =head1 API
28 =head2 Mojolicious::Plugin methods
30 =head3 register
32 =cut
34 sub register {
35 my ( $self, $app ) = @_;
37 =head2 Helper methods
39 =head3 add_pagination_headers
41 my $patrons = Koha::Patrons->search( ... );
42 $c->add_pagination_headers({
43 total => $patrons->count,
44 params => {
45 _page => ...
46 _per_page => ...
47 ...
49 });
51 Adds a Link header to the response message $c carries, following RFC5988, including
52 the following relation types: 'prev', 'next', 'first' and 'last'.
53 It also adds X-Total-Count, containing the total results count.
55 If page size is omitted, it defaults to the value of the RESTdefaultPageSize syspref.
57 =cut
59 $app->helper(
60 'add_pagination_headers' => sub {
61 my ( $c, $args ) = @_;
63 my $total = $args->{total};
64 my $req_page = $args->{params}->{_page} // 1;
65 my $per_page = $args->{params}->{_per_page} //
66 C4::Context->preference('RESTdefaultPageSize') // 20;
68 my $pages;
69 if ( $per_page == -1 ) {
70 $req_page = 1;
71 $pages = 1;
73 else {
74 $pages = int $total / $per_page;
75 $pages++
76 if $total % $per_page > 0;
79 my @links;
81 if ( $per_page != -1 and $pages > 1 and $req_page > 1 ) { # Previous exists?
82 push @links,
83 _build_link(
84 $c,
85 { page => $req_page - 1,
86 per_page => $per_page,
87 rel => 'prev',
88 params => $args->{params}
93 if ( $per_page != -1 and $pages > 1 and $req_page < $pages ) { # Next exists?
94 push @links,
95 _build_link(
96 $c,
97 { page => $req_page + 1,
98 per_page => $per_page,
99 rel => 'next',
100 params => $args->{params}
105 push @links,
106 _build_link( $c,
107 { page => 1, per_page => $per_page, rel => 'first', params => $args->{params} } );
108 push @links,
109 _build_link( $c,
110 { page => $pages, per_page => $per_page, rel => 'last', params => $args->{params} } );
112 # Add Link header
113 $c->res->headers->add( 'Link' => join( ',', @links ) );
115 # Add X-Total-Count header
116 $c->res->headers->add( 'X-Total-Count' => $total );
117 return $c;
121 =head3 dbic_merge_pagination
123 $filter = $c->dbic_merge_pagination({
124 filter => $filter,
125 params => {
126 page => $params->{_page},
127 per_page => $params->{_per_page}
131 Adds I<page> and I<rows> elements to the filter parameter.
133 =cut
135 $app->helper(
136 'dbic_merge_pagination' => sub {
137 my ( $c, $args ) = @_;
138 my $filter = $args->{filter};
140 $filter->{page} = $args->{params}->{_page};
141 $filter->{rows} = $args->{params}->{_per_page};
143 return $filter;
148 =head2 Internal methods
150 =head3 _build_link
152 my $link = _build_link( $c, { page => 1, per_page => 5, rel => 'prev' });
154 Returns a string, suitable for using in Link headers following RFC5988.
156 =cut
158 sub _build_link {
159 my ( $c, $args ) = @_;
161 my $params = $args->{params};
163 $params->{_page} = $args->{page};
164 $params->{_per_page} = $args->{per_page};
166 my $link = '<'
167 . $c->req->url->clone->query(
168 $params
169 )->to_abs
170 . '>; rel="'
171 . $args->{rel} . '"';
173 # TODO: Find a better solution for this horrible (but needed) fix
174 $link =~ s|api/v1/app\.pl/||;
176 return $link;