Bug 27715: Add a deprecation notice
[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 $orderby;
89     my @orderbys;
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+\s(asc|desc)$/ } @orderbys;
109
110     $orderby = " ORDER BY " . join(',', @orderbys) . " " if @orderbys;
111     return $orderby;
112 }
113
114 =head2 dt_get_params
115
116     my %dtparam = = dt_get_params( $input )
117     This function takes a reference to a new CGI object.
118     It prepares a hash containing Datatable parameters.
119
120 =cut
121 sub dt_get_params {
122     my $input = shift;
123     my %dtparam;
124     my $vars = $input->Vars;
125
126     foreach(qw/ iDisplayStart iDisplayLength iColumns sSearch bRegex iSortingCols sEcho /) {
127         $dtparam{$_} = $input->param($_);
128     }
129     foreach(grep /(?:_sorton|_filteron)$/, keys %$vars) {
130         $dtparam{$_} = $vars->{$_};
131     }
132     for(my $i=0; $i<$dtparam{'iColumns'}; $i++) {
133         foreach(qw/ bSearchable sSearch bRegex bSortable iSortCol mDataProp sSortDir /) {
134             my $key = $_ . '_' . $i;
135             $dtparam{$key} = $input->param($key) if defined $input->param($key);
136         }
137     }
138     return %dtparam;
139 }
140
141 1;