Bug 8439 - Printing basketgroup does not work on plack
[koha.git] / reports / guided_reports.pl
blob1112274a2c286c6736a2789b563a0949f2126e4c
1 #!/usr/bin/perl
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
10 # version.
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.
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use CGI;
23 use Text::CSV;
24 use URI::Escape;
25 use C4::Reports::Guided;
26 use C4::Auth qw/:DEFAULT get_session/;
27 use C4::Output;
28 use C4::Dates;
29 use C4::Debug;
30 use C4::Branch; # XXX subfield_is_koha_internal_p
32 =head1 NAME
34 guided_reports.pl
36 =head1 DESCRIPTION
38 Script to control the guided report creation
40 =cut
42 my $input = new CGI;
43 my $usecache = C4::Context->ismemcached;
45 my $phase = $input->param('phase');
46 my $flagsrequired;
47 if ( $phase eq 'Build new' or $phase eq 'Delete Saved' ) {
48 $flagsrequired = 'create_reports';
50 elsif ( $phase eq 'Use saved' ) {
51 $flagsrequired = 'execute_reports';
52 } else {
53 $flagsrequired = '*';
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
58 template_name => "reports/guided_reports_start.tmpl",
59 query => $input,
60 type => "intranet",
61 authnotrequired => 0,
62 flagsrequired => { reports => $flagsrequired },
63 debug => 1,
66 my $session = $cookie ? get_session($cookie->value) : undef;
68 my $filter;
69 if ( $input->param("filter_set") ) {
70 $filter = {};
71 $filter->{$_} = $input->param("filter_$_") foreach qw/date author keyword/;
72 $session->param('report_filter', $filter) if $session;
73 $template->param( 'filter_set' => 1 );
75 elsif ($session) {
76 $filter = $session->param('report_filter');
80 my @errors = ();
81 if ( !$phase ) {
82 $template->param( 'start' => 1 );
83 # show welcome page
85 elsif ( $phase eq 'Build new' ) {
86 # build a new report
87 $template->param( 'build1' => 1 );
88 $template->param( 'areas' => get_report_areas(), 'usecache' => $usecache, 'cache_expiry' => 300, 'public' => '0' );
90 elsif ( $phase eq 'Use saved' ) {
91 # use a saved report
92 # get list of reports and display them
93 $template->param(
94 'saved1' => 1,
95 'savedreports' => get_saved_reports($filter),
96 'usecache' => $usecache,
98 if ($filter) {
99 while ( my ($k, $v) = each %$filter ) {
100 $template->param( "filter_$k" => $v ) if $v;
105 elsif ( $phase eq 'Delete Saved') {
107 # delete a report from the saved reports list
108 my $id = $input->param('reports');
109 delete_report($id);
110 print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved");
111 exit;
114 elsif ( $phase eq 'Show SQL'){
116 my $id = $input->param('reports');
117 my ($sql,$type,$reportname,$notes) = get_saved_report($id);
118 $template->param(
119 'id' => $id,
120 'reportname' => $reportname,
121 'notes' => $notes,
122 'sql' => $sql,
123 'showsql' => 1,
127 elsif ( $phase eq 'Edit SQL'){
129 my $id = $input->param('reports');
130 my ($sql,$type,$reportname,$notes, $cache_expiry, $public) = get_saved_report($id);
131 $template->param(
132 'sql' => $sql,
133 'reportname' => $reportname,
134 'notes' => $notes,
135 'id' => $id,
136 'cache_expiry' => $cache_expiry,
137 'public' => $public,
138 'usecache' => $usecache,
139 'editsql' => 1,
143 elsif ( $phase eq 'Update SQL'){
144 my $id = $input->param('id');
145 my $sql = $input->param('sql');
146 my $reportname = $input->param('reportname');
147 my $notes = $input->param('notes');
148 my $cache_expiry = $input->param('cache_expiry');
149 my $cache_expiry_units = $input->param('cache_expiry_units');
150 my $public = $input->param('public');
152 my @errors;
154 # if we have the units, then we came from creating a report from SQL and thus need to handle converting units
155 if( $cache_expiry_units ){
156 if( $cache_expiry_units eq "minutes" ){
157 $cache_expiry *= 60;
158 } elsif( $cache_expiry_units eq "hours" ){
159 $cache_expiry *= 3600; # 60 * 60
160 } elsif( $cache_expiry_units eq "days" ){
161 $cache_expiry *= 86400; # 60 * 60 * 24
164 # check $cache_expiry isnt too large, Memcached::set requires it to be less than 30 days or it will be treated as if it were an absolute time stamp
165 if( $cache_expiry >= 2592000 ){
166 push @errors, {cache_expiry => $cache_expiry};
169 if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
170 push @errors, {sqlerr => $1};
172 elsif ($sql !~ /^(SELECT)/i) {
173 push @errors, {queryerr => 1};
175 if (@errors) {
176 $template->param(
177 'errors' => \@errors,
178 'sql' => $sql,
181 else {
182 update_sql( $id, $sql, $reportname, $notes, $cache_expiry, $public );
183 $template->param(
184 'save_successful' => 1,
185 'reportname' => $reportname,
186 'id' => $id,
192 elsif ($phase eq 'retrieve results') {
193 my $id = $input->param('id');
194 my ($results,$name,$notes) = format_results($id);
195 # do something
196 $template->param(
197 'retresults' => 1,
198 'results' => $results,
199 'name' => $name,
200 'notes' => $notes,
204 elsif ( $phase eq 'Report on this Area' ) {
205 my $cache_expiry_units = $input->param('cache_expiry_units'),
206 my $cache_expiry = $input->param('cache_expiry');
208 # we need to handle converting units
209 if( $cache_expiry_units eq "minutes" ){
210 $cache_expiry *= 60;
211 } elsif( $cache_expiry_units eq "hours" ){
212 $cache_expiry *= 3600; # 60 * 60
213 } elsif( $cache_expiry_units eq "days" ){
214 $cache_expiry *= 86400; # 60 * 60 * 24
216 # check $cache_expiry isnt too large, Memcached::set requires it to be less than 30 days or it will be treated as if it were an absolute time stamp
217 if( $cache_expiry >= 2592000 ){ # oops, over the limit of 30 days
218 # report error to user
219 $template->param(
220 'cache_error' => 1,
221 'build1' => 1,
222 'areas' => get_report_areas(),
223 'cache_expiry' => $cache_expiry,
224 'usecache' => $usecache,
225 'public' => $input->param('public'),
227 } else {
228 # they have choosen a new report and the area to report on
229 $template->param(
230 'build2' => 1,
231 'area' => $input->param('areas'),
232 'types' => get_report_types(),
233 'cache_expiry' => $cache_expiry,
234 'public' => $input->param('public'),
239 elsif ( $phase eq 'Choose this type' ) {
240 # they have chosen type and area
241 # get area and type and pass them to the template
242 my $area = $input->param('area');
243 my $type = $input->param('types');
244 $template->param(
245 'build3' => 1,
246 'area' => $area,
247 'type' => $type,
248 columns => get_columns($area,$input),
249 'cache_expiry' => $input->param('cache_expiry'),
250 'public' => $input->param('public'),
254 elsif ( $phase eq 'Choose these columns' ) {
255 # we now know type, area, and columns
256 # next step is the constraints
257 my $area = $input->param('area');
258 my $type = $input->param('type');
259 my @columns = $input->param('columns');
260 my $column = join( ',', @columns );
261 $template->param(
262 'build4' => 1,
263 'area' => $area,
264 'type' => $type,
265 'column' => $column,
266 definitions => get_from_dictionary($area),
267 criteria => get_criteria($area,$input),
268 'cache_expiry' => $input->param('cache_expiry'),
269 'cache_expiry_units' => $input->param('cache_expiry_units'),
270 'public' => $input->param('public'),
274 elsif ( $phase eq 'Choose these criteria' ) {
275 my $area = $input->param('area');
276 my $type = $input->param('type');
277 my $column = $input->param('column');
278 my @definitions = $input->param('definition');
279 my $definition = join (',',@definitions);
280 my @criteria = $input->param('criteria_column');
281 my $query_criteria;
282 foreach my $crit (@criteria) {
283 my $value = $input->param( $crit . "_value" );
285 # If value is not defined, then it may be range values
286 if (!defined $value) {
288 my $fromvalue = $input->param( "from_" . $crit . "_value" );
289 my $tovalue = $input->param( "to_" . $crit . "_value" );
291 # If the range values are dates
292 if ($fromvalue =~ C4::Dates->regexp('syspref') && $tovalue =~ C4::Dates->regexp('syspref')) {
293 $fromvalue = C4::Dates->new($fromvalue)->output("iso");
294 $tovalue = C4::Dates->new($tovalue)->output("iso");
297 if ($fromvalue && $tovalue) {
298 $query_criteria .= " AND $crit >= '$fromvalue' AND $crit <= '$tovalue'";
301 } else {
303 # If value is a date
304 if ($value =~ C4::Dates->regexp('syspref')) {
305 $value = C4::Dates->new($value)->output("iso");
307 # don't escape runtime parameters, they'll be at runtime
308 if ($value =~ /<<.*>>/) {
309 $query_criteria .= " AND $crit=$value";
310 } else {
311 $query_criteria .= " AND $crit='$value'";
315 $template->param(
316 'build5' => 1,
317 'area' => $area,
318 'type' => $type,
319 'column' => $column,
320 'definition' => $definition,
321 'criteriastring' => $query_criteria,
322 'cache_expiry' => $input->param('cache_expiry'),
323 'cache_expiry_units' => $input->param('cache_expiry_units'),
324 'public' => $input->param('public'),
327 # get columns
328 my @columns = split( ',', $column );
329 my @total_by;
331 # build structue for use by tmpl_loop to choose columns to order by
332 # need to do something about the order of the order :)
333 # we also want to use the %columns hash to get the plain english names
334 foreach my $col (@columns) {
335 my %total = (name => $col);
336 my @selects = map {+{ value => $_ }} (qw(sum min max avg count));
337 $total{'select'} = \@selects;
338 push @total_by, \%total;
341 $template->param( 'total_by' => \@total_by );
344 elsif ( $phase eq 'Choose these operations' ) {
345 my $area = $input->param('area');
346 my $type = $input->param('type');
347 my $column = $input->param('column');
348 my $criteria = $input->param('criteria');
349 my $definition = $input->param('definition');
350 my @total_by = $input->param('total_by');
351 my $totals;
352 foreach my $total (@total_by) {
353 my $value = $input->param( $total . "_tvalue" );
354 $totals .= "$value($total),";
357 $template->param(
358 'build6' => 1,
359 'area' => $area,
360 'type' => $type,
361 'column' => $column,
362 'criteriastring' => $criteria,
363 'totals' => $totals,
364 'definition' => $definition,
365 'cache_expiry' => $input->param('cache_expiry'),
366 'public' => $input->param('public'),
369 # get columns
370 my @columns = split( ',', $column );
371 my @order_by;
373 # build structue for use by tmpl_loop to choose columns to order by
374 # need to do something about the order of the order :)
375 foreach my $col (@columns) {
376 my %order = (name => $col);
377 my @selects = map {+{ value => $_ }} (qw(asc desc));
378 $order{'select'} = \@selects;
379 push @order_by, \%order;
382 $template->param( 'order_by' => \@order_by );
385 elsif ( $phase eq 'Build report' ) {
387 # now we have all the info we need and can build the sql
388 my $area = $input->param('area');
389 my $type = $input->param('type');
390 my $column = $input->param('column');
391 my $crit = $input->param('criteria');
392 my $totals = $input->param('totals');
393 my $definition = $input->param('definition');
394 my $query_criteria=$crit;
395 # split the columns up by ,
396 my @columns = split( ',', $column );
397 my @order_by = $input->param('order_by');
399 my $query_orderby;
400 foreach my $order (@order_by) {
401 my $value = $input->param( $order . "_ovalue" );
402 if ($query_orderby) {
403 $query_orderby .= ",$order $value";
405 else {
406 $query_orderby = " ORDER BY $order $value";
410 # get the sql
411 my $sql =
412 build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
413 $template->param(
414 'showreport' => 1,
415 'sql' => $sql,
416 'type' => $type,
417 'cache_expiry' => $input->param('cache_expiry'),
418 'public' => $input->param('public'),
422 elsif ( $phase eq 'Save' ) {
423 # Save the report that has just been built
424 my $sql = $input->param('sql');
425 my $type = $input->param('type');
426 $template->param(
427 'save' => 1,
428 'sql' => $sql,
429 'type' => $type,
430 'cache_expiry' => $input->param('cache_expiry'),
431 'public' => $input->param('public'),
435 elsif ( $phase eq 'Save Report' ) {
436 # save the sql pasted in by a user
437 my $sql = $input->param('sql');
438 my $name = $input->param('reportname');
439 my $type = $input->param('types');
440 my $notes = $input->param('notes');
441 my $cache_expiry = $input->param('cache_expiry');
442 my $cache_expiry_units = $input->param('cache_expiry_units');
443 my $public = $input->param('public');
446 # if we have the units, then we came from creating a report from SQL and thus need to handle converting units
447 if( $cache_expiry_units ){
448 if( $cache_expiry_units eq "minutes" ){
449 $cache_expiry *= 60;
450 } elsif( $cache_expiry_units eq "hours" ){
451 $cache_expiry *= 3600; # 60 * 60
452 } elsif( $cache_expiry_units eq "days" ){
453 $cache_expiry *= 86400; # 60 * 60 * 24
456 # check $cache_expiry isnt too large, Memcached::set requires it to be less than 30 days or it will be treated as if it were an absolute time stamp
457 if( $cache_expiry >= 2592000 ){
458 push @errors, {cache_expiry => $cache_expiry};
460 ## FIXME this is AFTER entering a name to save the report under
461 if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
462 push @errors, {sqlerr => $1};
464 elsif ($sql !~ /^(SELECT)/i) {
465 push @errors, {queryerr => 1};
467 if (@errors) {
468 $template->param(
469 'errors' => \@errors,
470 'sql' => $sql,
471 'reportname'=> $name,
472 'type' => $type,
473 'notes' => $notes,
474 'cache_expiry' => $cache_expiry,
475 'public' => $public,
478 else {
479 my $id = save_report( $borrowernumber, $sql, $name, $type, $notes, $cache_expiry, $public );
480 $template->param(
481 'save_successful' => 1,
482 'reportname' => $name,
483 'id' => $id,
488 elsif ($phase eq 'Run this report'){
489 # execute a saved report
490 my $limit = 20; # page size. # TODO: move to DB or syspref?
491 my $offset = 0;
492 my $report = $input->param('reports');
493 my @sql_params = $input->param('sql_params');
494 # offset algorithm
495 if ($input->param('page')) {
496 $offset = ($input->param('page') - 1) * $limit;
498 my ($sql,$type,$name,$notes) = get_saved_report($report);
499 unless ($sql) {
500 push @errors, {no_sql_for_id=>$report};
502 my @rows = ();
503 # if we have at least 1 parameter, and it's not filled, then don't execute but ask for parameters
504 if ($sql =~ /<</ && !@sql_params) {
505 # split on ??. Each odd (2,4,6,...) entry should be a parameter to fill
506 my @split = split /<<|>>/,$sql;
507 my @tmpl_parameters;
508 for(my $i=0;$i<($#split/2);$i++) {
509 my ($text,$authorised_value) = split /\|/,$split[$i*2+1];
510 my $input;
511 if ($authorised_value eq "date") {
512 $input = 'date';
514 elsif ($authorised_value) {
515 my $dbh=C4::Context->dbh;
516 my @authorised_values;
517 my %authorised_lib;
518 # builds list, depending on authorised value...
519 if ( $authorised_value eq "branches" ) {
520 my $branches = GetBranchesLoop();
521 foreach my $thisbranch (@$branches) {
522 push @authorised_values, $thisbranch->{value};
523 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
526 elsif ( $authorised_value eq "itemtypes" ) {
527 my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
528 $sth->execute;
529 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
530 push @authorised_values, $itemtype;
531 $authorised_lib{$itemtype} = $description;
534 elsif ( $authorised_value eq "cn_source" ) {
535 my $class_sources = GetClassSources();
536 my $default_source = C4::Context->preference("DefaultClassificationSource");
537 foreach my $class_source (sort keys %$class_sources) {
538 next unless $class_sources->{$class_source}->{'used'} or
539 ($class_source eq $default_source);
540 push @authorised_values, $class_source;
541 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
544 elsif ( $authorised_value eq "categorycode" ) {
545 my $sth = $dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description");
546 $sth->execute;
547 while ( my ( $categorycode, $description ) = $sth->fetchrow_array ) {
548 push @authorised_values, $categorycode;
549 $authorised_lib{$categorycode} = $description;
552 #---- "true" authorised value
554 else {
555 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
557 $authorised_values_sth->execute( $authorised_value);
559 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
560 push @authorised_values, $value;
561 $authorised_lib{$value} = $lib;
562 # For item location, we show the code and the libelle
563 $authorised_lib{$value} = $lib;
566 $input =CGI::scrolling_list( # FIXME: factor out scrolling_list
567 -name => "sql_params",
568 -values => \@authorised_values,
569 # -default => $value,
570 -labels => \%authorised_lib,
571 -override => 1,
572 -size => 1,
573 -multiple => 0,
574 -tabindex => 1,
577 } else {
578 $input = "<input type='text' name='sql_params'/>";
580 push @tmpl_parameters, {'entry' => $text, 'input' => $input };
582 $template->param('sql' => $sql,
583 'name' => $name,
584 'sql_params' => \@tmpl_parameters,
585 'enter_params' => 1,
586 'reports' => $report,
588 } else {
589 # OK, we have parameters, or there are none, we run the report
590 # if there were parameters, replace before running
591 # split on ??. Each odd (2,4,6,...) entry should be a parameter to fill
592 my @split = split /<<|>>/,$sql;
593 my @tmpl_parameters;
594 for(my $i=0;$i<$#split/2;$i++) {
595 my $quoted = C4::Context->dbh->quote($sql_params[$i]);
596 # if there are special regexp chars, we must \ them
597 $split[$i*2+1] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g;
598 $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
600 my ($sth, $errors) = execute_query($sql, $offset, $limit);
601 my $total = nb_rows($sql) || 0;
602 unless ($sth) {
603 die "execute_query failed to return sth for report $report: $sql";
604 } else {
605 my $headref = $sth->{NAME} || [];
606 my @headers = map { +{ cell => $_ } } @$headref;
607 $template->param(header_row => \@headers);
608 while (my $row = $sth->fetchrow_arrayref()) {
609 my @cells = map { +{ cell => $_ } } @$row;
610 push @rows, { cells => \@cells };
614 my $totpages = int($total/$limit) + (($total % $limit) > 0 ? 1 : 0);
615 my $url = "/cgi-bin/koha/reports/guided_reports.pl?reports=$report&amp;phase=Run%20this%20report";
616 if (@sql_params) {
617 $url = join('&amp;sql_params=', $url, map { URI::Escape::uri_escape($_) } @sql_params);
619 $template->param(
620 'results' => \@rows,
621 'sql' => $sql,
622 'id' => $report,
623 'execute' => 1,
624 'name' => $name,
625 'notes' => $notes,
626 'errors' => $errors,
627 'pagination_bar' => pagination_bar($url, $totpages, $input->param('page')),
628 'unlimited_total' => $total,
633 elsif ($phase eq 'Export'){
634 binmode STDOUT, ':encoding(UTF-8)';
636 # export results to tab separated text or CSV
637 my $sql = $input->param('sql'); # FIXME: use sql from saved report ID#, not new user-supplied SQL!
638 my $format = $input->param('format');
639 my ($sth, $q_errors) = execute_query($sql);
640 unless ($q_errors and @$q_errors) {
641 print $input->header( -type => 'application/octet-stream',
642 -attachment=>"reportresults.$format"
644 if ($format eq 'tab') {
645 print join("\t", header_cell_values($sth)), "\n";
646 while (my $row = $sth->fetchrow_arrayref()) {
647 print join("\t", @$row), "\n";
649 } else {
650 my $csv = Text::CSV->new({binary => 1});
651 $csv or die "Text::CSV->new({binary => 1}) FAILED: " . Text::CSV->error_diag();
652 if ($csv->combine(header_cell_values($sth))) {
653 print $csv->string(), "\n";
654 } else {
655 push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ;
657 while (my $row = $sth->fetchrow_arrayref()) {
658 if ($csv->combine(@$row)) {
659 print $csv->string(), "\n";
660 } else {
661 push @$q_errors, { combine => $csv->error_diag() } ;
665 foreach my $err (@$q_errors, @errors) {
666 print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n";
667 } # here we print all the non-fatal errors at the end. Not super smooth, but better than nothing.
668 exit;
670 $template->param(
671 'sql' => $sql,
672 'execute' => 1,
673 'name' => 'Error exporting report!',
674 'notes' => '',
675 'errors' => $q_errors,
679 elsif ($phase eq 'Create report from SQL') {
680 # allow the user to paste in sql
681 if ($input->param('sql')) {
682 $template->param(
683 'sql' => $input->param('sql'),
684 'reportname' => $input->param('reportname'),
685 'notes' => $input->param('notes'),
688 $template->param('create' => 1, 'public' => '0', 'cache_expiry' => 300, 'usecache' => $usecache);
691 elsif ($phase eq 'Create Compound Report'){
692 $template->param( 'savedreports' => get_saved_reports(),
693 'compound' => 1,
697 elsif ($phase eq 'Save Compound'){
698 my $master = $input->param('master');
699 my $subreport = $input->param('subreport');
700 my ($mastertables,$subtables) = create_compound($master,$subreport);
701 $template->param( 'save_compound' => 1,
702 master=>$mastertables,
703 subsql=>$subtables
707 # pass $sth, get back an array of names for the column headers
708 sub header_cell_values {
709 my $sth = shift or return ();
710 return @{$sth->{NAME}};
713 # pass $sth, get back a TMPL_LOOP-able set of names for the column headers
714 sub header_cell_loop {
715 my @headers = map { +{ cell => $_ } } header_cell_values (shift);
716 return \@headers;
719 foreach (1..6) {
720 $template->{VARS}->{'build' . $_} and $template->{VARS}->{'buildx' . $_} and last;
722 $template->param( 'referer' => $input->referer(),
723 'DHTMLcalendar_dateformat' => C4::Dates->DHTMLcalendar(),
726 output_html_with_http_headers $input, $cookie, $template->output;