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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #use warnings; FIXME - Bug 2505
25 use C4
::Reports
::Guided
;
26 use C4
::Auth qw
/:DEFAULT get_session/;
30 use C4
::Branch
; # XXX subfield_is_koha_internal_p
38 Script to control the guided report creation
43 my $usecache = C4
::Context
->ismemcached;
45 my $phase = $input->param('phase');
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';
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
58 template_name
=> "reports/guided_reports_start.tmpl",
62 flagsrequired
=> { reports
=> $flagsrequired },
66 my $session = $cookie ? get_session
($cookie->value) : undef;
69 if ( $input->param("filter_set") ) {
71 $filter->{$_} = $input->param("filter_$_") foreach qw
/date author keyword/;
72 $session->param('report_filter', $filter) if $session;
73 $template->param( 'filter_set' => 1 );
76 $filter = $session->param('report_filter');
82 $template->param( 'start' => 1 );
85 elsif ( $phase eq 'Build new' ) {
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' ) {
92 # get list of reports and display them
95 'savedreports' => get_saved_reports
($filter),
96 'usecache' => $usecache,
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');
110 print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved");
114 elsif ( $phase eq 'Show SQL'){
116 my $id = $input->param('reports');
117 my ($sql,$type,$reportname,$notes) = get_saved_report
($id);
120 'reportname' => $reportname,
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);
133 'reportname' => $reportname,
136 'cache_expiry' => $cache_expiry,
138 'usecache' => $usecache,
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');
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" ){
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};
177 'errors' => \
@errors,
182 update_sql
( $id, $sql, $reportname, $notes, $cache_expiry, $public );
184 'save_successful' => 1,
185 'reportname' => $reportname,
192 elsif ($phase eq 'retrieve results') {
193 my $id = $input->param('id');
194 my ($results,$name,$notes) = format_results
($id);
198 'results' => $results,
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" ){
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
222 'areas' => get_report_areas
(),
223 'cache_expiry' => $cache_expiry,
224 'usecache' => $usecache,
225 'public' => $input->param('public'),
228 # they have choosen a new report and the area to report on
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');
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 );
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');
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'";
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";
311 $query_criteria .= " AND $crit='$value'";
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'),
328 my @columns = split( ',', $column );
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');
352 foreach my $total (@total_by) {
353 my $value = $input->param( $total . "_tvalue" );
354 $totals .= "$value($total),";
362 'criteriastring' => $criteria,
364 'definition' => $definition,
365 'cache_expiry' => $input->param('cache_expiry'),
366 'public' => $input->param('public'),
370 my @columns = split( ',', $column );
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');
400 foreach my $order (@order_by) {
401 my $value = $input->param( $order . "_ovalue" );
402 if ($query_orderby) {
403 $query_orderby .= ",$order $value";
406 $query_orderby = " ORDER BY $order $value";
412 build_query
( \
@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
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');
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" ){
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};
469 'errors' => \
@errors,
471 'reportname'=> $name,
474 'cache_expiry' => $cache_expiry,
479 my $id = save_report
( $borrowernumber, $sql, $name, $type, $notes, $cache_expiry, $public );
481 'save_successful' => 1,
482 'reportname' => $name,
488 elsif ($phase eq 'Run this report'){
489 # execute a saved report
490 my $limit = 20; # page size. # TODO: move to DB or syspref?
492 my $report = $input->param('reports');
493 my @sql_params = $input->param('sql_params');
495 if ($input->param('page')) {
496 $offset = ($input->param('page') - 1) * $limit;
498 my ($sql,$type,$name,$notes) = get_saved_report
($report);
500 push @errors, {no_sql_for_id
=>$report};
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;
508 for(my $i=0;$i<($#split/2);$i++) {
509 my ($text,$authorised_value) = split /\|/,$split[$i*2+1];
511 if ($authorised_value eq "date") {
514 elsif ($authorised_value) {
515 my $dbh=C4
::Context
->dbh;
516 my @authorised_values;
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");
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");
547 while ( my ( $categorycode, $description ) = $sth->fetchrow_array ) {
548 push @authorised_values, $categorycode;
549 $authorised_lib{$categorycode} = $description;
552 #---- "true" authorised value
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,
578 $input = "<input type='text' name='sql_params'/>";
580 push @tmpl_parameters, {'entry' => $text, 'input' => $input };
582 $template->param('sql' => $sql,
584 'sql_params' => \
@tmpl_parameters,
586 'reports' => $report,
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;
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;
603 die "execute_query failed to return sth for report $report: $sql";
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&phase=Run%20this%20report";
617 $url = join('&sql_params=', $url, map { URI
::Escape
::uri_escape
($_) } @sql_params);
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";
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";
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";
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.
673 'name' => 'Error exporting report!',
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')) {
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
(),
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,
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);
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;