Bug 6836: Add jQuery dataTables plugin
[koha_gimpoz] / C4 / Utils / DataTables.pm
1 package C4::Utils::DataTables;
2
3 # Copyright 2011 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21 require Exporter;
22
23 use vars qw($VERSION @ISA @EXPORT);
24
25 BEGIN {
26     $VERSION    = 3.04,
27
28     @ISA        = qw(Exporter);
29     @EXPORT     = qw(dt_build_orderby dt_build_having dt_get_params dt_build_query);
30 }
31
32 =head1 NAME
33
34 C4::Utils::DataTables - Utility subs for building query when DataTables source is AJAX
35
36 =head1 SYNOPSYS
37
38     use CGI;
39     use C4::Context;
40     use C4::Utils::DataTables;
41
42     my $input = new CGI;
43     my $vars = $input->Vars;
44
45     my $query = qq{
46         SELECT surname, firstname
47         FROM borrowers
48         WHERE borrowernumber = ?
49     };
50     my ($having, $having_params) = dt_build_having($vars);
51     $query .= $having;
52     $query .= dt_build_orderby($vars);
53     $query .= " LIMIT ?,? ";
54
55     my $dbh = C4::Context->dbh;
56     my $sth = $dbh->prepare($query);
57     $sth->execute(
58         $vars->{'borrowernumber'},
59         @$having_params,
60         $vars->{'iDisplayStart'},
61         $vars->{'iDisplayLength'}
62     );
63     ...
64
65 =head1 DESCRIPTION
66
67     This module provide two utility functions to build a part of the SQL query,
68     depending on DataTables parameters.
69     One function build the 'ORDER BY' part, and the other the 'HAVING' part.
70
71 =head1 FUNCTIONS
72
73 =over 2
74
75 =item dt_build_orderby
76
77     my $orderby = dt_build_orderby($dt_param);
78
79     This function takes a reference to a hash containing DataTables parameters
80     and build the corresponding 'ORDER BY' clause.
81     This hash must contains the following keys:
82
83         iSortCol_N, where N is a number from 0 to the number of columns to sort on minus 1
84
85         sSortDir_N is the sorting order ('asc' or 'desc) for the corresponding column
86
87         mDataProp_N is a mapping between the column index, and the name of a SQL field
88
89 =cut
90
91 sub dt_build_orderby {
92     my $param = shift;
93
94     my $i = 0;
95     my $orderby;
96     my @orderbys;
97     while(exists $param->{'iSortCol_'.$i}){
98         my $iSortCol = $param->{'iSortCol_'.$i};
99         my $sSortDir = $param->{'sSortDir_'.$i};
100         my $mDataProp = $param->{'mDataProp_'.$iSortCol};
101         my @sort_fields = $param->{$mDataProp.'_sorton'}
102             ? split(' ', $param->{$mDataProp.'_sorton'})
103             : ();
104         if(@sort_fields > 0) {
105             push @orderbys, "$_ $sSortDir" foreach (@sort_fields);
106         } else {
107             push @orderbys, "$mDataProp $sSortDir";
108         }
109         $i++;
110     }
111
112     $orderby = " ORDER BY " . join(',', @orderbys) . " " if @orderbys;
113     return $orderby;
114 }
115
116 =item dt_build_having
117
118     my ($having, $having_params) = dt_build_having($dt_params)
119
120     This function takes a reference to a hash containing DataTables parameters
121     and build the corresponding 'HAVING' clause.
122     This hash must contains the following keys:
123
124         sSearch is the text entered in the global filter
125
126         iColumns is the number of columns
127
128         bSearchable_N is a boolean value that is true if the column is searchable
129
130         mDataProp_N is a mapping between the column index, and the name of a SQL field
131
132         sSearch_N is the text entered in individual filter for column N
133
134 =back
135
136 =cut
137
138 sub dt_build_having {
139     my $param = shift;
140
141     my @filters;
142     my @params;
143
144     # Global filter
145     if($param->{'sSearch'}) {
146         my $sSearch = $param->{'sSearch'};
147         my $i = 0;
148         my @gFilters;
149         my @gParams;
150         while($i < $param->{'iColumns'}) {
151             if($param->{'bSearchable_'.$i} eq 'true') {
152                 my $mDataProp = $param->{'mDataProp_'.$i};
153                 my @filter_fields = $param->{$mDataProp.'_filteron'}
154                     ? split(' ', $param->{$mDataProp.'_filteron'})
155                     : ();
156                 if(@filter_fields > 0) {
157                     foreach my $field (@filter_fields) {
158                         push @gFilters, " $field LIKE ? ";
159                         push @gParams, "%$sSearch%";
160                     }
161                 } else {
162                     push @gFilters, " $mDataProp LIKE ? ";
163                     push @gParams, "%$sSearch%";
164                 }
165             }
166             $i++;
167         }
168         push @filters, " (" . join(" OR ", @gFilters) . ") ";
169         push @params, @gParams;
170     }
171
172     # Individual filters
173     my $i = 0;
174     while($i < $param->{'iColumns'}) {
175         my $sSearch = $param->{'sSearch_'.$i};
176         if($sSearch) {
177             my $mDataProp = $param->{'mDataProp_'.$i};
178             my @filter_fields = $param->{$mDataProp.'_filteron'}
179                 ? split(' ', $param->{$mDataProp.'_filteron'})
180                 : ();
181             if(@filter_fields > 0) {
182                 my @localfilters;
183                 foreach my $field (@filter_fields) {
184                     push @localfilters, " $field LIKE ? ";
185                     push @params, "%$sSearch%";
186                 }
187                 push @filters, " ( ". join(" OR ", @localfilters) ." ) ";
188             } else {
189                 push @filters, " $mDataProp LIKE ? ";
190                 push @params, "%$sSearch%";
191             }
192         }
193         $i++;
194     }
195
196     return (\@filters, \@params);
197 }
198
199 =item dt_get_params
200
201     my %dtparam = = dt_get_params( $input )
202
203     This function takes a reference to a new CGI object.
204
205     It prepares a hash containing Datatable parameters.
206
207 =back
208
209 =cut
210 sub dt_get_params {
211     my $input = shift;
212     my %dtparam;
213     my $vars = $input->Vars;
214
215     foreach(qw/ iDisplayStart iDisplayLength iColumns sSearch bRegex iSortingCols sEcho /) {
216         $dtparam{$_} = $input->param($_);
217     }
218     foreach(grep /(?:_sorton|_filteron)$/, keys %$vars) {
219         $dtparam{$_} = $vars->{$_};
220     }
221     for(my $i=0; $i<$dtparam{'iColumns'}; $i++) {
222         foreach(qw/ bSearchable sSearch bRegex bSortable iSortCol mDataProp sSortDir /) {
223             my $key = $_ . '_' . $i;
224             $dtparam{$key} = $input->param($key) if defined $input->param($key);
225         }
226     }
227     return %dtparam;
228 }
229
230 =item dt_build_query_simple
231
232     my ( $query, $params )= dt_build_query_simple( $value, $field )
233
234     This function takes a value and a field (table.field).
235
236     It returns (undef, []) if not $value.
237     Else, returns a SQL where string and an arrayref containing parameters
238     for the execute method of the statement.
239
240 =back
241
242 =cut
243 sub dt_build_query_simple {
244     my ( $value, $field ) = @_;
245     my $query;
246     my @params;
247     if( $value ) {
248         $query .= " AND $field = ? ";
249         push @params, $value;
250     }
251     return ( $query, \@params );
252 }
253
254 =item dt_build_query_dates
255
256     my ( $query, $params )= dt_build_query_dates( $datefrom, $dateto, $field)
257
258     This function takes a datefrom, dateto and a field (table.field).
259
260     It returns (undef, []) if not $value.
261     Else, returns a SQL where string and an arrayref containing parameters
262     for the execute method of the statement.
263
264 =back
265
266 =cut
267 sub dt_build_query_dates {
268     my ( $datefrom, $dateto, $field ) = @_;
269     my $query;
270     my @params;
271     if ( $datefrom ) {
272         $query .= " AND $field >= ? ";
273         push @params, C4::Dates->new($datefrom)->output('iso');
274     }
275     if ( $dateto ) {
276         $query .= " AND $field <= ? ";
277         push @params, C4::Dates->new($dateto)->output('iso');
278     }
279     return ( $query, \@params );
280 }
281
282 =item dt_build_query
283
284     my ( $query, $filter ) = dt_build_query( $type, @params )
285
286     This function takes a value and a list of parameters.
287
288     It calls dt_build_query_dates or dt_build_query_simple fonction of $type.
289
290     $type can be 'simple' or 'rage_dates'.
291
292 =back
293
294 =cut
295 sub dt_build_query {
296     my ( $type, @params ) = @_;
297     given ( $type ) {
298         when ( /simple/ ) {
299             return dt_build_query_simple( @params );
300         }
301         when ( /range_dates/ ) {
302             return dt_build_query_dates( @params );
303         }
304     }
305 }
306
307 1;