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