Bug 23777: Use To.json in results.tt
[koha.git] / reports / dictionary.pl
blobbca95ce0bde57e6f0ae31c4c7616e00b1d5a7074
1 #!/usr/bin/perl
3 # Copyright 2007 Liblime ltd
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>.
19 use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
20 use Modern::Perl;
21 use C4::Auth;
22 use CGI qw ( -utf8 );
23 use C4::Output;
24 use C4::Reports::Guided;
25 use Koha::DateUtils;
27 =head1 NAME
29 Script to control the guided report creation
31 =head1 DESCRIPTION
33 =cut
35 my $input = new CGI;
36 my $referer = $input->referer();
38 my $phase = $input->param('phase') || 'View Dictionary';
39 my $definition_name = $input->param('definition_name');
40 my $definition_description = $input->param('definition_description');
41 my $area = $input->param('area') || '';
42 my $no_html = 0; # this will be set if we dont want to print out an html::template
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44 { template_name => "reports/dictionary.tt",
45 query => $input,
46 type => "intranet",
47 authnotrequired => 0,
48 flagsrequired => { reports => '*' },
49 debug => 1,
53 if ($phase eq 'View Dictionary'){
54 # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
55 my $definitions = get_from_dictionary($area);
56 $template->param(
57 'areas' => areas( $area ),
58 'start_dictionary' => 1,
59 'definitions' => $definitions,
61 } elsif ( $phase eq 'Add New Definition' ) {
63 # display form allowing them to add a new definition
64 $template->param( 'new_dictionary' => 1, );
67 elsif ( $phase eq 'New Term step 2' ) {
69 # Choosing the area
70 $template->param(
71 'step_2' => 1,
72 'areas' => areas( $area ),
73 'definition_name' => $definition_name,
74 'definition_description' => $definition_description,
78 elsif ( $phase eq 'New Term step 3' ) {
80 # Choosing the columns
81 my $columns = get_columns( $area, $input );
82 $template->param(
83 'step_3' => 1,
84 'area' => $area,
85 'columns' => $columns,
86 'definition_name' => $definition_name,
87 'definition_description' => $definition_description,
91 elsif ( $phase eq 'New Term step 4' ) {
93 # Choosing the values
94 my @columns = $input->multi_param('columns');
95 my $columnstring = join( ',', @columns );
96 my @column_loop;
97 foreach my $column (@columns) {
98 my %tmp_hash;
99 $tmp_hash{'name'} = $column;
100 my $type = get_column_type($column);
101 if ( $type eq 'distinct' ) {
102 my $values = get_distinct_values($column);
103 $tmp_hash{'values'} = $values;
104 $tmp_hash{'distinct'} = 1;
107 if ( $type eq 'DATE' || $type eq 'DATETIME' ) {
108 $tmp_hash{'date'} = 1;
110 if ($type eq 'TEXT' || $type eq 'MEDIUMTEXT'){
111 $tmp_hash{'text'} = 1;
114 # else {
115 # warn $type;#
117 push @column_loop, \%tmp_hash;
120 $template->param( 'step_4' => 1,
121 'area' => $area,
122 'definition_name' => $definition_name,
123 'definition_description' => $definition_description,
124 'columns' => \@column_loop,
125 'columnstring' => $columnstring,
129 elsif ( $phase eq 'New Term step 5' ) {
130 # Confirmation screen
131 my $columnstring = $input->param('columnstring');
132 my @criteria = $input->multi_param('criteria_column');
133 my $query_criteria;
134 my @criteria_loop;
136 foreach my $crit (@criteria) {
137 my $value = $input->param( $crit . "_value" );
138 if ($value) {
139 my %tmp_hash;
140 $tmp_hash{'name'} = $crit;
141 $tmp_hash{'value'} = $value;
142 push @criteria_loop, \%tmp_hash;
143 my $value_dt = eval { dt_from_string( $value ) };
144 $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
145 if ( $value_dt );
147 $query_criteria .= " AND $crit='$value'";
150 if ( my $date_type_value = $input->param( $crit . "_date_type_value" ) ) {
151 if ( $date_type_value eq 'range' ) {
152 if ( $value = $input->param( $crit . "_start_value" ) ) {
153 my %tmp_hash;
154 $tmp_hash{'name'} = "$crit Start";
155 $tmp_hash{'value'} = $value;
156 push @criteria_loop, \%tmp_hash;
157 my $value_dt = eval { dt_from_string( $value ) };
158 $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
159 if ( $value_dt );
161 $query_criteria .= " AND $crit >= '$value'";
164 if ( $value = $input->param( $crit . "_end_value" ) ) {
165 my %tmp_hash;
166 $tmp_hash{'name'} = "$crit End";
167 $tmp_hash{'value'} = $value;
168 push @criteria_loop, \%tmp_hash;
169 my $value_dt = eval { dt_from_string( $value ) };
170 $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
171 if ( $value_dt );
173 $query_criteria .= " AND $crit <= '$value'";
176 # else we want all dates
179 $template->param(
180 'step_5' => 1,
181 'area' => $area,
182 'definition_name' => $definition_name,
183 'definition_description' => $definition_description,
184 'query' => $query_criteria,
185 'columnstring' => $columnstring,
186 'criteria_loop' => \@criteria_loop,
190 elsif ( $phase eq 'New Term step 6' ) {
191 # Saving
192 my $area = $input->param('area');
193 my $sql = $input->param('sql');
194 save_dictionary( $definition_name, $definition_description, $sql, $area );
195 $no_html = 1;
196 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
198 } elsif ( $phase eq 'Delete Definition' ) {
199 $no_html = 1;
200 my $id = $input->param('id');
201 delete_definition($id);
202 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
205 $template->param( 'referer' => $referer );
208 if (!$no_html){
209 output_html_with_http_headers $input, $cookie, $template->output;
212 sub areas {
214 my $selected = shift;
216 my $areas = get_report_areas();
217 my @a;
218 foreach my $area ( @$areas ) {
219 push @a, {
220 id => $area,
221 selected => ( $area eq $selected )
225 return \@a;