Bug 7677 - Schema update
[koha.git] / reports / dictionary.pl
blob7d86f8d11a6abc09e209b4b1d7637cd30853219e
1 #!/usr/bin/perl
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);
20 use strict;
21 use warnings;
22 use C4::Auth;
23 use CGI qw ( -utf8 );
24 use C4::Output;
25 use C4::Reports::Guided;
26 use Koha::DateUtils;
28 =head1 NAME
30 Script to control the guided report creation
32 =head1 DESCRIPTION
34 =cut
36 my $input = new CGI;
37 my $referer = $input->referer();
39 my $phase = $input->param('phase') || 'View Dictionary';
40 my $definition_name = $input->param('definition_name');
41 my $definition_description = $input->param('definition_description');
42 my $area = $input->param('area') || '';
43 my $no_html = 0; # this will be set if we dont want to print out an html::template
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45 { template_name => "reports/dictionary.tt",
46 query => $input,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => { reports => '*' },
50 debug => 1,
54 if ($phase eq 'View Dictionary'){
55 # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
56 my $definitions = get_from_dictionary($area);
57 $template->param(
58 'areas' => areas( $area ),
59 'start_dictionary' => 1,
60 'definitions' => $definitions,
62 } elsif ( $phase eq 'Add New Definition' ) {
64 # display form allowing them to add a new definition
65 $template->param( 'new_dictionary' => 1, );
68 elsif ( $phase eq 'New Term step 2' ) {
70 # Choosing the area
71 $template->param(
72 'step_2' => 1,
73 'areas' => areas( $area ),
74 'definition_name' => $definition_name,
75 'definition_description' => $definition_description,
79 elsif ( $phase eq 'New Term step 3' ) {
81 # Choosing the columns
82 my $columns = get_columns( $area, $input );
83 $template->param(
84 'step_3' => 1,
85 'area' => $area,
86 'columns' => $columns,
87 'definition_name' => $definition_name,
88 'definition_description' => $definition_description,
92 elsif ( $phase eq 'New Term step 4' ) {
94 # Choosing the values
95 my @columns = $input->multi_param('columns');
96 my $columnstring = join( ',', @columns );
97 my @column_loop;
98 foreach my $column (@columns) {
99 my %tmp_hash;
100 $tmp_hash{'name'} = $column;
101 my $type = get_column_type($column);
102 if ( $type eq 'distinct' ) {
103 my $values = get_distinct_values($column);
104 $tmp_hash{'values'} = $values;
105 $tmp_hash{'distinct'} = 1;
108 if ( $type eq 'DATE' || $type eq 'DATETIME' ) {
109 $tmp_hash{'date'} = 1;
111 if ($type eq 'TEXT' || $type eq 'MEDIUMTEXT'){
112 $tmp_hash{'text'} = 1;
115 # else {
116 # warn $type;#
118 push @column_loop, \%tmp_hash;
121 $template->param( 'step_4' => 1,
122 'area' => $area,
123 'definition_name' => $definition_name,
124 'definition_description' => $definition_description,
125 'columns' => \@column_loop,
126 'columnstring' => $columnstring,
130 elsif ( $phase eq 'New Term step 5' ) {
131 # Confirmation screen
132 my $columnstring = $input->param('columnstring');
133 my @criteria = $input->multi_param('criteria_column');
134 my $query_criteria;
135 my @criteria_loop;
137 foreach my $crit (@criteria) {
138 my $value = $input->param( $crit . "_value" );
139 if ($value) {
140 my %tmp_hash;
141 $tmp_hash{'name'} = $crit;
142 $tmp_hash{'value'} = $value;
143 push @criteria_loop, \%tmp_hash;
144 my $value_dt = eval { dt_from_string( $value ) };
145 $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
146 if ( $value_dt );
148 $query_criteria .= " AND $crit='$value'";
151 if ( my $date_type_value = $input->param( $crit . "_date_type_value" ) ) {
152 if ( $date_type_value eq 'range' ) {
153 if ( $value = $input->param( $crit . "_start_value" ) ) {
154 my %tmp_hash;
155 $tmp_hash{'name'} = "$crit Start";
156 $tmp_hash{'value'} = $value;
157 push @criteria_loop, \%tmp_hash;
158 my $value_dt = eval { dt_from_string( $value ) };
159 $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
160 if ( $value_dt );
162 $query_criteria .= " AND $crit >= '$value'";
165 if ( $value = $input->param( $crit . "_end_value" ) ) {
166 my %tmp_hash;
167 $tmp_hash{'name'} = "$crit End";
168 $tmp_hash{'value'} = $value;
169 push @criteria_loop, \%tmp_hash;
170 my $value_dt = eval { dt_from_string( $value ) };
171 $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
172 if ( $value_dt );
174 $query_criteria .= " AND $crit <= '$value'";
177 # else we want all dates
180 $template->param(
181 'step_5' => 1,
182 'area' => $area,
183 'definition_name' => $definition_name,
184 'definition_description' => $definition_description,
185 'query' => $query_criteria,
186 'columnstring' => $columnstring,
187 'criteria_loop' => \@criteria_loop,
191 elsif ( $phase eq 'New Term step 6' ) {
192 # Saving
193 my $area = $input->param('area');
194 my $sql = $input->param('sql');
195 save_dictionary( $definition_name, $definition_description, $sql, $area );
196 $no_html = 1;
197 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
199 } elsif ( $phase eq 'Delete Definition' ) {
200 $no_html = 1;
201 my $id = $input->param('id');
202 delete_definition($id);
203 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
206 $template->param( 'referer' => $referer );
209 if (!$no_html){
210 output_html_with_http_headers $input, $cookie, $template->output;
213 sub areas {
215 my $selected = shift;
217 my $areas = get_report_areas();
218 my @a;
219 foreach my $area ( @$areas ) {
220 push @a, {
221 id => $area,
222 selected => ( $area eq $selected )
226 return \@a;