1 package Koha
::REST
::Plugin
::Pagination
;
3 # This file is part of Koha.
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
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.
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.
20 use Mojo
::Base
'Mojolicious::Plugin';
24 Koha::REST::Plugin::Pagination
28 =head2 Mojolicious::Plugin methods
35 my ( $self, $app ) = @_;
39 =head3 add_pagination_headers
41 my $patrons = Koha::Patrons->search( ... );
42 $c->add_pagination_headers({
43 total => $patrons->count,
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.
58 'add_pagination_headers' => sub {
59 my ( $c, $args ) = @_;
61 my $total = $args->{total
};
62 my $req_page = $args->{params
}->{_page
};
63 my $per_page = $args->{params
}->{_per_page
};
65 my $pages = int $total / $per_page;
67 if $total % $per_page > 0;
71 if ( $pages > 1 and $req_page > 1 ) { # Previous exists?
75 { page
=> $req_page - 1,
76 per_page
=> $per_page,
78 params
=> $args->{params
}
83 if ( $pages > 1 and $req_page < $pages ) { # Next exists?
87 { page
=> $req_page + 1,
88 per_page
=> $per_page,
90 params
=> $args->{params
}
97 { page
=> 1, per_page
=> $per_page, rel
=> 'first', params
=> $args->{params
} } );
100 { page
=> $pages, per_page
=> $per_page, rel
=> 'last', params
=> $args->{params
} } );
103 $c->res->headers->add( 'Link' => join( ',', @links ) );
105 # Add X-Total-Count header
106 $c->res->headers->add( 'X-Total-Count' => $total );
111 =head3 dbic_merge_pagination
113 $filter = $c->dbic_merge_pagination({
116 page => $params->{_page},
117 per_page => $params->{_per_page}
121 Adds I<page> and I<rows> elements to the filter parameter.
126 'dbic_merge_pagination' => sub {
127 my ( $c, $args ) = @_;
128 my $filter = $args->{filter
};
130 $filter->{page
} = $args->{params
}->{_page
};
131 $filter->{rows
} = $args->{params
}->{_per_page
};
138 =head2 Internal methods
142 my $link = _build_link( $c, { page => 1, per_page => 5, rel => 'prev' });
144 Returns a string, suitable for using in Link headers following RFC5988.
149 my ( $c, $args ) = @_;
151 my $params = $args->{params
};
153 $params->{_page
} = $args->{page
};
154 $params->{_per_page
} = $args->{per_page
};
157 . $c->req->url->clone->query(
161 . $args->{rel
} . '"';
163 # TODO: Find a better solution for this horrible (but needed) fix
164 $link =~ s
|api
/v1/app\
.pl
/||;