Bug 24321: Clean /biblios
[koha.git] / Koha / REST / Plugin / Query.pm
1 package Koha::REST::Plugin::Query;
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 use Scalar::Util qw(reftype);
22
23 use Koha::Exceptions;
24
25 =head1 NAME
26
27 Koha::REST::Plugin::Query
28
29 =head1 API
30
31 =head2 Mojolicious::Plugin methods
32
33 =head3 register
34
35 =cut
36
37 sub register {
38     my ( $self, $app ) = @_;
39
40 =head2 Helper methods
41
42 =head3 extract_reserved_params
43
44     my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($params);
45
46 Generates the DBIC query from the query parameters.
47
48 =cut
49
50     $app->helper(
51         'extract_reserved_params' => sub {
52             my ( $c, $params ) = @_;
53
54             my $reserved_params;
55             my $filtered_params;
56
57             my $reserved_words = _reserved_words();
58
59             foreach my $param ( keys %{$params} ) {
60                 if ( grep { $param eq $_ } @{$reserved_words} ) {
61                     $reserved_params->{$param} = $params->{$param};
62                 }
63                 else {
64                     $filtered_params->{$param} = $params->{$param};
65                 }
66             }
67
68             return ( $filtered_params, $reserved_params );
69         }
70     );
71
72 =head3 dbic_merge_sorting
73
74     $attributes = $c->dbic_merge_sorting({ attributes => $attributes, params => $params });
75
76 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$attributes>.
77
78 =cut
79
80     $app->helper(
81         'dbic_merge_sorting' => sub {
82             my ( $c, $args ) = @_;
83             my $attributes = $args->{attributes};
84             my $result_set = $args->{result_set};
85
86             if ( defined $args->{params}->{_order_by} ) {
87                 my $order_by = $args->{params}->{_order_by};
88                 if ( reftype($order_by) and reftype($order_by) eq 'ARRAY' ) {
89                     my @order_by = map { _build_order_atom({ string => $_, result_set => $result_set }) }
90                                 @{ $args->{params}->{_order_by} };
91                     $attributes->{order_by} = \@order_by;
92                 }
93                 else {
94                     $attributes->{order_by} = _build_order_atom({ string => $order_by, result_set => $result_set });
95                 }
96             }
97
98             return $attributes;
99         }
100     );
101
102 =head3 _build_query_params_from_api
103
104     my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
105
106 Builds the params for searching on DBIC based on the selected matching algorithm.
107 Valid options are I<contains>, I<starts_with>, I<ends_with> and I<exact>. Default is
108 I<contains>. If other value is passed, a Koha::Exceptions::WrongParameter exception
109 is raised.
110
111 =cut
112
113     $app->helper(
114         'build_query_params' => sub {
115
116             my ( $c, $filtered_params, $reserved_params ) = @_;
117
118             my $params;
119             my $match = $reserved_params->{_match} // 'contains';
120
121             foreach my $param ( keys %{$filtered_params} ) {
122                 if ( $match eq 'contains' ) {
123                     $params->{$param} =
124                       { like => '%' . $filtered_params->{$param} . '%' };
125                 }
126                 elsif ( $match eq 'starts_with' ) {
127                     $params->{$param} = { like => $filtered_params->{$param} . '%' };
128                 }
129                 elsif ( $match eq 'ends_with' ) {
130                     $params->{$param} = { like => '%' . $filtered_params->{$param} };
131                 }
132                 elsif ( $match eq 'exact' ) {
133                     $params->{$param} = $filtered_params->{$param};
134                 }
135                 else {
136                     # We should never reach here, because the OpenAPI plugin should
137                     # prevent invalid params to be passed
138                     Koha::Exceptions::WrongParameter->throw(
139                         "Invalid value for _match param ($match)");
140                 }
141             }
142
143             return $params;
144         }
145     );
146 }
147
148 =head2 Internal methods
149
150 =head3 _reserved_words
151
152     my $reserved_words = _reserved_words();
153
154 =cut
155
156 sub _reserved_words {
157
158     my @reserved_words = qw( _match _order_by _page _per_page );
159     return \@reserved_words;
160 }
161
162 =head3 _build_order_atom
163
164     my $order_atom = _build_order_atom( $string );
165
166 Parses I<$string> and outputs data valid for using in SQL::Abstract order_by attribute
167 according to the following rules:
168
169      string -> I<string>
170     +string -> I<{ -asc => string }>
171     -string -> I<{ -desc => string }>
172
173 =cut
174
175 sub _build_order_atom {
176     my ( $args )   = @_;
177     my $string     = $args->{string};
178     my $result_set = $args->{result_set};
179
180     my $param = $string;
181     $param =~ s/^(\+|\-|\s)//;
182     if ( $result_set ) {
183         $param = (keys %{$result_set->attributes_from_api({ $param => 1 })})[0];
184     }
185
186     if ( $string =~ m/^\+/ or
187          $string =~ m/^\s/ ) {
188         # asc order operator present
189         return { -asc => $param };
190     }
191     elsif ( $string =~ m/^\-/ ) {
192         # desc order operator present
193         return { -desc => $param };
194     }
195     else {
196         # no order operator present
197         return $param;
198     }
199 }
200
201 1;