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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 use CGI
::Carp
qw(fatalsToBrowser warningsToBrowser);
25 use C4
::Reports
::Guided
;
30 Script to control the guided report creation
40 my $referer = $input->referer();
42 my $phase = $input->param('phase') || 'View Dictionary';
43 my $area = $input->param('areas') || '';
44 my $no_html = 0; # this will be set if we dont want to print out an html::template
45 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
47 template_name
=> "reports/dictionary.tmpl",
51 flagsrequired
=> { reports
=> '*' },
56 if ($phase eq 'View Dictionary'){
57 # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
58 my $areas = get_report_areas
();
59 foreach (@
{ $areas }) {
60 $_->{selected
} = 1 if $_->{id
} eq $area; # mark active area
62 my $definitions = get_from_dictionary
($area);
63 $template->param( 'areas' => $areas ,
64 'start_dictionary' => 1,
65 'definitions' => $definitions,
68 elsif ($phase eq 'Add New Definition'){
69 # display form allowing them to add a new definition
70 $template->param( 'new_dictionary' => 1,
74 elsif ($phase eq 'New Term step 2'){
76 my $areas = C4
::Reports
::Guided
::get_report_areas
();
77 my $definition_name=$input->param('definition_name');
78 my $definition_description=$input->param('definition_description');
79 $template->param( 'step_2' => 1,
81 'definition_name' => $definition_name,
82 'definition_description' => $definition_description,
86 elsif ($phase eq 'New Term step 3'){
87 # Choosing the columns
88 my $area = $input->param('areas');
89 my $columns = get_columns
($area,$input);
90 my $definition_name=$input->param('definition_name');
91 my $definition_description=$input->param('definition_description');
92 $template->param( 'step_3' => 1,
94 'columns' => $columns,
95 'definition_name' => $definition_name,
96 'definition_description' => $definition_description,
100 elsif ($phase eq 'New Term step 4'){
101 # Choosing the values
102 my $area=$input->param('area');
103 my $definition_name=$input->param('definition_name');
104 my $definition_description=$input->param('definition_description');
105 my @columns = $input->param('columns');
106 my $columnstring = join (',',@columns);
108 foreach my $column (@columns){
110 $tmp_hash{'name'}=$column;
111 my $type =get_column_type
($column);
112 if ($type eq 'distinct'){
113 my $values = get_distinct_values
($column);
114 $tmp_hash{'values'} = $values;
115 $tmp_hash{'distinct'} = 1;
118 if ($type eq 'DATE' || $type eq 'DATETIME'){
121 if ($type eq 'TEXT'){
127 push @column_loop,\
%tmp_hash;
130 $template->param( 'step_4' => 1,
132 'definition_name' => $definition_name,
133 'definition_description' => $definition_description,
134 'columns' => \
@column_loop,
135 'columnstring' => $columnstring,
136 'DHTMLcalendar_dateformat' => C4
::Dates
->DHTMLcalendar(),
140 elsif ($phase eq 'New Term step 5'){
141 # Confirmation screen
142 my $areas = C4
::Reports
::Guided
::get_report_areas
();
143 my $area = $input->param('area');
144 my $areaname = $areas->[$area - 1]->{'name'};
145 my $columnstring = $input->param('columnstring');
146 my $definition_name=$input->param('definition_name');
147 my $definition_description=$input->param('definition_description');
148 my @criteria = $input->param('criteria_column');
151 foreach my $crit (@criteria) {
152 my $value = $input->param( $crit . "_value" );
155 $tmp_hash{'name'}=$crit;
156 $tmp_hash{'value'} = $value;
157 push @criteria_loop,\
%tmp_hash;
158 if ($value =~ C4
::Dates
->regexp(C4
::Context
->preference('dateformat'))) {
159 my $date = C4
::Dates
->new($value);
160 $value = $date->output("iso");
162 $query_criteria .= " AND $crit='$value'";
164 $value = $input->param( $crit . "_start_value" );
167 $tmp_hash{'name'}="$crit Start";
168 $tmp_hash{'value'} = $value;
169 push @criteria_loop,\
%tmp_hash;
170 if ($value =~ C4
::Dates
->regexp(C4
::Context
->preference('dateformat'))) {
171 my $date = C4
::Dates
->new($value);
172 $value = $date->output("iso");
174 $query_criteria .= " AND $crit >= '$value'";
176 $value = $input->param( $crit . "_end_value" );
179 $tmp_hash{'name'}="$crit End";
180 $tmp_hash{'value'} = $value;
181 push @criteria_loop,\
%tmp_hash;
182 if ($value =~ C4
::Dates
->regexp(C4
::Context
->preference('dateformat'))) {
183 my $date = C4
::Dates
->new($value);
184 $value = $date->output("iso");
186 $query_criteria .= " AND $crit <= '$value'";
189 $template->param( 'step_5' => 1,
191 'areaname' => $areaname,
192 'definition_name' => $definition_name,
193 'definition_description' => $definition_description,
194 'query' => $query_criteria,
195 'columnstring' => $columnstring,
196 'criteria_loop' => \
@criteria_loop,
200 elsif ($phase eq 'New Term step 6'){
202 my $area = $input->param('area');
203 my $definition_name=$input->param('definition_name');
204 my $definition_description=$input->param('definition_description');
205 my $sql=$input->param('sql');
206 save_dictionary
($definition_name,$definition_description,$sql,$area);
208 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
211 elsif ($phase eq 'Delete Definition'){
213 my $id = $input->param('id');
214 delete_definition
($id);
215 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
218 $template->param( 'referer' => $referer );
222 output_html_with_http_headers
$input, $cookie, $template->output;