Bug 8485 - Make koha_perl_deps.pl batch friendly Added a -b flag for brief which...
[koha.git] / reports / guided_reports.pl
blob8a2aada943d9e80b5c010c24f0c91e8652870b2a
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 my $labelid;
512 if ($authorised_value eq "date") {
513 $input = 'date';
515 elsif ($authorised_value) {
516 my $dbh=C4::Context->dbh;
517 my @authorised_values;
518 my %authorised_lib;
519 # builds list, depending on authorised value...
520 if ( $authorised_value eq "branches" ) {
521 my $branches = GetBranchesLoop();
522 foreach my $thisbranch (@$branches) {
523 push @authorised_values, $thisbranch->{value};
524 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
527 elsif ( $authorised_value eq "itemtypes" ) {
528 my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
529 $sth->execute;
530 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
531 push @authorised_values, $itemtype;
532 $authorised_lib{$itemtype} = $description;
535 elsif ( $authorised_value eq "cn_source" ) {
536 my $class_sources = GetClassSources();
537 my $default_source = C4::Context->preference("DefaultClassificationSource");
538 foreach my $class_source (sort keys %$class_sources) {
539 next unless $class_sources->{$class_source}->{'used'} or
540 ($class_source eq $default_source);
541 push @authorised_values, $class_source;
542 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
545 elsif ( $authorised_value eq "categorycode" ) {
546 my $sth = $dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description");
547 $sth->execute;
548 while ( my ( $categorycode, $description ) = $sth->fetchrow_array ) {
549 push @authorised_values, $categorycode;
550 $authorised_lib{$categorycode} = $description;
553 #---- "true" authorised value
555 else {
556 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
558 $authorised_values_sth->execute( $authorised_value);
560 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
561 push @authorised_values, $value;
562 $authorised_lib{$value} = $lib;
563 # For item location, we show the code and the libelle
564 $authorised_lib{$value} = $lib;
567 $labelid = $text;
568 $labelid =~ s/\W//g;
569 $input =CGI::scrolling_list( # FIXME: factor out scrolling_list
570 -name => "sql_params",
571 -id => "sql_params_".$labelid,
572 -values => \@authorised_values,
573 # -default => $value,
574 -labels => \%authorised_lib,
575 -override => 1,
576 -size => 1,
577 -multiple => 0,
578 -tabindex => 1,
581 } else {
582 $input = "text";
584 push @tmpl_parameters, {'entry' => $text, 'input' => $input, 'labelid' => $labelid };
586 $template->param('sql' => $sql,
587 'name' => $name,
588 'sql_params' => \@tmpl_parameters,
589 'enter_params' => 1,
590 'reports' => $report,
592 } else {
593 # OK, we have parameters, or there are none, we run the report
594 # if there were parameters, replace before running
595 # split on ??. Each odd (2,4,6,...) entry should be a parameter to fill
596 my @split = split /<<|>>/,$sql;
597 my @tmpl_parameters;
598 for(my $i=0;$i<$#split/2;$i++) {
599 my $quoted = C4::Context->dbh->quote($sql_params[$i]);
600 # if there are special regexp chars, we must \ them
601 $split[$i*2+1] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g;
602 $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
604 my ($sth, $errors) = execute_query($sql, $offset, $limit);
605 my $total = nb_rows($sql) || 0;
606 unless ($sth) {
607 die "execute_query failed to return sth for report $report: $sql";
608 } else {
609 my $headref = $sth->{NAME} || [];
610 my @headers = map { +{ cell => $_ } } @$headref;
611 $template->param(header_row => \@headers);
612 while (my $row = $sth->fetchrow_arrayref()) {
613 my @cells = map { +{ cell => $_ } } @$row;
614 push @rows, { cells => \@cells };
618 my $totpages = int($total/$limit) + (($total % $limit) > 0 ? 1 : 0);
619 my $url = "/cgi-bin/koha/reports/guided_reports.pl?reports=$report&amp;phase=Run%20this%20report";
620 if (@sql_params) {
621 $url = join('&amp;sql_params=', $url, map { URI::Escape::uri_escape($_) } @sql_params);
623 $template->param(
624 'results' => \@rows,
625 'sql' => $sql,
626 'id' => $report,
627 'execute' => 1,
628 'name' => $name,
629 'notes' => $notes,
630 'errors' => $errors,
631 'pagination_bar' => pagination_bar($url, $totpages, $input->param('page')),
632 'unlimited_total' => $total,
637 elsif ($phase eq 'Export'){
638 binmode STDOUT, ':encoding(UTF-8)';
640 # export results to tab separated text or CSV
641 my $sql = $input->param('sql'); # FIXME: use sql from saved report ID#, not new user-supplied SQL!
642 my $format = $input->param('format');
643 my ($sth, $q_errors) = execute_query($sql);
644 unless ($q_errors and @$q_errors) {
645 print $input->header( -type => 'application/octet-stream',
646 -attachment=>"reportresults.$format"
648 if ($format eq 'tab') {
649 print join("\t", header_cell_values($sth)), "\n";
650 while (my $row = $sth->fetchrow_arrayref()) {
651 print join("\t", @$row), "\n";
653 } else {
654 my $csv = Text::CSV->new({binary => 1});
655 $csv or die "Text::CSV->new({binary => 1}) FAILED: " . Text::CSV->error_diag();
656 if ($csv->combine(header_cell_values($sth))) {
657 print $csv->string(), "\n";
658 } else {
659 push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ;
661 while (my $row = $sth->fetchrow_arrayref()) {
662 if ($csv->combine(@$row)) {
663 print $csv->string(), "\n";
664 } else {
665 push @$q_errors, { combine => $csv->error_diag() } ;
669 foreach my $err (@$q_errors, @errors) {
670 print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n";
671 } # here we print all the non-fatal errors at the end. Not super smooth, but better than nothing.
672 exit;
674 $template->param(
675 'sql' => $sql,
676 'execute' => 1,
677 'name' => 'Error exporting report!',
678 'notes' => '',
679 'errors' => $q_errors,
683 elsif ($phase eq 'Create report from SQL') {
684 # allow the user to paste in sql
685 if ($input->param('sql')) {
686 $template->param(
687 'sql' => $input->param('sql'),
688 'reportname' => $input->param('reportname'),
689 'notes' => $input->param('notes'),
692 $template->param('create' => 1, 'public' => '0', 'cache_expiry' => 300, 'usecache' => $usecache);
695 elsif ($phase eq 'Create Compound Report'){
696 $template->param( 'savedreports' => get_saved_reports(),
697 'compound' => 1,
701 elsif ($phase eq 'Save Compound'){
702 my $master = $input->param('master');
703 my $subreport = $input->param('subreport');
704 my ($mastertables,$subtables) = create_compound($master,$subreport);
705 $template->param( 'save_compound' => 1,
706 master=>$mastertables,
707 subsql=>$subtables
711 # pass $sth, get back an array of names for the column headers
712 sub header_cell_values {
713 my $sth = shift or return ();
714 return @{$sth->{NAME}};
717 # pass $sth, get back a TMPL_LOOP-able set of names for the column headers
718 sub header_cell_loop {
719 my @headers = map { +{ cell => $_ } } header_cell_values (shift);
720 return \@headers;
723 foreach (1..6) {
724 $template->{VARS}->{'build' . $_} and $template->{VARS}->{'buildx' . $_} and last;
726 $template->param( 'referer' => $input->referer(),
727 'DHTMLcalendar_dateformat' => C4::Dates->DHTMLcalendar(),
730 output_html_with_http_headers $input, $cookie, $template->output;