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;
140 my $value_dt = eval { dt_from_string( $value ) };
141 $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
144 $query_criteria .= " AND $crit='$value'";
147 if ( my $date_type_value = $input->param( $crit . "_date_type_value" ) ) {
148 if ( $date_type_value eq 'range' ) {
149 if ( $value = $input->param( $crit . "_start_value" ) ) {
151 $tmp_hash{'name'} = "$crit Start";
152 $tmp_hash{'value'} = $value;
153 push @criteria_loop, \%tmp_hash;
154 my $value_dt = eval { dt_from_string( $value ) };
155 $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
158 $query_criteria .= " AND $crit >= '$value'";
161 if ( $value = $input->param( $crit . "_end_value" ) ) {
163 $tmp_hash{'name'} = "$crit End";
164 $tmp_hash{'value'} = $value;
165 push @criteria_loop, \%tmp_hash;
166 my $value_dt = eval { dt_from_string( $value ) };
167 $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
170 $query_criteria .= " AND $crit <= '$value'";
173 # else we want all dates
179 'definition_name' => $definition_name,
180 'definition_description' => $definition_description,
181 'query' => $query_criteria,
182 'columnstring' => $columnstring,
183 'criteria_loop' => \@criteria_loop,
187 elsif ( $phase eq 'New Term step 6' ) {
189 my $area = $input->param('area');
190 my $sql = $input->param('sql');
191 save_dictionary( $definition_name, $definition_description, $sql, $area );
193 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
195 } elsif ( $phase eq 'Delete Definition' ) {
197 my $id = $input->param('id');
198 delete_definition($id);
199 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
202 $template->param( 'referer' => $referer );
206 output_html_with_http_headers $input, $cookie, $template->output;
211 my $selected = shift;
213 my $areas = get_report_areas();
215 foreach my $area ( @$areas ) {
218 selected => ( $area eq $selected )