DHTMLcalendar should not be imported.
[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 use C4::Dates;
26
27 =head1 NAME
28
29 Script to control the guided report creation
30
31 =head1 DESCRIPTION
32
33
34 =over2
35
36 =cut
37
38 my $input = new CGI;
39 my $referer = $input->referer();
40
41 my $phase = $input->param('phase');
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     {
45         template_name   => "reports/dictionary.tmpl",
46         query           => $input,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { editcatalogue => 1 },
50         debug           => 1,
51     }
52         );
53
54 if ($phase eq 'View Dictionary'){
55         # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
56         my $areas = C4::Reports::get_report_areas();
57         my $definitions = get_from_dictionary();
58         $template->param( 'areas' => $areas ,
59                 'start_dictionary' => 1,
60                 'definitions' => $definitions,
61         );
62 }
63 elsif ($phase eq 'Add New Definition'){
64         # display form allowing them to add a new definition
65         $template->param( 'new_dictionary' => 1,
66                 );
67 }
68
69 elsif ($phase eq 'New Term step 2'){
70         # Choosing the area
71         my $areas = C4::Reports::get_report_areas();
72         my $definition_name=$input->param('definition_name');
73         my $definition_description=$input->param('definition_description');             
74         $template->param( 'step_2' => 1,
75                 'areas' => $areas,
76                 'definition_name' => $definition_name,
77                 'definition_description' => $definition_description,
78         );
79 }
80
81 elsif ($phase eq 'New Term step 3'){
82         # Choosing the columns
83         my $area = $input->param('areas');
84         my $columns = get_columns($area,$input);
85         my $definition_name=$input->param('definition_name');
86         my $definition_description=$input->param('definition_description');             
87         $template->param( 'step_3' => 1,
88                 'area' => $area,
89                 'columns' => $columns,
90                 'definition_name' => $definition_name,
91                 'definition_description' => $definition_description,
92         );
93 }
94
95 elsif ($phase eq 'New Term step 4'){
96         # Choosing the values
97         my $area=$input->param('area');
98         my $definition_name=$input->param('definition_name');
99         my $definition_description=$input->param('definition_description');             
100     my @columns = $input->param('columns');
101         my $columnstring = join (',',@columns);
102         my @column_loop;
103         foreach my $column (@columns){
104                 my %tmp_hash;
105                 $tmp_hash{'name'}=$column;
106                 my $type =get_column_type($column);
107                 if ($type eq 'distinct'){
108                         my $values = get_distinct_values($column);
109                         $tmp_hash{'values'} = $values;
110                         $tmp_hash{'distinct'} = 1;
111                           
112                 }
113                 if ($type eq 'DATE' || $type eq 'DATETIME'){
114                         $tmp_hash{'date'}=1;
115                 }
116                 if ($type eq 'TEXT'){
117                         $tmp_hash{'text'}=1;
118                 }
119 #               else {
120 #                       warn $type;#
121 #                       }
122                 push @column_loop,\%tmp_hash;
123                 }
124
125         $template->param( 'step_4' => 1,
126                 'area' => $area,
127                 'definition_name' => $definition_name,
128                 'definition_description' => $definition_description,
129                 'columns' => \@column_loop,
130                 'columnstring' => $columnstring,
131                 'DHTMLcalendar_dateformat' => C4::Dates->DHTMLcalendar(),
132         );
133 }
134
135 elsif ($phase eq 'New Term step 5'){
136         # Confirmation screen
137         my $areas = C4::Reports::get_report_areas();
138         my $area = $input->param('area');
139     my $areaname = $areas->[$area - 1]->{'name'};
140         my $columnstring = $input->param('columnstring');
141         my $definition_name=$input->param('definition_name');
142         my $definition_description=$input->param('definition_description');     
143         my @criteria = $input->param('criteria_column'); 
144         my $query_criteria;
145         my @criteria_loop;
146         foreach my $crit (@criteria) {
147                 my $value = $input->param( $crit . "_value" );
148                 if ($value) {
149                     my %tmp_hash;
150                     $tmp_hash{'name'}=$crit;
151                     $tmp_hash{'value'} = $value;
152                     push @criteria_loop,\%tmp_hash;
153                     if ($value =~ C4::Dates->regexp(C4::Context->preference('dateformat'))) {    
154                         my $date = C4::Dates->new($value);
155                         $value = $date->output("iso");
156                     }
157                     $query_criteria .= " AND $crit='$value'";
158                 }
159                 $value = $input->param( $crit . "_start_value" );
160                 if ($value) {
161                     my %tmp_hash;
162                     $tmp_hash{'name'}="$crit Start";
163                     $tmp_hash{'value'} = $value;
164                     push @criteria_loop,\%tmp_hash;
165                     if ($value =~ C4::Dates->regexp(C4::Context->preference('dateformat'))) {    
166                         my $date = C4::Dates->new($value);
167                         $value = $date->output("iso");
168                     }
169                     $query_criteria .= " AND $crit >= '$value'";
170                 }
171                 $value = $input->param( $crit . "_end_value" );
172                 if ($value) {
173                     my %tmp_hash;
174                     $tmp_hash{'name'}="$crit End";
175                     $tmp_hash{'value'} = $value;
176                     push @criteria_loop,\%tmp_hash;
177                     if ($value =~ C4::Dates->regexp(C4::Context->preference('dateformat'))) {    
178                         my $date = C4::Dates->new($value);
179                         $value = $date->output("iso");
180                     }
181                     $query_criteria .= " AND $crit <= '$value'";
182                 }                 
183         }
184         $template->param( 'step_5' => 1,
185                 'area' => $area,
186                 'areaname' => $areaname,
187                 'definition_name' => $definition_name,
188                 'definition_description' => $definition_description,
189                 'query' => $query_criteria,
190                 'columnstring' => $columnstring,
191                 'criteria_loop' => \@criteria_loop,
192         );
193 }
194
195 elsif ($phase eq 'New Term step 6'){
196         # Saving
197         my $area = $input->param('area');
198         my $definition_name=$input->param('definition_name');
199         my $definition_description=$input->param('definition_description');             
200         my $sql=$input->param('sql');
201         save_dictionary($definition_name,$definition_description,$sql,$area);
202         $no_html=1;
203         print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");  
204
205 }
206 elsif ($phase eq 'Delete Definition'){
207         $no_html=1;
208         my $id = $input->param('id');
209         delete_definition($id);
210         print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
211         }
212
213 $template->param( 'referer' => $referer );
214
215
216 if (!$no_html){
217         output_html_with_http_headers $input, $cookie, $template->output;
218 }