3 # Copyright 2007 Liblime ltd
5 # This file is part of Koha.
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.
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.
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>.
20 use C4::Auth qw( get_template_and_user );
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 );
28 Script to control the guided report creation
35 my $referer = $input->referer();
37 my $phase = $input->param('phase') || 'View Dictionary';
38 my $definition_name = $input->param('definition_name');
39 my $definition_description = $input->param('definition_description');
40 my $area = $input->param('area') || '';
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 { template_name => "reports/dictionary.tt",
46 flagsrequired => { reports => '*' },
50 if ($phase eq 'View Dictionary'){
51 # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
52 my $definitions = get_from_dictionary($area);
54 'areas' => areas( $area ),
55 'start_dictionary' => 1,
56 'definitions' => $definitions,
58 } elsif ( $phase eq 'Add New Definition' ) {
60 # display form allowing them to add a new definition
61 $template->param( 'new_dictionary' => 1, );
64 elsif ( $phase eq 'New Term step 2' ) {
69 'areas' => areas( $area ),
70 'definition_name' => $definition_name,
71 'definition_description' => $definition_description,
75 elsif ( $phase eq 'New Term step 3' ) {
77 # Choosing the columns
78 my $columns = get_columns( $area, $input );
82 'columns' => $columns,
83 'definition_name' => $definition_name,
84 'definition_description' => $definition_description,
88 elsif ( $phase eq 'New Term step 4' ) {
91 my @columns = $input->multi_param('columns');
92 my $columnstring = join( ',', @columns );
94 foreach my $column (@columns) {
96 $tmp_hash{'name'} = $column;
97 my $type = get_column_type($column);
98 if ( $type eq 'distinct' ) {
99 my $values = get_distinct_values($column);
100 $tmp_hash{'values'} = $values;
101 $tmp_hash{'distinct'} = 1;
104 if ( $type eq 'DATE' || $type eq 'DATETIME' ) {
105 $tmp_hash{'date'} = 1;
107 if ($type eq 'TEXT' || $type eq 'MEDIUMTEXT'){
108 $tmp_hash{'text'} = 1;
114 push @column_loop, \%tmp_hash;
117 $template->param( 'step_4' => 1,
119 'definition_name' => $definition_name,
120 'definition_description' => $definition_description,
121 'columns' => \@column_loop,
122 'columnstring' => $columnstring,
126 elsif ( $phase eq 'New Term step 5' ) {
127 # Confirmation screen
128 my $columnstring = $input->param('columnstring');
129 my @criteria = $input->multi_param('criteria_column');
133 foreach my $crit (@criteria) {
134 my $value = $input->param( $crit . "_value" );
137 $tmp_hash{'name'} = $crit;
138 $tmp_hash{'value'} = $value;
139 push @criteria_loop, \%tmp_hash;
141 $query_criteria .= " AND $crit='$value'";
144 if ( my $date_type_value = $input->param( $crit . "_date_type_value" ) ) {
145 if ( $date_type_value eq 'range' ) {
146 if ( $value = $input->param( $crit . "_start_value" ) ) {
148 $tmp_hash{'name'} = "$crit Start";
149 $tmp_hash{'value'} = $value;
150 push @criteria_loop, \%tmp_hash;
152 $query_criteria .= " AND $crit >= '$value'";
155 if ( $value = $input->param( $crit . "_end_value" ) ) {
157 $tmp_hash{'name'} = "$crit End";
158 $tmp_hash{'value'} = $value;
159 push @criteria_loop, \%tmp_hash;
161 $query_criteria .= " AND $crit <= '$value'";
164 # else we want all dates
170 'definition_name' => $definition_name,
171 'definition_description' => $definition_description,
172 'query' => $query_criteria,
173 'columnstring' => $columnstring,
174 'criteria_loop' => \@criteria_loop,
178 elsif ( $phase eq 'New Term step 6' ) {
180 my $area = $input->param('area');
181 my $sql = $input->param('sql');
182 save_dictionary( $definition_name, $definition_description, $sql, $area );
184 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
186 } elsif ( $phase eq 'Delete Definition' ) {
188 my $id = $input->param('id');
189 delete_definition($id);
190 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
193 $template->param( 'referer' => $referer );
197 output_html_with_http_headers $input, $cookie, $template->output;
202 my $selected = shift;
204 my $areas = get_report_areas();
206 foreach my $area ( @$areas ) {
209 selected => ( $area eq $selected )