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);
28 Script to control the guided report creation
38 my $referer = $input->referer();
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
41 template_name => "reports/guided_reports_start.tmpl",
45 flagsrequired => { editcatalogue => 1 },
50 my $phase = $input->param('phase');
51 my $no_html = 0; # this will be set if we dont want to print out an html::template
54 $template->param( 'start' => 1 );
59 elsif ( $phase eq 'Build new' ) {
62 $template->param( 'build1' => 1 );
65 my $areas = C4::Reports::get_report_areas();
66 $template->param( 'areas' => $areas );
70 elsif ( $phase eq 'Used saved' ) {
73 # get list of reports and display them
74 $template->param( 'saved1' => 1 );
75 my $reports = get_saved_reports();
76 $template->param( 'savedreports' => $reports );
79 elsif ( $phase eq 'Delete Saved') {
81 # delete a report from the saved reports list
83 my $id = $input->param('reports');
85 print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Used%20saved");
89 elsif ( $phase eq 'Report on this Area' ) {
91 # they have choosen a new report and the area to report on
93 my $area = $input->param('areas');
100 my $types = C4::Reports::get_report_types();
101 $template->param( 'types' => $types );
104 elsif ( $phase eq 'Choose this type' ) {
106 # they have chosen type and area
107 # get area and type and pass them to the template
108 my $area = $input->param('area');
109 my $type = $input->param('types');
117 my $columns = get_columns($area);
118 $template->param( 'columns' => $columns );
121 elsif ( $phase eq 'Choose these columns' ) {
123 # we now know type, area, and columns
124 # next step is the constraints
125 my $area = $input->param('area');
126 my $type = $input->param('type');
127 my @columns = $input->param('columns');
128 my $column = join( ',', @columns );
129 my $definitions = get_from_dictionary($area);
136 my $criteria = get_criteria($area);
137 $template->param( 'criteria' => $criteria,
138 'definitions' => $definitions);
141 elsif ( $phase eq 'Choose these criteria' ) {
142 my $area = $input->param('area');
143 my $type = $input->param('type');
144 my $column = $input->param('column');
145 my @definitions = $input->param('definition');
146 my $definition = join (',',@definitions);
147 my @criteria = $input->param('criteria_column');
149 foreach my $crit (@criteria) {
150 my $value = $input->param( $crit . "_value" );
152 $query_criteria .= " AND $crit='$value'";
161 'definition' => $definition,
162 'criteriastring' => $query_criteria,
166 my @columns = split( ',', $column );
169 # build structue for use by tmpl_loop to choose columns to order by
170 # need to do something about the order of the order :)
171 # we also want to use the %columns hash to get the plain english names
172 foreach my $col (@columns) {
174 $total{'name'} = $col;
177 $select1{'value'} = 'sum';
178 push @selects, \%select1;
180 $select2{'value'} = 'min';
181 push @selects, \%select2;
183 $select3{'value'} = 'max';
184 push @selects, \%select3;
186 $select4{'value'} = 'avg';
187 push @selects, \%select4;
189 $select5{'value'} = 'count';
190 push @selects, \%select5;
192 $total{'select'} = \@selects;
193 push @total_by, \%total;
196 $template->param( 'total_by' => \@total_by );
199 elsif ( $phase eq 'Choose These Operations' ) {
200 my $area = $input->param('area');
201 my $type = $input->param('type');
202 my $column = $input->param('column');
203 my $criteria = $input->param('criteria');
204 my $definition = $input->param('definition');
205 my @total_by = $input->param('total_by');
207 foreach my $total (@total_by) {
208 my $value = $input->param( $total . "_tvalue" );
209 $totals .= "$value($total),";
217 'criteriastring' => $criteria,
219 'definition' => $definition,
223 my @columns = split( ',', $column );
226 # build structue for use by tmpl_loop to choose columns to order by
227 # need to do something about the order of the order :)
228 foreach my $col (@columns) {
230 $order{'name'} = $col;
233 $select1{'value'} = 'asc';
234 push @selects, \%select1;
236 $select2{'value'} = 'desc';
237 push @selects, \%select2;
238 $order{'select'} = \@selects;
239 push @order_by, \%order;
242 $template->param( 'order_by' => \@order_by );
245 elsif ( $phase eq 'Build Report' ) {
247 # now we have all the info we need and can build the sql
248 my $area = $input->param('area');
249 my $type = $input->param('type');
250 my $column = $input->param('column');
251 my $crit = $input->param('criteria');
252 my $totals = $input->param('totals');
253 my $definition = $input->param('definition');
254 # my @criteria = split( ',', $crit );
255 my $query_criteria=$crit;
256 # split the columns up by ,
257 my @columns = split( ',', $column );
258 my @order_by = $input->param('order_by');
261 foreach my $order (@order_by) {
262 my $value = $input->param( $order . "_ovalue" );
263 if ($query_orderby) {
264 $query_orderby .= ",$order $value";
267 $query_orderby = " ORDER BY $order $value";
273 build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
281 elsif ( $phase eq 'Save' ) {
282 # Save the report that has just been built
283 my $sql = $input->param('sql');
284 my $type = $input->param('type');
292 elsif ( $phase eq 'Save Report' ) {
293 # save the sql pasted in by a user
294 my $sql = $input->param('sql');
295 my $name = $input->param('reportname');
296 my $type = $input->param('type');
297 my $notes = $input->param('notes');
298 save_report( $sql, $name, $type, $notes );
301 elsif ( $phase eq 'Execute' ) {
302 # run the sql, and output results in a template
303 my $sql = $input->param('sql');
304 my $type = $input->param('type');
305 my $results = execute_query($sql,$type);
307 'results' => $results,
313 elsif ($phase eq 'Run this report'){
314 # execute a saved report
315 my $report = $input->param('reports');
316 my ($sql,$type) = get_saved_report($report);
317 my $results = execute_query($sql,$type);
319 'results' => $results,
325 elsif ($phase eq 'Export'){
326 # export results to tab separated text
327 my $sql = $input->param('sql');
329 print $input->header( -type => 'application/octet-stream',
330 -attachment=>'reportresults.csv');
331 my $format=$input->param('format');
332 my $results = execute_query($sql,1,$format);
337 elsif ($phase eq 'Create report from SQL'){
338 # alllow the user to paste in sql
339 $template->param('create' => 1);
340 my $types = C4::Reports::get_report_types();
341 $template->param( 'types' => $types );
344 elsif ($phase eq 'Create Compound Report'){
345 my $reports = get_saved_reports();
346 $template->param( 'savedreports' => $reports,
351 elsif ($phase eq 'Save Compound'){
352 my $master = $input->param('master');
353 my $subreport = $input->param('subreport');
354 # my $compound_report = create_compound($master,$subreport);
355 # my $results = run_compound($compound_report);
356 my ($mastertables,$subtables) = create_compound($master,$subreport);
357 $template->param( 'save_compound' => 1,
358 master=>$mastertables,
364 $template->param( 'referer' => $referer );
368 output_html_with_http_headers $input, $cookie, $template->output;