Link Here
|
0 |
- |
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 |
page => $input->{page}, |
45 |
per_page => $input->{per_page} |
46 |
}); |
47 |
|
48 |
Adds a Link header to the response message $c carries, following RFC5988, including |
49 |
the following relation types: 'prev', 'next', 'first' and 'last'. |
50 |
It also adds X-Total-Count, containing the total results count. |
51 |
|
52 |
=cut |
53 |
|
54 |
$app->helper( |
55 |
'add_pagination_headers' => sub { |
56 |
my ( $c, $args ) = @_; |
57 |
|
58 |
my $total = $args->{total}; |
59 |
my $req_page = $args->{params}->{page}; |
60 |
my $per_page = $args->{params}->{per_page}; |
61 |
|
62 |
my $pages = int $total / $per_page; |
63 |
$pages++ |
64 |
if $total % $per_page > 0; |
65 |
|
66 |
my @links; |
67 |
|
68 |
if ( $pages > 1 and $req_page > 1 ) { # Previous exists? |
69 |
push @links, |
70 |
_build_link( |
71 |
$c, |
72 |
{ page => $req_page - 1, |
73 |
per_page => $per_page, |
74 |
rel => 'prev', |
75 |
params => $args->{params} |
76 |
} |
77 |
); |
78 |
} |
79 |
|
80 |
if ( $pages > 1 and $req_page < $pages ) { # Next exists? |
81 |
push @links, |
82 |
_build_link( |
83 |
$c, |
84 |
{ page => $req_page + 1, |
85 |
per_page => $per_page, |
86 |
rel => 'next', |
87 |
params => $args->{params} |
88 |
} |
89 |
); |
90 |
} |
91 |
|
92 |
push @links, |
93 |
_build_link( $c, |
94 |
{ page => 1, per_page => $per_page, rel => 'first', params => $args->{params} } ); |
95 |
push @links, |
96 |
_build_link( $c, |
97 |
{ page => $pages, per_page => $per_page, rel => 'last', params => $args->{params} } ); |
98 |
|
99 |
# Add Link header |
100 |
$c->res->headers->add( 'Link' => join( ',', @links ) ); |
101 |
|
102 |
# Add X-Total-Count header |
103 |
$c->res->headers->add( 'X-Total-Count' => $total ); |
104 |
return $c; |
105 |
} |
106 |
); |
107 |
} |
108 |
|
109 |
=head2 Internal methods |
110 |
|
111 |
=head3 _build_link |
112 |
|
113 |
my $link = _build_link( $c, { page => 1, per_page => 5, rel => 'prev' }); |
114 |
|
115 |
Returns a string, suitable for using in Link headers following RFC5988. |
116 |
|
117 |
=cut |
118 |
|
119 |
sub _build_link { |
120 |
my ( $c, $args ) = @_; |
121 |
|
122 |
my $params = $args->{params}; |
123 |
|
124 |
$params->{page} = $args->{page}; |
125 |
$params->{per_page} = $args->{per_page}; |
126 |
|
127 |
my $link = '<' |
128 |
. $c->req->url->clone->query( |
129 |
$params |
130 |
)->to_abs |
131 |
. '>; rel="' |
132 |
. $args->{rel} . '"'; |
133 |
|
134 |
# TODO: Find a better solution for this horrible (but needed) fix |
135 |
$link =~ s|api/v1/app\.pl/||; |
136 |
|
137 |
return $link; |
138 |
} |
139 |
|
140 |
# [ 'per_page' => $params->{per_page}, |
141 |
# 'page' => $params->{page} |
142 |
# ] |
143 |
|
144 |
1; |