Bug 20473: Whitespace
[koha.git] / Koha / SearchFilter.pm
1 package Koha::SearchFilter;
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
15 # along with Koha; if not, see <http://www.gnu.org/licenses>.
16
17 use Modern::Perl;
18 use JSON qw( encode_json decode_json );
19
20 use Koha::Database;
21
22 use base qw(Koha::Object);
23
24 =head1 NAME
25
26 Koha::SearchFilter - Koha Search filter object class
27
28 =head1 API
29
30 =head2 Class methods
31
32 =head3 expand_filter
33
34     my ($expanded_limit, $query_limit) = $filter->expand_filter;
35
36     Returns the filter as an arrayref of limit queries, and the query parts combined
37     into a string suitable to be passed to QueryBuilder
38
39 =cut
40
41 sub expand_filter {
42     my $self = shift;
43
44     my $query_part = $self->query;
45     my $limits_part = $self->limits;
46
47     my $limits = decode_json($limits_part)->{limits};
48
49     my $query = decode_json($query_part);
50     my $operators = $query->{operators};
51     my $operands = $query->{operands};
52     my $indexes = $query->{indexes};
53
54     my $query_limit;
55     for( my $i = 0; $i < scalar @$operands; $i++ ){
56         next unless @$operands[$i];
57         my $index = @$indexes[$i] ? @$indexes[$i] . "=" : "";
58         my $query = "(" . @$operands[$i] . ")";
59         my $operator = "";
60         $operator = @$operators[$i-1] ? " " . @$operators[$i-1] . " " : scalar @$operands > $i ? " AND " : "" if $i > 0;
61         my $limit = $operator . $index . $query;
62         $query_limit .= $limit;
63     }
64
65     return ($limits, $query_limit);
66 }
67
68 =head2 Internal methods
69
70 =head3 _type
71
72 =cut
73
74 sub _type {
75     return 'SearchFilter';
76 }
77
78 1;