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 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
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.
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.
23 use vars
qw($VERSION @ISA @EXPORT);
26 $VERSION = 3.07.00.049,
29 @EXPORT = qw(dt_build_orderby dt_build_having dt_get_params dt_build_query);
34 C4::Utils::DataTables - Utility subs for building query when DataTables source is AJAX
40 use C4::Utils::DataTables;
43 my $vars = $input->Vars;
46 SELECT surname, firstname
48 WHERE borrowernumber = ?
50 my ($having, $having_params) = dt_build_having($vars);
52 $query .= dt_build_orderby($vars);
53 $query .= " LIMIT ?,? ";
55 my $dbh = C4::Context->dbh;
56 my $sth = $dbh->prepare($query);
58 $vars->{'borrowernumber'},
60 $vars->{'iDisplayStart'},
61 $vars->{'iDisplayLength'}
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.
73 =head2 dt_build_orderby
75 my $orderby = dt_build_orderby($dt_param);
76 This function takes a reference to a hash containing DataTables parameters
77 and build the corresponding 'ORDER BY' clause.
78 This hash must contains the following keys:
79 iSortCol_N, where N is a number from 0 to the number of columns to sort on minus 1
80 sSortDir_N is the sorting order ('asc' or 'desc) for the corresponding column
81 mDataProp_N is a mapping between the column index, and the name of a SQL field
85 sub dt_build_orderby
{
91 while(exists $param->{'iSortCol_'.$i}){
92 my $iSortCol = $param->{'iSortCol_'.$i};
93 my $sSortDir = $param->{'sSortDir_'.$i};
94 my $mDataProp = $param->{'mDataProp_'.$iSortCol};
95 my @sort_fields = $param->{$mDataProp.'_sorton'}
96 ?
split(' ', $param->{$mDataProp.'_sorton'})
98 if(@sort_fields > 0) {
99 push @orderbys, "$_ $sSortDir" foreach (@sort_fields);
101 push @orderbys, "$mDataProp $sSortDir";
106 $orderby = " ORDER BY " . join(',', @orderbys) . " " if @orderbys;
110 =head2 dt_build_having
112 my ($having, $having_params) = dt_build_having($dt_params)
114 This function takes a reference to a hash containing DataTables parameters
115 and build the corresponding 'HAVING' clause.
116 This hash must contains the following keys:
117 sSearch is the text entered in the global filter
118 iColumns is the number of columns
119 bSearchable_N is a boolean value that is true if the column is searchable
120 mDataProp_N is a mapping between the column index, and the name of a SQL field
121 sSearch_N is the text entered in individual filter for column N
125 sub dt_build_having
{
132 if($param->{'sSearch'}) {
133 my $sSearch = $param->{'sSearch'};
137 while($i < $param->{'iColumns'}) {
138 if($param->{'bSearchable_'.$i} eq 'true') {
139 my $mDataProp = $param->{'mDataProp_'.$i};
140 my @filter_fields = $param->{$mDataProp.'_filteron'}
141 ?
split(' ', $param->{$mDataProp.'_filteron'})
143 if(@filter_fields > 0) {
144 foreach my $field (@filter_fields) {
145 push @gFilters, " $field LIKE ? ";
146 push @gParams, "%$sSearch%";
149 push @gFilters, " $mDataProp LIKE ? ";
150 push @gParams, "%$sSearch%";
155 push @filters, " (" . join(" OR ", @gFilters) . ") ";
156 push @params, @gParams;
161 while($i < $param->{'iColumns'}) {
162 my $sSearch = $param->{'sSearch_'.$i};
164 my $mDataProp = $param->{'mDataProp_'.$i};
165 my @filter_fields = $param->{$mDataProp.'_filteron'}
166 ?
split(' ', $param->{$mDataProp.'_filteron'})
168 if(@filter_fields > 0) {
170 foreach my $field (@filter_fields) {
171 push @localfilters, " $field LIKE ? ";
172 push @params, "%$sSearch%";
174 push @filters, " ( ". join(" OR ", @localfilters) ." ) ";
176 push @filters, " $mDataProp LIKE ? ";
177 push @params, "%$sSearch%";
183 return (\
@filters, \
@params);
188 my %dtparam = = dt_get_params( $input )
189 This function takes a reference to a new CGI object.
190 It prepares a hash containing Datatable parameters.
196 my $vars = $input->Vars;
198 foreach(qw
/ iDisplayStart iDisplayLength iColumns sSearch bRegex iSortingCols sEcho /) {
199 $dtparam{$_} = $input->param($_);
201 foreach(grep /(?:_sorton|_filteron)$/, keys %$vars) {
202 $dtparam{$_} = $vars->{$_};
204 for(my $i=0; $i<$dtparam{'iColumns'}; $i++) {
205 foreach(qw
/ bSearchable sSearch bRegex bSortable iSortCol mDataProp sSortDir /) {
206 my $key = $_ . '_' . $i;
207 $dtparam{$key} = $input->param($key) if defined $input->param($key);
213 =head2 dt_build_query_simple
215 my ( $query, $params )= dt_build_query_simple( $value, $field )
217 This function takes a value and a field (table.field).
219 It returns (undef, []) if not $value.
220 Else, returns a SQL where string and an arrayref containing parameters
221 for the execute method of the statement.
224 sub dt_build_query_simple
{
225 my ( $value, $field ) = @_;
229 $query .= " AND $field = ? ";
230 push @params, $value;
232 return ( $query, \
@params );
235 =head2 dt_build_query_dates
237 my ( $query, $params )= dt_build_query_dates( $datefrom, $dateto, $field)
239 This function takes a datefrom, dateto and a field (table.field).
241 It returns (undef, []) if not $value.
242 Else, returns a SQL where string and an arrayref containing parameters
243 for the execute method of the statement.
246 sub dt_build_query_dates
{
247 my ( $datefrom, $dateto, $field ) = @_;
251 $query .= " AND $field >= ? ";
252 push @params, C4
::Dates
->new($datefrom)->output('iso');
255 $query .= " AND $field <= ? ";
256 push @params, C4
::Dates
->new($dateto)->output('iso');
258 return ( $query, \
@params );
261 =head2 dt_build_query
263 my ( $query, $filter ) = dt_build_query( $type, @params )
265 This function takes a value and a list of parameters.
267 It calls dt_build_query_dates or dt_build_query_simple function of $type.
269 $type can contain 'simple' or 'range_dates'.
270 if $type is not matched it returns undef
274 my ( $type, @params ) = @_;
275 if ( $type =~ m/simple/ ) {
276 return dt_build_query_simple
(@params);
278 elsif ( $type =~ m/range_dates/ ) {
279 return dt_build_query_dates
(@params);