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 $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(
43 template_name => "reports/dictionary.tt",
46 flagsrequired => { reports => '*' },
50 if ( $op eq 'add_form' ) {
52 # display form allowing them to add a new definition
53 $template->param( 'new_dictionary' => 1, );
56 elsif ( $op eq 'cud-add_form_2' ) {
61 'areas' => areas( $area ),
62 'definition_name' => $definition_name,
63 'definition_description' => $definition_description,
67 elsif ( $op eq 'cud-add_form_3' ) {
69 # Choosing the columns
70 my $columns = get_columns( $area, $input );
74 'columns' => $columns,
75 'definition_name' => $definition_name,
76 'definition_description' => $definition_description,
80 elsif ( $op eq 'cud-add_form_4' ) {
83 my @columns = $input->multi_param('columns');
84 my $columnstring = join( ',', @columns );
86 foreach my $column (@columns) {
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;
96 if ( $type eq 'DATE' || $type eq 'DATETIME' ) {
97 $tmp_hash{'date'} = 1;
99 if ($type eq 'TEXT' || $type eq 'MEDIUMTEXT'){
100 $tmp_hash{'text'} = 1;
106 push @column_loop, \%tmp_hash;
109 $template->param( 'step_4' => 1,
111 'definition_name' => $definition_name,
112 'definition_description' => $definition_description,
113 'columns' => \@column_loop,
114 'columnstring' => $columnstring,
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');
125 foreach my $crit (@criteria) {
126 my $value = $input->param( $crit . "_value" );
129 $tmp_hash{'name'} = $crit;
130 $tmp_hash{'value'} = $value;
131 push @criteria_loop, \%tmp_hash;
133 $query_criteria .= " AND $crit='$value'";
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" ) ) {
140 $tmp_hash{'name'} = "$crit Start";
141 $tmp_hash{'value'} = $value;
142 push @criteria_loop, \%tmp_hash;
144 $query_criteria .= " AND $crit >= '$value'";
147 if ( $value = $input->param( $crit . "_end_value" ) ) {
149 $tmp_hash{'name'} = "$crit End";
150 $tmp_hash{'value'} = $value;
151 push @criteria_loop, \%tmp_hash;
153 $query_criteria .= " AND $crit <= '$value'";
156 # else we want all dates
162 'definition_name' => $definition_name,
163 'definition_description' => $definition_description,
164 'query' => $query_criteria,
165 'columnstring' => $columnstring,
166 'criteria_loop' => \@criteria_loop,
170 elsif ( $op eq 'cud-add_form_6' ) {
172 my $area = $input->param('area');
173 my $sql = $input->param('sql');
174 save_dictionary( $definition_name, $definition_description, $sql, $area );
177 } elsif ( $op eq 'cud-delete' ) {
178 my $id = $input->param('id');
179 delete_definition($id);
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);
187 'areas' => areas( $area ),
188 'start_dictionary' => 1,
189 'definitions' => $definitions,
193 $template->param( 'referer' => $referer );
196 output_html_with_http_headers $input, $cookie, $template->output;
200 my $selected = shift;
202 my $areas = get_report_areas();
204 foreach my $area ( @$areas ) {
207 selected => ( $area eq $selected )