Bug 13457: Suggestions.t expects sample itemtypes
[koha.git] / C4 / Utils / DataTables.pm
blob7f62910e8a9630ce264d4359c946ae3c7f12bead
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
10 # version.
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.
20 use Modern::Perl;
21 require Exporter;
23 use vars qw($VERSION @ISA @EXPORT);
25 BEGIN {
26 $VERSION = 3.07.00.049,
28 @ISA = qw(Exporter);
29 @EXPORT = qw(dt_build_orderby dt_build_having dt_get_params dt_build_query);
32 =head1 NAME
34 C4::Utils::DataTables - Utility subs for building query when DataTables source is AJAX
36 =head1 SYNOPSYS
38 use CGI;
39 use C4::Context;
40 use C4::Utils::DataTables;
42 my $input = new CGI;
43 my $vars = $input->Vars;
45 my $query = qq{
46 SELECT surname, firstname
47 FROM borrowers
48 WHERE borrowernumber = ?
50 my ($having, $having_params) = dt_build_having($vars);
51 $query .= $having;
52 $query .= dt_build_orderby($vars);
53 $query .= " LIMIT ?,? ";
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'}
63 ...
65 =head1 DESCRIPTION
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.
71 =head1 FUNCTIONS
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
83 =cut
85 sub dt_build_orderby {
86 my $param = shift;
88 my $i = 0;
89 my $orderby;
90 my @orderbys;
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'})
97 : ();
98 if(@sort_fields > 0) {
99 push @orderbys, "$_ $sSortDir" foreach (@sort_fields);
100 } else {
101 push @orderbys, "$mDataProp $sSortDir";
103 $i++;
106 $orderby = " ORDER BY " . join(',', @orderbys) . " " if @orderbys;
107 return $orderby;
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
123 =cut
125 sub dt_build_having {
126 my $param = shift;
128 my @filters;
129 my @params;
131 # Global filter
132 if($param->{'sSearch'}) {
133 my $sSearch = $param->{'sSearch'};
134 my $i = 0;
135 my @gFilters;
136 my @gParams;
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'})
142 : ();
143 if(@filter_fields > 0) {
144 foreach my $field (@filter_fields) {
145 push @gFilters, " $field LIKE ? ";
146 push @gParams, "%$sSearch%";
148 } else {
149 push @gFilters, " $mDataProp LIKE ? ";
150 push @gParams, "%$sSearch%";
153 $i++;
155 push @filters, " (" . join(" OR ", @gFilters) . ") ";
156 push @params, @gParams;
159 # Individual filters
160 my $i = 0;
161 while($i < $param->{'iColumns'}) {
162 my $sSearch = $param->{'sSearch_'.$i};
163 if($sSearch) {
164 my $mDataProp = $param->{'mDataProp_'.$i};
165 my @filter_fields = $param->{$mDataProp.'_filteron'}
166 ? split(' ', $param->{$mDataProp.'_filteron'})
167 : ();
168 if(@filter_fields > 0) {
169 my @localfilters;
170 foreach my $field (@filter_fields) {
171 push @localfilters, " $field LIKE ? ";
172 push @params, "%$sSearch%";
174 push @filters, " ( ". join(" OR ", @localfilters) ." ) ";
175 } else {
176 push @filters, " $mDataProp LIKE ? ";
177 push @params, "%$sSearch%";
180 $i++;
183 return (\@filters, \@params);
186 =head2 dt_get_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.
192 =cut
193 sub dt_get_params {
194 my $input = shift;
195 my %dtparam;
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);
210 return %dtparam;
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.
223 =cut
224 sub dt_build_query_simple {
225 my ( $value, $field ) = @_;
226 my $query;
227 my @params;
228 if( $value ) {
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.
245 =cut
246 sub dt_build_query_dates {
247 my ( $datefrom, $dateto, $field ) = @_;
248 my $query;
249 my @params;
250 if ( $datefrom ) {
251 $query .= " AND $field >= ? ";
252 push @params, C4::Dates->new($datefrom)->output('iso');
254 if ( $dateto ) {
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
272 =cut
273 sub dt_build_query {
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);
281 return;