View | Details | Raw Unified | Return to bug 27715
Collapse All | Expand All

(-)a/C4/Utils/DataTables.pm (-80 / +1 lines)
Lines 25-31 use vars qw(@ISA @EXPORT); Link Here
25
BEGIN {
25
BEGIN {
26
26
27
    @ISA        = qw(Exporter);
27
    @ISA        = qw(Exporter);
28
    @EXPORT     = qw(dt_build_orderby dt_build_having dt_get_params);
28
    @EXPORT     = qw(dt_build_orderby dt_get_params);
29
}
29
}
30
30
31
=head1 NAME
31
=head1 NAME
Lines 46-53 C4::Utils::DataTables - Utility subs for building query when DataTables source i Link Here
46
        FROM borrowers
46
        FROM borrowers
47
        WHERE borrowernumber = ?
47
        WHERE borrowernumber = ?
48
    };
48
    };
49
    my ($having, $having_params) = dt_build_having($vars);
50
    $query .= $having;
51
    $query .= dt_build_orderby($vars);
49
    $query .= dt_build_orderby($vars);
52
    $query .= " LIMIT ?,? ";
50
    $query .= " LIMIT ?,? ";
53
51
Lines 106-187 sub dt_build_orderby { Link Here
106
    return $orderby;
104
    return $orderby;
107
}
105
}
108
106
109
=head2 dt_build_having
110
111
    my ($having, $having_params) = dt_build_having($dt_params)
112
113
    This function takes a reference to a hash containing DataTables parameters
114
    and build the corresponding 'HAVING' clause.
115
    This hash must contains the following keys:
116
        sSearch is the text entered in the global filter
117
        iColumns is the number of columns
118
        bSearchable_N is a boolean value that is true if the column is searchable
119
        mDataProp_N is a mapping between the column index, and the name of a SQL field
120
        sSearch_N is the text entered in individual filter for column N
121
122
=cut
123
124
sub dt_build_having {
125
    my $param = shift;
126
127
    my @filters;
128
    my @params;
129
130
    # Global filter
131
    if($param->{'sSearch'}) {
132
        my $sSearch = $param->{'sSearch'};
133
        my $i = 0;
134
        my @gFilters;
135
        my @gParams;
136
        while($i < $param->{'iColumns'}) {
137
            if($param->{'bSearchable_'.$i} eq 'true') {
138
                my $mDataProp = $param->{'mDataProp_'.$i};
139
                my @filter_fields = $param->{$mDataProp.'_filteron'}
140
                    ? split(' ', $param->{$mDataProp.'_filteron'})
141
                    : ();
142
                if(@filter_fields > 0) {
143
                    foreach my $field (@filter_fields) {
144
                        push @gFilters, " $field LIKE ? ";
145
                        push @gParams, "%$sSearch%";
146
                    }
147
                } else {
148
                    push @gFilters, " $mDataProp LIKE ? ";
149
                    push @gParams, "%$sSearch%";
150
                }
151
            }
152
            $i++;
153
        }
154
        push @filters, " (" . join(" OR ", @gFilters) . ") ";
155
        push @params, @gParams;
156
    }
157
158
    # Individual filters
159
    my $i = 0;
160
    while($i < $param->{'iColumns'}) {
161
        my $sSearch = $param->{'sSearch_'.$i};
162
        if($sSearch) {
163
            my $mDataProp = $param->{'mDataProp_'.$i};
164
            my @filter_fields = $param->{$mDataProp.'_filteron'}
165
                ? split(' ', $param->{$mDataProp.'_filteron'})
166
                : ();
167
            if(@filter_fields > 0) {
168
                my @localfilters;
169
                foreach my $field (@filter_fields) {
170
                    push @localfilters, " $field LIKE ? ";
171
                    push @params, "%$sSearch%";
172
                }
173
                push @filters, " ( ". join(" OR ", @localfilters) ." ) ";
174
            } else {
175
                push @filters, " $mDataProp LIKE ? ";
176
                push @params, "%$sSearch%";
177
            }
178
        }
179
        $i++;
180
    }
181
182
    return (\@filters, \@params);
183
}
184
185
=head2 dt_get_params
107
=head2 dt_get_params
186
108
187
    my %dtparam = = dt_get_params( $input )
109
    my %dtparam = = dt_get_params( $input )
188
- 

Return to bug 27715