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