1 package Koha::REST::Plugin::Query;
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::Query
28 =head2 Mojolicious::Plugin methods
35 my ( $self, $app ) = @_;
39 =head3 extract_reserved_params
41 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($params);
43 Generates the DBIC query from the query parameters.
48 'extract_reserved_params' => sub {
49 my ( $c, $params ) = @_;
54 my $reserved_words = _reserved_words();
56 foreach my $param ( keys %{$params} ) {
57 if ( grep { $param eq $_ } @{$reserved_words} ) {
58 $reserved_params->{$param} = $params->{$param};
61 $filtered_params->{$param} = $params->{$param};
65 return ( $filtered_params, $reserved_params );
69 =head3 dbic_merge_sorting
71 $attributes = $c->dbic_merge_sorting({ attributes => $attributes, params => $params });
73 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$attributes>.
78 'dbic_merge_sorting' => sub {
79 my ( $c, $args ) = @_;
80 my $attributes = $args->{attributes};
83 map { _build_order_atom($_) }
84 split( /\|/, $args->{params}->{_order_by} );
86 $attributes->{order_by} = \@order_by;
92 =head2 Internal methods
94 =head3 _reserved_words
96 my $reserved_words = _reserved_words();
100 sub _reserved_words {
102 my @reserved_words = qw( _match _order_by _page _per_page );
103 return \@reserved_words;
106 =head3 _build_order_atom
108 my $order_atom = _build_order_atom( $string );
110 Parses I<$string> and outputs data valid for using in SQL::Abstract order_by attribute
111 according to the following rules:
114 +string -> I<{ -asc => string }>
115 -string -> I<{ -desc => string }>
119 sub _build_order_atom {
122 if ( $string =~ m/^\+/ ) {
123 # asc order operator present
125 return { -asc => $string };
127 elsif ( $string =~ m/^\-/ ) {
128 # desc order operator present
130 return { -desc => $string };
133 # no order operator present