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