1 package C4
::Utils
::DataTables
;
3 # Copyright 2011 BibLibre
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use vars
qw(@ISA @EXPORT);
28 @EXPORT = qw(dt_build_orderby dt_build_having dt_get_params dt_build_query);
33 C4::Utils::DataTables - Utility subs for building query when DataTables source is AJAX
39 use C4::Utils::DataTables;
42 my $vars = $input->Vars;
45 SELECT surname, firstname
47 WHERE borrowernumber = ?
49 my ($having, $having_params) = dt_build_having($vars);
51 $query .= dt_build_orderby($vars);
52 $query .= " LIMIT ?,? ";
54 my $dbh = C4::Context->dbh;
55 my $sth = $dbh->prepare($query);
57 $vars->{'borrowernumber'},
59 $vars->{'iDisplayStart'},
60 $vars->{'iDisplayLength'}
66 This module provide two utility functions to build a part of the SQL query,
67 depending on DataTables parameters.
68 One function build the 'ORDER BY' part, and the other the 'HAVING' part.
72 =head2 dt_build_orderby
74 my $orderby = dt_build_orderby($dt_param);
75 This function takes a reference to a hash containing DataTables parameters
76 and build the corresponding 'ORDER BY' clause.
77 This hash must contains the following keys:
78 iSortCol_N, where N is a number from 0 to the number of columns to sort on minus 1
79 sSortDir_N is the sorting order ('asc' or 'desc) for the corresponding column
80 mDataProp_N is a mapping between the column index, and the name of a SQL field
84 sub dt_build_orderby
{
90 while(exists $param->{'iSortCol_'.$i}){
91 my $iSortCol = $param->{'iSortCol_'.$i};
92 my $sSortDir = $param->{'sSortDir_'.$i};
93 my $mDataProp = $param->{'mDataProp_'.$iSortCol};
94 my @sort_fields = $param->{$mDataProp.'_sorton'}
95 ?
split(' ', $param->{$mDataProp.'_sorton'})
97 if(@sort_fields > 0) {
98 push @orderbys, "$_ $sSortDir" foreach (@sort_fields);
100 push @orderbys, "$mDataProp $sSortDir";
105 $orderby = " ORDER BY " . join(',', @orderbys) . " " if @orderbys;
109 =head2 dt_build_having
111 my ($having, $having_params) = dt_build_having($dt_params)
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
124 sub dt_build_having
{
131 if($param->{'sSearch'}) {
132 my $sSearch = $param->{'sSearch'};
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'})
142 if(@filter_fields > 0) {
143 foreach my $field (@filter_fields) {
144 push @gFilters, " $field LIKE ? ";
145 push @gParams, "%$sSearch%";
148 push @gFilters, " $mDataProp LIKE ? ";
149 push @gParams, "%$sSearch%";
154 push @filters, " (" . join(" OR ", @gFilters) . ") ";
155 push @params, @gParams;
160 while($i < $param->{'iColumns'}) {
161 my $sSearch = $param->{'sSearch_'.$i};
163 my $mDataProp = $param->{'mDataProp_'.$i};
164 my @filter_fields = $param->{$mDataProp.'_filteron'}
165 ?
split(' ', $param->{$mDataProp.'_filteron'})
167 if(@filter_fields > 0) {
169 foreach my $field (@filter_fields) {
170 push @localfilters, " $field LIKE ? ";
171 push @params, "%$sSearch%";
173 push @filters, " ( ". join(" OR ", @localfilters) ." ) ";
175 push @filters, " $mDataProp LIKE ? ";
176 push @params, "%$sSearch%";
182 return (\
@filters, \
@params);
187 my %dtparam = = dt_get_params( $input )
188 This function takes a reference to a new CGI object.
189 It prepares a hash containing Datatable parameters.
195 my $vars = $input->Vars;
197 foreach(qw
/ iDisplayStart iDisplayLength iColumns sSearch bRegex iSortingCols sEcho /) {
198 $dtparam{$_} = $input->param($_);
200 foreach(grep /(?:_sorton|_filteron)$/, keys %$vars) {
201 $dtparam{$_} = $vars->{$_};
203 for(my $i=0; $i<$dtparam{'iColumns'}; $i++) {
204 foreach(qw
/ bSearchable sSearch bRegex bSortable iSortCol mDataProp sSortDir /) {
205 my $key = $_ . '_' . $i;
206 $dtparam{$key} = $input->param($key) if defined $input->param($key);
212 =head2 dt_build_query_simple
214 my ( $query, $params )= dt_build_query_simple( $value, $field )
216 This function takes a value and a field (table.field).
218 It returns (undef, []) if not $value.
219 Else, returns a SQL where string and an arrayref containing parameters
220 for the execute method of the statement.
223 sub dt_build_query_simple
{
224 my ( $value, $field ) = @_;
228 $query .= " AND $field = ? ";
229 push @params, $value;
231 return ( $query, \
@params );
234 =head2 dt_build_query_dates
236 my ( $query, $params )= dt_build_query_dates( $datefrom, $dateto, $field)
238 This function takes a datefrom, dateto and a field (table.field).
240 It returns (undef, []) if not $value.
241 Else, returns a SQL where string and an arrayref containing parameters
242 for the execute method of the statement.
245 sub dt_build_query_dates
{
246 my ( $datefrom, $dateto, $field ) = @_;
250 $query .= " AND $field >= ? ";
251 push @params, eval { output_pref
( { dt
=> dt_from_string
( $datefrom ), dateonly
=> 1, dateformat
=> 'iso' } ); };
254 $query .= " AND $field <= ? ";
255 push @params, eval { output_pref
( { dt
=> dt_from_string
( $dateto ), dateonly
=> 1, dateformat
=> 'iso' } ); };
257 return ( $query, \
@params );
260 =head2 dt_build_query
262 my ( $query, $filter ) = dt_build_query( $type, @params )
264 This function takes a value and a list of parameters.
266 It calls dt_build_query_dates or dt_build_query_simple function of $type.
268 $type can contain 'simple' or 'range_dates'.
269 if $type is not matched it returns undef
273 my ( $type, @params ) = @_;
274 if ( $type =~ m/simple/ ) {
275 return dt_build_query_simple
(@params);
277 elsif ( $type =~ m/range_dates/ ) {
278 return dt_build_query_dates
(@params);