|
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 links_header |
| 40 |
|
| 41 |
my $patrons = Koha::Patrons->search( ... ); |
| 42 |
$c->links_header({ |
| 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 |
|
| 51 |
=cut |
| 52 |
|
| 53 |
$app->helper( |
| 54 |
'links_header' => sub { |
| 55 |
my ( $c, $params ) = @_; |
| 56 |
|
| 57 |
my $total = $params->{total}; |
| 58 |
my $page = $params->{page}; |
| 59 |
my $per_page = $params->{per_page}; |
| 60 |
|
| 61 |
my $pages = int $total / $per_page; |
| 62 |
$pages++ |
| 63 |
if $total % $per_page > 0; |
| 64 |
|
| 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 |
$c->res->headers->add( 'Link' => join( ',', @links ) ); |
| 82 |
|
| 83 |
return $c; |
| 84 |
} |
| 85 |
); |
| 86 |
|
| 87 |
=head3 total_count_header |
| 88 |
|
| 89 |
my $patrons = Koha::Patrons->search( ... ); |
| 90 |
$c->total_count_header({ total => $patrons->count }); |
| 91 |
|
| 92 |
Adds an X-Total-Count header containing a passed integer to the response message of $c. |
| 93 |
|
| 94 |
=cut |
| 95 |
|
| 96 |
$app->helper( |
| 97 |
'total_count_header' => sub { |
| 98 |
my ( $c, $params ) = @_; |
| 99 |
my $total = $params->{total}; |
| 100 |
|
| 101 |
$c->res->headers->add( 'X-Total-Count' => $total ) |
| 102 |
if $total; |
| 103 |
|
| 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, $params ) = @_; |
| 121 |
|
| 122 |
my $link = '<' |
| 123 |
. $c->req->url->clone->query( |
| 124 |
[ 'per_page' => $params->{per_page}, |
| 125 |
'page' => $params->{page} |
| 126 |
] |
| 127 |
)->to_abs |
| 128 |
. '>; rel="' |
| 129 |
. $params->{rel} . '"'; |
| 130 |
|
| 131 |
# TODO: Find a better solution for this horrible (but needed) fix |
| 132 |
$link =~ s|api/v1/app\.pl/||; |
| 133 |
|
| 134 |
return $link; |
| 135 |
} |
| 136 |
|
| 137 |
1; |