Bug 24545: Fix license statements
[koha-equinox.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
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.
9 #
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.
14 #
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>.
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 If page size is omitted, it defaults to the value of the RESTdefaultPageSize syspref.
56
57 =cut
58
59     $app->helper(
60         'add_pagination_headers' => sub {
61             my ( $c, $args ) = @_;
62
63             my $total    = $args->{total};
64             my $req_page = $args->{params}->{_page};
65             my $per_page = $args->{params}->{_per_page} //
66                             C4::Context->preference('RESTdefaultPageSize') // 20;
67
68             # do we need to paginate?
69             return $c unless $req_page;
70
71             my $pages = int $total / $per_page;
72             $pages++
73                 if $total % $per_page > 0;
74
75             my @links;
76
77             if ( $pages > 1 and $req_page > 1 ) {    # Previous exists?
78                 push @links,
79                     _build_link(
80                     $c,
81                     {   page     => $req_page - 1,
82                         per_page => $per_page,
83                         rel      => 'prev',
84                         params   => $args->{params}
85                     }
86                     );
87             }
88
89             if ( $pages > 1 and $req_page < $pages ) {    # Next exists?
90                 push @links,
91                     _build_link(
92                     $c,
93                     {   page     => $req_page + 1,
94                         per_page => $per_page,
95                         rel      => 'next',
96                         params   => $args->{params}
97                     }
98                     );
99             }
100
101             push @links,
102                 _build_link( $c,
103                 { page => 1, per_page => $per_page, rel => 'first', params => $args->{params} } );
104             push @links,
105                 _build_link( $c,
106                 { page => $pages, per_page => $per_page, rel => 'last', params => $args->{params} } );
107
108             # Add Link header
109             $c->res->headers->add( 'Link' => join( ',', @links ) );
110
111             # Add X-Total-Count header
112             $c->res->headers->add( 'X-Total-Count' => $total );
113             return $c;
114         }
115     );
116
117 =head3 dbic_merge_pagination
118
119     $filter = $c->dbic_merge_pagination({
120         filter => $filter,
121         params => {
122             page     => $params->{_page},
123             per_page => $params->{_per_page}
124         }
125     });
126
127 Adds I<page> and I<rows> elements to the filter parameter.
128
129 =cut
130
131     $app->helper(
132         'dbic_merge_pagination' => sub {
133             my ( $c, $args ) = @_;
134             my $filter = $args->{filter};
135
136             $filter->{page} = $args->{params}->{_page};
137             $filter->{rows} = $args->{params}->{_per_page};
138
139             return $filter;
140         }
141     );
142 }
143
144 =head2 Internal methods
145
146 =head3 _build_link
147
148     my $link = _build_link( $c, { page => 1, per_page => 5, rel => 'prev' });
149
150 Returns a string, suitable for using in Link headers following RFC5988.
151
152 =cut
153
154 sub _build_link {
155     my ( $c, $args ) = @_;
156
157     my $params = $args->{params};
158
159     $params->{_page}     = $args->{page};
160     $params->{_per_page} = $args->{per_page};
161
162     my $link = '<'
163         . $c->req->url->clone->query(
164             $params
165         )->to_abs
166         . '>; rel="'
167         . $args->{rel} . '"';
168
169     # TODO: Find a better solution for this horrible (but needed) fix
170     $link =~ s|api/v1/app\.pl/||;
171
172     return $link;
173 }
174
175 1;