Bug 27921: Log correct timestamp for HOLD MODIFY when set waiting
[koha.git] / 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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21 require Exporter;
22
23 use vars qw(@ISA @EXPORT);
24
25 BEGIN {
26
27     @ISA        = qw(Exporter);
28     @EXPORT     = qw(dt_build_orderby dt_get_params);
29 }
30
31 =head1 NAME
32
33 ! DEPRECATED - This module is deprecated, the REST API route and REST API Datatables wrapper must be used instead!
34
35 C4::Utils::DataTables - Utility subs for building query when DataTables source is AJAX
36
37 =head1 SYNOPSYS
38
39     use CGI qw ( -utf8 );
40     use C4::Context;
41     use C4::Utils::DataTables;
42
43     my $input = new CGI;
44     my $vars = $input->Vars;
45
46     my $query = qq{
47         SELECT surname, firstname
48         FROM borrowers
49         WHERE borrowernumber = ?
50     };
51     $query .= dt_build_orderby($vars);
52     $query .= " LIMIT ?,? ";
53
54     my $dbh = C4::Context->dbh;
55     my $sth = $dbh->prepare($query);
56     $sth->execute(
57         $vars->{'borrowernumber'},
58         @$having_params,
59         $vars->{'iDisplayStart'},
60         $vars->{'iDisplayLength'}
61     );
62     ...
63
64 =head1 DESCRIPTION
65
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.
69
70 =head1 FUNCTIONS
71
72 =head2 dt_build_orderby
73
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
81
82 =cut
83
84 sub dt_build_orderby {
85     my $param = shift;
86
87     my $i = 0;
88     my @orderbys;
89     my $dbh = C4::Context->dbh;
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'})
96             : ();
97         if(@sort_fields > 0) {
98             push @orderbys, "$_ $sSortDir" foreach (@sort_fields);
99         } else {
100             push @orderbys, "$mDataProp $sSortDir";
101         }
102         $i++;
103     }
104
105     return unless @orderbys;
106
107     # Must be "branches.branchname asc", "borrowers.firstname desc", etc.
108     @orderbys = grep { /^\w+\.\w+ (asc|desc)$/ } @orderbys;
109
110     my @sanitized_orderbys;
111     for my $orderby (@orderbys) {
112       my ($identifier, $direction) = split / /, $orderby, 2;
113       my ($table, $column) = split /\./, $identifier, 2;
114       my $sanitized_identifier = $dbh->quote_identifier(undef, $table, $column);
115       my $sanitized_direction = $direction eq 'asc' ? 'ASC' : 'DESC';
116       push @sanitized_orderbys, "$sanitized_identifier $sanitized_direction";
117     }
118
119     return " ORDER BY " . join(',', @sanitized_orderbys) . " " if @sanitized_orderbys;
120 }
121
122 =head2 dt_get_params
123
124     my %dtparam = = dt_get_params( $input )
125     This function takes a reference to a new CGI object.
126     It prepares a hash containing Datatable parameters.
127
128 =cut
129 sub dt_get_params {
130     my $input = shift;
131     my %dtparam;
132     my $vars = $input->Vars;
133
134     foreach(qw/ iDisplayStart iDisplayLength iColumns sSearch bRegex iSortingCols sEcho /) {
135         $dtparam{$_} = $input->param($_);
136     }
137     foreach(grep /(?:_sorton|_filteron)$/, keys %$vars) {
138         $dtparam{$_} = $vars->{$_};
139     }
140     for(my $i=0; $i<$dtparam{'iColumns'}; $i++) {
141         foreach(qw/ bSearchable sSearch bRegex bSortable iSortCol mDataProp sSortDir /) {
142             my $key = $_ . '_' . $i;
143             $dtparam{$key} = $input->param($key) if defined $input->param($key);
144         }
145     }
146     return %dtparam;
147 }
148
149 1;