Bug 19278: Make the pagination plugin use RESTdefaultPageSize
[koha.git] / Koha / REST / Plugin / Pagination.pm
1 package Koha::REST::Plugin::Pagination;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Plugin';
21
22 =head1 NAME
23
24 Koha::REST::Plugin::Pagination
25
26 =head1 API
27
28 =head2 Mojolicious::Plugin methods
29
30 =head3 register
31
32 =cut
33
34 sub register {
35     my ( $self, $app ) = @_;
36
37 =head2 Helper methods
38
39 =head3 add_pagination_headers
40
41     my $patrons = Koha::Patrons->search( ... );
42     $c->add_pagination_headers({
43         total  => $patrons->count,
44         params => {
45             _page     => ...
46             _per_page => ...
47             ...
48         }
49     });
50
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.
54
55 =cut
56
57     $app->helper(
58         'add_pagination_headers' => sub {
59             my ( $c, $args ) = @_;
60
61             my $total    = $args->{total};
62             my $req_page = $args->{params}->{_page};
63             my $per_page = $args->{params}->{_per_page} //
64                             C4::Context->preference('RESTdefaultPageSize');
65
66             # do we need to paginate?
67             return $c unless $req_page;
68
69             my $pages = int $total / $per_page;
70             $pages++
71                 if $total % $per_page > 0;
72
73             my @links;
74
75             if ( $pages > 1 and $req_page > 1 ) {    # Previous exists?
76                 push @links,
77                     _build_link(
78                     $c,
79                     {   page     => $req_page - 1,
80                         per_page => $per_page,
81                         rel      => 'prev',
82                         params   => $args->{params}
83                     }
84                     );
85             }
86
87             if ( $pages > 1 and $req_page < $pages ) {    # Next exists?
88                 push @links,
89                     _build_link(
90                     $c,
91                     {   page     => $req_page + 1,
92                         per_page => $per_page,
93                         rel      => 'next',
94                         params   => $args->{params}
95                     }
96                     );
97             }
98
99             push @links,
100                 _build_link( $c,
101                 { page => 1, per_page => $per_page, rel => 'first', params => $args->{params} } );
102             push @links,
103                 _build_link( $c,
104                 { page => $pages, per_page => $per_page, rel => 'last', params => $args->{params} } );
105
106             # Add Link header
107             $c->res->headers->add( 'Link' => join( ',', @links ) );
108
109             # Add X-Total-Count header
110             $c->res->headers->add( 'X-Total-Count' => $total );
111             return $c;
112         }
113     );
114
115 =head3 dbic_merge_pagination
116
117     $filter = $c->dbic_merge_pagination({
118         filter => $filter,
119         params => {
120             page     => $params->{_page},
121             per_page => $params->{_per_page}
122         }
123     });
124
125 Adds I<page> and I<rows> elements to the filter parameter.
126
127 =cut
128
129     $app->helper(
130         'dbic_merge_pagination' => sub {
131             my ( $c, $args ) = @_;
132             my $filter = $args->{filter};
133
134             $filter->{page} = $args->{params}->{_page};
135             $filter->{rows} = $args->{params}->{_per_page};
136
137             return $filter;
138         }
139     );
140 }
141
142 =head2 Internal methods
143
144 =head3 _build_link
145
146     my $link = _build_link( $c, { page => 1, per_page => 5, rel => 'prev' });
147
148 Returns a string, suitable for using in Link headers following RFC5988.
149
150 =cut
151
152 sub _build_link {
153     my ( $c, $args ) = @_;
154
155     my $params = $args->{params};
156
157     $params->{_page}     = $args->{page};
158     $params->{_per_page} = $args->{per_page};
159
160     my $link = '<'
161         . $c->req->url->clone->query(
162             $params
163         )->to_abs
164         . '>; rel="'
165         . $args->{rel} . '"';
166
167     # TODO: Find a better solution for this horrible (but needed) fix
168     $link =~ s|api/v1/app\.pl/||;
169
170     return $link;
171 }
172
173 1;