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 'retrieve results') {
90 my $id = $input->param('id');
91 my $results = format_results($id);
95 'results' => $results,
100 elsif ( $phase eq 'Report on this Area' ) {
102 # they have choosen a new report and the area to report on
104 my $area = $input->param('areas');
111 my $types = C4::Reports::get_report_types();
112 $template->param( 'types' => $types );
115 elsif ( $phase eq 'Choose this type' ) {
117 # they have chosen type and area
118 # get area and type and pass them to the template
119 my $area = $input->param('area');
120 my $type = $input->param('types');
128 my $columns = get_columns($area);
129 $template->param( 'columns' => $columns );
132 elsif ( $phase eq 'Choose these columns' ) {
134 # we now know type, area, and columns
135 # next step is the constraints
136 my $area = $input->param('area');
137 my $type = $input->param('type');
138 my @columns = $input->param('columns');
139 my $column = join( ',', @columns );
140 my $definitions = get_from_dictionary($area);
147 my $criteria = get_criteria($area);
148 $template->param( 'criteria' => $criteria,
149 'definitions' => $definitions);
152 elsif ( $phase eq 'Choose these criteria' ) {
153 my $area = $input->param('area');
154 my $type = $input->param('type');
155 my $column = $input->param('column');
156 my @definitions = $input->param('definition');
157 my $definition = join (',',@definitions);
158 my @criteria = $input->param('criteria_column');
160 foreach my $crit (@criteria) {
161 my $value = $input->param( $crit . "_value" );
163 $query_criteria .= " AND $crit='$value'";
172 'definition' => $definition,
173 'criteriastring' => $query_criteria,
177 my @columns = split( ',', $column );
180 # build structue for use by tmpl_loop to choose columns to order by
181 # need to do something about the order of the order :)
182 # we also want to use the %columns hash to get the plain english names
183 foreach my $col (@columns) {
185 $total{'name'} = $col;
188 $select1{'value'} = 'sum';
189 push @selects, \%select1;
191 $select2{'value'} = 'min';
192 push @selects, \%select2;
194 $select3{'value'} = 'max';
195 push @selects, \%select3;
197 $select4{'value'} = 'avg';
198 push @selects, \%select4;
200 $select5{'value'} = 'count';
201 push @selects, \%select5;
203 $total{'select'} = \@selects;
204 push @total_by, \%total;
207 $template->param( 'total_by' => \@total_by );
210 elsif ( $phase eq 'Choose These Operations' ) {
211 my $area = $input->param('area');
212 my $type = $input->param('type');
213 my $column = $input->param('column');
214 my $criteria = $input->param('criteria');
215 my $definition = $input->param('definition');
216 my @total_by = $input->param('total_by');
218 foreach my $total (@total_by) {
219 my $value = $input->param( $total . "_tvalue" );
220 $totals .= "$value($total),";
228 'criteriastring' => $criteria,
230 'definition' => $definition,
234 my @columns = split( ',', $column );
237 # build structue for use by tmpl_loop to choose columns to order by
238 # need to do something about the order of the order :)
239 foreach my $col (@columns) {
241 $order{'name'} = $col;
244 $select1{'value'} = 'asc';
245 push @selects, \%select1;
247 $select2{'value'} = 'desc';
248 push @selects, \%select2;
249 $order{'select'} = \@selects;
250 push @order_by, \%order;
253 $template->param( 'order_by' => \@order_by );
256 elsif ( $phase eq 'Build Report' ) {
258 # now we have all the info we need and can build the sql
259 my $area = $input->param('area');
260 my $type = $input->param('type');
261 my $column = $input->param('column');
262 my $crit = $input->param('criteria');
263 my $totals = $input->param('totals');
264 my $definition = $input->param('definition');
265 # my @criteria = split( ',', $crit );
266 my $query_criteria=$crit;
267 # split the columns up by ,
268 my @columns = split( ',', $column );
269 my @order_by = $input->param('order_by');
272 foreach my $order (@order_by) {
273 my $value = $input->param( $order . "_ovalue" );
274 if ($query_orderby) {
275 $query_orderby .= ",$order $value";
278 $query_orderby = " ORDER BY $order $value";
284 build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
292 elsif ( $phase eq 'Save' ) {
293 # Save the report that has just been built
294 my $sql = $input->param('sql');
295 my $type = $input->param('type');
303 elsif ( $phase eq 'Save Report' ) {
304 # save the sql pasted in by a user
305 my $sql = $input->param('sql');
306 my $name = $input->param('reportname');
307 my $type = $input->param('type');
308 my $notes = $input->param('notes');
309 save_report( $sql, $name, $type, $notes );
312 elsif ( $phase eq 'Execute' ) {
313 # run the sql, and output results in a template
314 my $sql = $input->param('sql');
315 my $type = $input->param('type');
316 my $results = execute_query($sql,$type);
318 'results' => $results,
324 elsif ($phase eq 'Run this report'){
325 # execute a saved report
326 my $report = $input->param('reports');
327 my ($sql,$type) = get_saved_report($report);
328 my $results = execute_query($sql,$type);
330 'results' => $results,
336 elsif ($phase eq 'Export'){
337 # export results to tab separated text
338 my $sql = $input->param('sql');
340 print $input->header( -type => 'application/octet-stream',
341 -attachment=>'reportresults.csv');
342 my $format=$input->param('format');
343 my $results = execute_query($sql,1,$format);
348 elsif ($phase eq 'Create report from SQL'){
349 # alllow the user to paste in sql
350 $template->param('create' => 1);
351 my $types = C4::Reports::get_report_types();
352 $template->param( 'types' => $types );
355 elsif ($phase eq 'Create Compound Report'){
356 my $reports = get_saved_reports();
357 $template->param( 'savedreports' => $reports,
362 elsif ($phase eq 'Save Compound'){
363 my $master = $input->param('master');
364 my $subreport = $input->param('subreport');
365 # my $compound_report = create_compound($master,$subreport);
366 # my $results = run_compound($compound_report);
367 my ($mastertables,$subtables) = create_compound($master,$subreport);
368 $template->param( 'save_compound' => 1,
369 master=>$mastertables,
375 $template->param( 'referer' => $referer );
379 output_html_with_http_headers $input, $cookie, $template->output;