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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 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 my $definitions = get_from_dictionary
($area);
60 $template->param( 'areas' => $areas ,
61 'start_dictionary' => 1,
62 'definitions' => $definitions,
65 elsif ($phase eq 'Add New Definition'){
66 # display form allowing them to add a new definition
67 $template->param( 'new_dictionary' => 1,
71 elsif ($phase eq 'New Term step 2'){
73 my $areas = C4
::Reports
::Guided
::get_report_areas
();
74 my $definition_name=$input->param('definition_name');
75 my $definition_description=$input->param('definition_description');
76 $template->param( 'step_2' => 1,
78 'definition_name' => $definition_name,
79 'definition_description' => $definition_description,
83 elsif ($phase eq 'New Term step 3'){
84 # Choosing the columns
85 my $area = $input->param('areas');
86 my $columns = get_columns
($area,$input);
87 my $definition_name=$input->param('definition_name');
88 my $definition_description=$input->param('definition_description');
89 $template->param( 'step_3' => 1,
91 'columns' => $columns,
92 'definition_name' => $definition_name,
93 'definition_description' => $definition_description,
97 elsif ($phase eq 'New Term step 4'){
99 my $area=$input->param('area');
100 my $definition_name=$input->param('definition_name');
101 my $definition_description=$input->param('definition_description');
102 my @columns = $input->param('columns');
103 my $columnstring = join (',',@columns);
105 foreach my $column (@columns){
107 $tmp_hash{'name'}=$column;
108 my $type =get_column_type
($column);
109 if ($type eq 'distinct'){
110 my $values = get_distinct_values
($column);
111 $tmp_hash{'values'} = $values;
112 $tmp_hash{'distinct'} = 1;
115 if ($type eq 'DATE' || $type eq 'DATETIME'){
118 if ($type eq 'TEXT'){
124 push @column_loop,\
%tmp_hash;
127 $template->param( 'step_4' => 1,
129 'definition_name' => $definition_name,
130 'definition_description' => $definition_description,
131 'columns' => \
@column_loop,
132 'columnstring' => $columnstring,
133 'DHTMLcalendar_dateformat' => C4
::Dates
->DHTMLcalendar(),
137 elsif ($phase eq 'New Term step 5'){
138 # Confirmation screen
139 my $areas = C4
::Reports
::Guided
::get_report_areas
();
140 my $area = $input->param('area');
141 my $areaname = $areas->[$area - 1]->{'name'};
142 my $columnstring = $input->param('columnstring');
143 my $definition_name=$input->param('definition_name');
144 my $definition_description=$input->param('definition_description');
145 my @criteria = $input->param('criteria_column');
148 foreach my $crit (@criteria) {
149 my $value = $input->param( $crit . "_value" );
152 $tmp_hash{'name'}=$crit;
153 $tmp_hash{'value'} = $value;
154 push @criteria_loop,\
%tmp_hash;
155 if ($value =~ C4
::Dates
->regexp(C4
::Context
->preference('dateformat'))) {
156 my $date = C4
::Dates
->new($value);
157 $value = $date->output("iso");
159 $query_criteria .= " AND $crit='$value'";
161 $value = $input->param( $crit . "_start_value" );
164 $tmp_hash{'name'}="$crit Start";
165 $tmp_hash{'value'} = $value;
166 push @criteria_loop,\
%tmp_hash;
167 if ($value =~ C4
::Dates
->regexp(C4
::Context
->preference('dateformat'))) {
168 my $date = C4
::Dates
->new($value);
169 $value = $date->output("iso");
171 $query_criteria .= " AND $crit >= '$value'";
173 $value = $input->param( $crit . "_end_value" );
176 $tmp_hash{'name'}="$crit End";
177 $tmp_hash{'value'} = $value;
178 push @criteria_loop,\
%tmp_hash;
179 if ($value =~ C4
::Dates
->regexp(C4
::Context
->preference('dateformat'))) {
180 my $date = C4
::Dates
->new($value);
181 $value = $date->output("iso");
183 $query_criteria .= " AND $crit <= '$value'";
186 $template->param( 'step_5' => 1,
188 'areaname' => $areaname,
189 'definition_name' => $definition_name,
190 'definition_description' => $definition_description,
191 'query' => $query_criteria,
192 'columnstring' => $columnstring,
193 'criteria_loop' => \
@criteria_loop,
197 elsif ($phase eq 'New Term step 6'){
199 my $area = $input->param('area');
200 my $definition_name=$input->param('definition_name');
201 my $definition_description=$input->param('definition_description');
202 my $sql=$input->param('sql');
203 save_dictionary
($definition_name,$definition_description,$sql,$area);
205 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
208 elsif ($phase eq 'Delete Definition'){
210 my $id = $input->param('id');
211 delete_definition
($id);
212 print $input->redirect("/cgi-bin/koha/reports/dictionary.pl?phase=View%20Dictionary");
215 $template->param( 'referer' => $referer );
219 output_html_with_http_headers
$input, $cookie, $template->output;