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>.
19 use CGI
::Carp
qw(fatalsToBrowser warningsToBrowser);
24 use C4
::Reports
::Guided
;
29 Script to control the guided report creation
36 my $referer = $input->referer();
38 my $phase = $input->param('phase') || 'View Dictionary';
39 my $definition_name = $input->param('definition_name');
40 my $definition_description = $input->param('definition_description');
41 my $area = $input->param('area') || '';
42 my $no_html = 0; # this will be set if we dont want to print out an html::template
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
44 { template_name
=> "reports/dictionary.tt",
47 flagsrequired
=> { reports
=> '*' },
52 if ($phase eq 'View Dictionary'){
53 # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
54 my $definitions = get_from_dictionary
($area);
56 'areas' => areas
( $area ),
57 'start_dictionary' => 1,
58 'definitions' => $definitions,
60 } elsif ( $phase eq 'Add New Definition' ) {
62 # display form allowing them to add a new definition
63 $template->param( 'new_dictionary' => 1, );
66 elsif ( $phase eq 'New Term step 2' ) {
71 'areas' => areas
( $area ),
72 'definition_name' => $definition_name,
73 'definition_description' => $definition_description,
77 elsif ( $phase eq 'New Term step 3' ) {
79 # Choosing the columns
80 my $columns = get_columns
( $area, $input );
84 'columns' => $columns,
85 'definition_name' => $definition_name,
86 'definition_description' => $definition_description,
90 elsif ( $phase eq 'New Term step 4' ) {
93 my @columns = $input->multi_param('columns');
94 my $columnstring = join( ',', @columns );
96 foreach my $column (@columns) {
98 $tmp_hash{'name'} = $column;
99 my $type = get_column_type
($column);
100 if ( $type eq 'distinct' ) {
101 my $values = get_distinct_values
($column);
102 $tmp_hash{'values'} = $values;
103 $tmp_hash{'distinct'} = 1;
106 if ( $type eq 'DATE' || $type eq 'DATETIME' ) {
107 $tmp_hash{'date'} = 1;
109 if ($type eq 'TEXT' || $type eq 'MEDIUMTEXT'){
110 $tmp_hash{'text'} = 1;
116 push @column_loop, \
%tmp_hash;
119 $template->param( 'step_4' => 1,
121 'definition_name' => $definition_name,
122 'definition_description' => $definition_description,
123 'columns' => \
@column_loop,
124 'columnstring' => $columnstring,
128 elsif ( $phase eq 'New Term step 5' ) {
129 # Confirmation screen
130 my $columnstring = $input->param('columnstring');
131 my @criteria = $input->multi_param('criteria_column');
135 foreach my $crit (@criteria) {
136 my $value = $input->param( $crit . "_value" );
139 $tmp_hash{'name'} = $crit;
140 $tmp_hash{'value'} = $value;
141 push @criteria_loop, \
%tmp_hash;
142 my $value_dt = eval { dt_from_string
( $value ) };
143 $value = output_pref
( { dt
=> $value_dt, dateonly
=> 1, dateformat
=> 'iso' } )
146 $query_criteria .= " AND $crit='$value'";
149 if ( my $date_type_value = $input->param( $crit . "_date_type_value" ) ) {
150 if ( $date_type_value eq 'range' ) {
151 if ( $value = $input->param( $crit . "_start_value" ) ) {
153 $tmp_hash{'name'} = "$crit Start";
154 $tmp_hash{'value'} = $value;
155 push @criteria_loop, \
%tmp_hash;
156 my $value_dt = eval { dt_from_string
( $value ) };
157 $value = output_pref
( { dt
=> $value_dt, dateonly
=> 1, dateformat
=> 'iso' } )
160 $query_criteria .= " AND $crit >= '$value'";
163 if ( $value = $input->param( $crit . "_end_value" ) ) {
165 $tmp_hash{'name'} = "$crit End";
166 $tmp_hash{'value'} = $value;
167 push @criteria_loop, \
%tmp_hash;
168 my $value_dt = eval { dt_from_string
( $value ) };
169 $value = output_pref
( { dt
=> $value_dt, dateonly
=> 1, dateformat
=> 'iso' } )
172 $query_criteria .= " AND $crit <= '$value'";
175 # else we want all dates
181 'definition_name' => $definition_name,
182 'definition_description' => $definition_description,
183 'query' => $query_criteria,
184 'columnstring' => $columnstring,
185 'criteria_loop' => \
@criteria_loop,
189 elsif ( $phase eq 'New Term step 6' ) {
191 my $area = $input->param('area');
192 my $sql = $input->param('sql');
193 save_dictionary
( $definition_name, $definition_description, $sql, $area );
195 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
197 } elsif ( $phase eq 'Delete Definition' ) {
199 my $id = $input->param('id');
200 delete_definition
($id);
201 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
204 $template->param( 'referer' => $referer );
208 output_html_with_http_headers
$input, $cookie, $template->output;
213 my $selected = shift;
215 my $areas = get_report_areas
();
217 foreach my $area ( @
$areas ) {
220 selected
=> ( $area eq $selected )