26976c02155dee9fa3cd32590d986508635c84b4
[srvgit] / 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     }
49         );
50
51 if ($phase eq 'View Dictionary'){
52     # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
53     my $definitions = get_from_dictionary($area);
54     $template->param(
55         'areas'            => areas( $area ),
56         'start_dictionary' => 1,
57         'definitions'      => $definitions,
58     );
59 } elsif ( $phase eq 'Add New Definition' ) {
60
61     # display form allowing them to add a new definition
62     $template->param( 'new_dictionary' => 1, );
63 }
64
65 elsif ( $phase eq 'New Term step 2' ) {
66
67     # Choosing the area
68     $template->param(
69         'step_2'                 => 1,
70         'areas'                  => areas( $area ),
71         'definition_name'        => $definition_name,
72         'definition_description' => $definition_description,
73     );
74 }
75
76 elsif ( $phase eq 'New Term step 3' ) {
77
78     # Choosing the columns
79     my $columns                = get_columns( $area, $input );
80     $template->param(
81         'step_3'                 => 1,
82         'area'                   => $area,
83         'columns'                => $columns,
84         'definition_name'        => $definition_name,
85         'definition_description' => $definition_description,
86     );
87 }
88
89 elsif ( $phase eq 'New Term step 4' ) {
90
91     # Choosing the values
92     my @columns                = $input->multi_param('columns');
93     my $columnstring           = join( ',', @columns );
94     my @column_loop;
95     foreach my $column (@columns) {
96         my %tmp_hash;
97         $tmp_hash{'name'} = $column;
98         my $type = get_column_type($column);
99         if ( $type eq 'distinct' ) {
100             my $values = get_distinct_values($column);
101             $tmp_hash{'values'}   = $values;
102             $tmp_hash{'distinct'} = 1;
103
104         }
105         if ( $type eq 'DATE' || $type eq 'DATETIME' ) {
106             $tmp_hash{'date'} = 1;
107         }
108         if ($type eq 'TEXT' || $type eq 'MEDIUMTEXT'){
109             $tmp_hash{'text'} = 1;
110         }
111
112         #               else {
113         #                       warn $type;#
114         #                       }
115         push @column_loop, \%tmp_hash;
116     }
117
118         $template->param( 'step_4' => 1,
119                 'area' => $area,
120                 'definition_name' => $definition_name,
121                 'definition_description' => $definition_description,
122                 'columns' => \@column_loop,
123                 'columnstring' => $columnstring,
124         );
125 }
126
127 elsif ( $phase eq 'New Term step 5' ) {
128     # Confirmation screen
129     my $columnstring           = $input->param('columnstring');
130     my @criteria               = $input->multi_param('criteria_column');
131     my $query_criteria;
132     my @criteria_loop;
133
134     foreach my $crit (@criteria) {
135         my $value = $input->param( $crit . "_value" );
136         if ($value) {
137             my %tmp_hash;
138             $tmp_hash{'name'}  = $crit;
139             $tmp_hash{'value'} = $value;
140             push @criteria_loop, \%tmp_hash;
141             my $value_dt = eval { dt_from_string( $value ) };
142             $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
143                 if ( $value_dt );
144
145             $query_criteria .= " AND $crit='$value'";
146         }
147
148         if ( my $date_type_value = $input->param( $crit . "_date_type_value" ) ) {
149             if ( $date_type_value eq 'range' ) {
150                 if ( $value = $input->param( $crit . "_start_value" ) ) {
151                     my %tmp_hash;
152                     $tmp_hash{'name'}  = "$crit Start";
153                     $tmp_hash{'value'} = $value;
154                     push @criteria_loop, \%tmp_hash;
155                     my $value_dt = eval { dt_from_string( $value ) };
156                     $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
157                         if ( $value_dt );
158
159                     $query_criteria .= " AND $crit >= '$value'";
160                 }
161
162                 if ( $value = $input->param( $crit . "_end_value" ) ) {
163                     my %tmp_hash;
164                     $tmp_hash{'name'}  = "$crit End";
165                     $tmp_hash{'value'} = $value;
166                     push @criteria_loop, \%tmp_hash;
167                     my $value_dt = eval { dt_from_string( $value ) };
168                     $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
169                         if ( $value_dt );
170
171                     $query_criteria .= " AND $crit <= '$value'";
172                 }
173             }
174             # else we want all dates
175         }
176     }
177     $template->param(
178         'step_5'                 => 1,
179         'area'                   => $area,
180         'definition_name'        => $definition_name,
181         'definition_description' => $definition_description,
182         'query'                  => $query_criteria,
183         'columnstring'           => $columnstring,
184         'criteria_loop'          => \@criteria_loop,
185     );
186 }
187
188 elsif ( $phase eq 'New Term step 6' ) {
189     # Saving
190     my $area                   = $input->param('area');
191     my $sql                    = $input->param('sql');
192     save_dictionary( $definition_name, $definition_description, $sql, $area );
193     $no_html = 1;
194     print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
195
196 } elsif ( $phase eq 'Delete Definition' ) {
197     $no_html = 1;
198     my $id = $input->param('id');
199     delete_definition($id);
200     print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
201 }
202
203 $template->param( 'referer' => $referer );
204
205
206 if (!$no_html){
207         output_html_with_http_headers $input, $cookie, $template->output;
208 }
209
210 sub areas {
211
212     my $selected = shift;
213
214     my $areas = get_report_areas();
215     my @a;
216     foreach my $area ( @$areas ) {
217         push @a, {
218             id       => $area,
219             selected => ( $area eq $selected )
220         };
221     }
222
223     return \@a;
224 }