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, $params ) = @_; |
57 |
|
58 |
my $total = $params->{total}; |
59 |
my $page = $params->{page}; |
60 |
my $per_page = $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 $page > 1 ) { # Previous exists? |
69 |
push @links, |
70 |
_build_link( $c, { page => $page - 1, per_page => $per_page, rel => 'prev' } ); |
71 |
} |
72 |
|
73 |
if ( $pages > 1 and $page < $pages ) { # Next exists? |
74 |
push @links, |
75 |
_build_link( $c, { page => $page + 1, per_page => $per_page, rel => 'next' } ); |
76 |
} |
77 |
|
78 |
push @links, _build_link( $c, { page => 1, per_page => $per_page, rel => 'first' } ); |
79 |
push @links, _build_link( $c, { page => $pages, per_page => $per_page, rel => 'last' } ); |
80 |
|
81 |
# Add Link header |
82 |
$c->res->headers->add( 'Link' => join( ',', @links ) ); |
83 |
# Add X-Total-Count header |
84 |
$c->res->headers->add( 'X-Total-Count' => $total ); |
85 |
return $c; |
86 |
} |
87 |
); |
88 |
} |
89 |
|
90 |
=head2 Internal methods |
91 |
|
92 |
=head3 _build_link |
93 |
|
94 |
my $link = _build_link( $c, { page => 1, per_page => 5, rel => 'prev' }); |
95 |
|
96 |
Returns a string, suitable for using in Link headers following RFC5988. |
97 |
|
98 |
=cut |
99 |
|
100 |
sub _build_link { |
101 |
my ( $c, $params ) = @_; |
102 |
|
103 |
my $link = '<' |
104 |
. $c->req->url->clone->query( |
105 |
[ 'per_page' => $params->{per_page}, |
106 |
'page' => $params->{page} |
107 |
] |
108 |
)->to_abs |
109 |
. '>; rel="' |
110 |
. $params->{rel} . '"'; |
111 |
|
112 |
# TODO: Find a better solution for this horrible (but needed) fix |
113 |
$link =~ s|api/v1/app\.pl/||; |
114 |
|
115 |
return $link; |
116 |
} |
117 |
|
118 |
1; |