Bug 27498: (follow-up) Update to display logic
[koha.git] / reports / dictionary.pl
1 #!/usr/bin/perl
2
3 # Copyright 2007 Liblime ltd
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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 Modern::Perl;
21 use C4::Auth;
22 use CGI qw ( -utf8 );
23 use C4::Output;
24 use C4::Reports::Guided;
25 use Koha::DateUtils;
26
27 =head1 NAME
28
29 Script to control the guided report creation
30
31 =head1 DESCRIPTION
32
33 =cut
34
35 my $input = CGI->new;
36 my $referer = $input->referer();
37
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",
45         query           => $input,
46         type            => "intranet",
47         flagsrequired   => { reports => '*' },
48         debug           => 1,
49     }
50         );
51
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);
55     $template->param(
56         'areas'            => areas( $area ),
57         'start_dictionary' => 1,
58         'definitions'      => $definitions,
59     );
60 } elsif ( $phase eq 'Add New Definition' ) {
61
62     # display form allowing them to add a new definition
63     $template->param( 'new_dictionary' => 1, );
64 }
65
66 elsif ( $phase eq 'New Term step 2' ) {
67
68     # Choosing the area
69     $template->param(
70         'step_2'                 => 1,
71         'areas'                  => areas( $area ),
72         'definition_name'        => $definition_name,
73         'definition_description' => $definition_description,
74     );
75 }
76
77 elsif ( $phase eq 'New Term step 3' ) {
78
79     # Choosing the columns
80     my $columns                = get_columns( $area, $input );
81     $template->param(
82         'step_3'                 => 1,
83         'area'                   => $area,
84         'columns'                => $columns,
85         'definition_name'        => $definition_name,
86         'definition_description' => $definition_description,
87     );
88 }
89
90 elsif ( $phase eq 'New Term step 4' ) {
91
92     # Choosing the values
93     my @columns                = $input->multi_param('columns');
94     my $columnstring           = join( ',', @columns );
95     my @column_loop;
96     foreach my $column (@columns) {
97         my %tmp_hash;
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;
104
105         }
106         if ( $type eq 'DATE' || $type eq 'DATETIME' ) {
107             $tmp_hash{'date'} = 1;
108         }
109         if ($type eq 'TEXT' || $type eq 'MEDIUMTEXT'){
110             $tmp_hash{'text'} = 1;
111         }
112
113         #               else {
114         #                       warn $type;#
115         #                       }
116         push @column_loop, \%tmp_hash;
117     }
118
119         $template->param( 'step_4' => 1,
120                 'area' => $area,
121                 'definition_name' => $definition_name,
122                 'definition_description' => $definition_description,
123                 'columns' => \@column_loop,
124                 'columnstring' => $columnstring,
125         );
126 }
127
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');
132     my $query_criteria;
133     my @criteria_loop;
134
135     foreach my $crit (@criteria) {
136         my $value = $input->param( $crit . "_value" );
137         if ($value) {
138             my %tmp_hash;
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' } )
144                 if ( $value_dt );
145
146             $query_criteria .= " AND $crit='$value'";
147         }
148
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" ) ) {
152                     my %tmp_hash;
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' } )
158                         if ( $value_dt );
159
160                     $query_criteria .= " AND $crit >= '$value'";
161                 }
162
163                 if ( $value = $input->param( $crit . "_end_value" ) ) {
164                     my %tmp_hash;
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' } )
170                         if ( $value_dt );
171
172                     $query_criteria .= " AND $crit <= '$value'";
173                 }
174             }
175             # else we want all dates
176         }
177     }
178     $template->param(
179         'step_5'                 => 1,
180         'area'                   => $area,
181         'definition_name'        => $definition_name,
182         'definition_description' => $definition_description,
183         'query'                  => $query_criteria,
184         'columnstring'           => $columnstring,
185         'criteria_loop'          => \@criteria_loop,
186     );
187 }
188
189 elsif ( $phase eq 'New Term step 6' ) {
190     # Saving
191     my $area                   = $input->param('area');
192     my $sql                    = $input->param('sql');
193     save_dictionary( $definition_name, $definition_description, $sql, $area );
194     $no_html = 1;
195     print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
196
197 } elsif ( $phase eq 'Delete Definition' ) {
198     $no_html = 1;
199     my $id = $input->param('id');
200     delete_definition($id);
201     print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
202 }
203
204 $template->param( 'referer' => $referer );
205
206
207 if (!$no_html){
208         output_html_with_http_headers $input, $cookie, $template->output;
209 }
210
211 sub areas {
212
213     my $selected = shift;
214
215     my $areas = get_report_areas();
216     my @a;
217     foreach my $area ( @$areas ) {
218         push @a, {
219             id       => $area,
220             selected => ( $area eq $selected )
221         };
222     }
223
224     return \@a;
225 }