Bug 36102: Remove cookie from the installer session
[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
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.
11 #
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.
16 #
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 Modern::Perl;
20 use C4::Auth qw( get_template_and_user );
21 use CGI qw ( -utf8 );
22 use C4::Output qw( output_html_with_http_headers );
23 use C4::Reports::Guided qw( get_from_dictionary get_columns get_column_type get_distinct_values save_dictionary delete_definition get_report_areas );
24 use Koha::DateUtils qw( dt_from_string output_pref );
25
26 =head1 NAME
27
28 Script to control the guided report creation
29
30 =head1 DESCRIPTION
31
32 =cut
33
34 my $input = CGI->new;
35 my $referer = $input->referer();
36
37 my $op = $input->param('op') || q{list};
38 my $definition_name        = $input->param('definition_name');
39 my $definition_description = $input->param('definition_description');
40 my $area  = $input->param('area') || '';
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
42     {
43         template_name => "reports/dictionary.tt",
44         query         => $input,
45         type          => "intranet",
46         flagsrequired => { reports => '*' },
47     }
48 );
49
50 if ( $op eq 'add_form' ) {
51
52     # display form allowing them to add a new definition
53     $template->param( 'new_dictionary' => 1, );
54 }
55
56 elsif ( $op eq 'cud-add_form_2' ) {
57
58     # Choosing the area
59     $template->param(
60         'step_2'                 => 1,
61         'areas'                  => areas( $area ),
62         'definition_name'        => $definition_name,
63         'definition_description' => $definition_description,
64     );
65 }
66
67 elsif ( $op eq 'cud-add_form_3' ) {
68
69     # Choosing the columns
70     my $columns                = get_columns( $area, $input );
71     $template->param(
72         'step_3'                 => 1,
73         'area'                   => $area,
74         'columns'                => $columns,
75         'definition_name'        => $definition_name,
76         'definition_description' => $definition_description,
77     );
78 }
79
80 elsif ( $op eq 'cud-add_form_4' ) {
81
82     # Choosing the values
83     my @columns                = $input->multi_param('columns');
84     my $columnstring           = join( ',', @columns );
85     my @column_loop;
86     foreach my $column (@columns) {
87         my %tmp_hash;
88         $tmp_hash{'name'} = $column;
89         my $type = get_column_type($column);
90         if ( $type eq 'distinct' ) {
91             my $values = get_distinct_values($column);
92             $tmp_hash{'values'}   = $values;
93             $tmp_hash{'distinct'} = 1;
94
95         }
96         if ( $type eq 'DATE' || $type eq 'DATETIME' ) {
97             $tmp_hash{'date'} = 1;
98         }
99         if ($type eq 'TEXT' || $type eq 'MEDIUMTEXT'){
100             $tmp_hash{'text'} = 1;
101         }
102
103         #               else {
104         #                       warn $type;#
105         #                       }
106         push @column_loop, \%tmp_hash;
107     }
108
109         $template->param( 'step_4' => 1,
110                 'area' => $area,
111                 'definition_name' => $definition_name,
112                 'definition_description' => $definition_description,
113                 'columns' => \@column_loop,
114                 'columnstring' => $columnstring,
115         );
116 }
117
118 elsif ( $op eq 'cud-add_form_5' ) {
119     # Confirmation screen
120     my $columnstring           = $input->param('columnstring');
121     my @criteria               = $input->multi_param('criteria_column');
122     my $query_criteria;
123     my @criteria_loop;
124
125     foreach my $crit (@criteria) {
126         my $value = $input->param( $crit . "_value" );
127         if ($value) {
128             my %tmp_hash;
129             $tmp_hash{'name'}  = $crit;
130             $tmp_hash{'value'} = $value;
131             push @criteria_loop, \%tmp_hash;
132
133             $query_criteria .= " AND $crit='$value'";
134         }
135
136         if ( my $date_type_value = $input->param( $crit . "_date_type_value" ) ) {
137             if ( $date_type_value eq 'range' ) {
138                 if ( $value = $input->param( $crit . "_start_value" ) ) {
139                     my %tmp_hash;
140                     $tmp_hash{'name'}  = "$crit Start";
141                     $tmp_hash{'value'} = $value;
142                     push @criteria_loop, \%tmp_hash;
143
144                     $query_criteria .= " AND $crit >= '$value'";
145                 }
146
147                 if ( $value = $input->param( $crit . "_end_value" ) ) {
148                     my %tmp_hash;
149                     $tmp_hash{'name'}  = "$crit End";
150                     $tmp_hash{'value'} = $value;
151                     push @criteria_loop, \%tmp_hash;
152
153                     $query_criteria .= " AND $crit <= '$value'";
154                 }
155             }
156             # else we want all dates
157         }
158     }
159     $template->param(
160         'step_5'                 => 1,
161         'area'                   => $area,
162         'definition_name'        => $definition_name,
163         'definition_description' => $definition_description,
164         'query'                  => $query_criteria,
165         'columnstring'           => $columnstring,
166         'criteria_loop'          => \@criteria_loop,
167     );
168 }
169
170 elsif ( $op eq 'cud-add_form_6' ) {
171     # Saving
172     my $area                   = $input->param('area');
173     my $sql                    = $input->param('sql');
174     save_dictionary( $definition_name, $definition_description, $sql, $area );
175     $op = "list";
176
177 } elsif ( $op eq 'cud-delete' ) {
178     my $id = $input->param('id');
179     delete_definition($id);
180     $op = "list";
181 }
182
183 if ($op eq 'list'){
184     # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
185     my $definitions = get_from_dictionary($area);
186     $template->param(
187         'areas'            => areas( $area ),
188         'start_dictionary' => 1,
189         'definitions'      => $definitions,
190     );
191 }
192
193 $template->param( 'referer' => $referer );
194
195
196 output_html_with_http_headers $input, $cookie, $template->output;
197
198 sub areas {
199
200     my $selected = shift;
201
202     my $areas = get_report_areas();
203     my @a;
204     foreach my $area ( @$areas ) {
205         push @a, {
206             id       => $area,
207             selected => ( $area eq $selected )
208         };
209     }
210
211     return \@a;
212 }