Fixing a bug where a cgi object wasnt being passed, in the reports dictionary
[koha.git] / reports / dictionary.pl
1 #!/usr/bin/perl
2
3 # Copyright 2007 Liblime ltd
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19 use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
20 use strict;
21 use C4::Auth;
22 use CGI;
23 use C4::Output;
24 use C4::Reports;
25
26 =head1 NAME
27
28 Script to control the guided report creation
29
30 =head1 DESCRIPTION
31
32
33 =over2
34
35 =cut
36
37 my $input = new CGI;
38 my $referer = $input->referer();
39
40 my $phase = $input->param('phase');
41 my $no_html = 0; # this will be set if we dont want to print out an html::template
42 my      ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43     {
44         template_name   => "reports/dictionary.tmpl",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { editcatalogue => 1 },
49         debug           => 1,
50     }
51         );
52
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 $areas = C4::Reports::get_report_areas();
56         my $definitions = get_from_dictionary();
57         $template->param( 'areas' => $areas ,
58                 'start_dictionary' => 1,
59                 'definitions' => $definitions,
60         );
61 }
62 elsif ($phase eq 'Add New Definition'){
63         # display form allowing them to add a new definition
64         $template->param( 'new_dictionary' => 1,
65                 );
66 }
67
68 elsif ($phase eq 'New Term step 2'){
69         # Choosing the area
70         my $areas = C4::Reports::get_report_areas();
71         my $definition_name=$input->param('definition_name');
72         my $definition_description=$input->param('definition_description');             
73         $template->param( 'step_2' => 1,
74                 'areas' => $areas,
75                 'definition_name' => $definition_name,
76                 'definition_description' => $definition_description,
77         );
78 }
79
80 elsif ($phase eq 'New Term step 3'){
81         # Choosing the columns
82         my $area = $input->param('areas');
83         my $columns = get_columns($area,$input);
84         my $definition_name=$input->param('definition_name');
85         my $definition_description=$input->param('definition_description');             
86         $template->param( 'step_3' => 1,
87                 'area' => $area,
88                 'columns' => $columns,
89                 'definition_name' => $definition_name,
90                 'definition_description' => $definition_description,
91         );
92 }
93
94 elsif ($phase eq 'New Term step 4'){
95         # Choosing the values
96         my $area=$input->param('area');
97         my $definition_name=$input->param('definition_name');
98         my $definition_description=$input->param('definition_description');             
99     my @columns = $input->param('columns');
100         my $columnstring = join (',',@columns);
101         my @column_loop;
102         foreach my $column (@columns){
103                 my %tmp_hash;
104                 $tmp_hash{'name'}=$column;
105                 my $type =get_column_type($column);
106                 if ($type eq 'distinct'){
107                         my $values = get_distinct_values($column);
108                         $tmp_hash{'values'} = $values;
109                         $tmp_hash{'distinct'} = 1;
110                           
111                 }
112                 if ($type eq 'DATE' || $type eq 'DATETIME'){
113                         $tmp_hash{'date'}=1;
114                 }
115                 if ($type eq 'TEXT'){
116                         $tmp_hash{'text'}=1;
117                 }
118 #               else {
119 #                       warn $type;#
120 #                       }
121                 push @column_loop,\%tmp_hash;
122                 }
123
124         $template->param( 'step_4' => 1,
125                 'area' => $area,
126                 'definition_name' => $definition_name,
127                 'definition_description' => $definition_description,
128                 'columns' => \@column_loop,
129                 'columnstring' => $columnstring,
130
131         );
132 }
133
134 elsif ($phase eq 'New Term step 5'){
135         # Confirmation screen
136         my $area = $input->param('area');
137         my $columnstring = $input->param('columnstring');
138         my $definition_name=$input->param('definition_name');
139         my $definition_description=$input->param('definition_description');     
140         my @criteria = $input->param('criteria_column'); 
141         my $query_criteria;
142         my @criteria_loop;
143         foreach my $crit (@criteria) {
144                 my $value = $input->param( $crit . "_value" );
145                 if ($value) {
146                         $query_criteria .= " AND $crit='$value'";
147                         my %tmp_hash;
148                         $tmp_hash{'name'}=$crit;
149                         $tmp_hash{'value'} = $value;
150                         push @criteria_loop,\%tmp_hash;
151                 }
152                 
153                 $value = $input->param( $crit . "_start_value" );
154                 if ($value) {
155                         $query_criteria .= " AND $crit > '$value'";
156                         my %tmp_hash;
157                         $tmp_hash{'name'}="$crit Start";
158                         $tmp_hash{'value'} = $value;
159                         push @criteria_loop,\%tmp_hash;
160                 }
161                 $value = $input->param( $crit . "_end_value" );
162                 if ($value) {
163                         $query_criteria .= " AND $crit <= '$value'";
164                         my %tmp_hash;
165                         $tmp_hash{'name'}="$crit End";
166                         $tmp_hash{'value'} = $value;
167                         push @criteria_loop,\%tmp_hash;
168                 }                 
169         }
170         $template->param( 'step_5' => 1,
171                 'area' => $area,
172                 'definition_name' => $definition_name,
173                 'definition_description' => $definition_description,
174                 'query' => $query_criteria,
175                 'columnstring' => $columnstring,
176                 'criteria_loop' => \@criteria_loop,
177         );
178 }
179
180 elsif ($phase eq 'New Term step 6'){
181         # Saving
182         my $area = $input->param('area');
183         my $definition_name=$input->param('definition_name');
184         my $definition_description=$input->param('definition_description');             
185         my $sql=$input->param('sql');
186         save_dictionary($definition_name,$definition_description,$sql,$area);
187         $no_html=1;
188         print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=View%20Dictionary");      
189
190 }
191 elsif ($phase eq 'Delete Definition'){
192         $no_html=1;
193         my $id = $input->param('id');
194         delete_definition($id);
195         print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=View%20Dictionary");
196         }
197
198 $template->param( 'referer' => $referer );
199
200
201 if (!$no_html){
202         output_html_with_http_headers $input, $cookie, $template->output;
203 }