Bug 13937: Fix issues found in QA
[koha.git] / opac / svc / report
blobdac74277db947e541cb45e40924844ba28eafaf8
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright (C) 2011 Chris Cormack <chris@bigballofwax.co.nz>
6 # Copyright (C) 2013 Mark Tompsett
7 # Updated 2013 by Chris Cormack <chris@bigballofwax.co.nz>
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 3 of the License, or (at your option) any later
14 # version.
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use Modern::Perl;
26 use C4::Reports::Guided;
27 use Koha::Reports;
28 use JSON;
29 use CGI qw ( -utf8 );
31 use Koha::Caches;
33 my $query = CGI->new();
34 my $report_id = $query->param('id');
35 my $report_name = $query->param('name');
36 my $report_annotation = $query->param('annotated');
38 my $report_recs = Koha::Reports->search( $report_name ? { 'report_name' => $report_name } : { 'id' => $report_id } );
39 if ( !$report_recs || $report_recs->count == 0 ) { die "There is no such report.\n"; }
40 my $report_rec = $report_recs->next();
42 die "Sorry this report is not public\n" unless $report_rec->public;
44 my @sql_params = $query->multi_param('sql_params');
46 my $cache = Koha::Caches->get_instance();
47 my $cache_active = $cache->is_cache_active;
48 my ($cache_key, $json_text);
49 if ($cache_active) {
50 $cache_key =
51 "opac:report:"
52 . ( $report_name ? "name:$report_name:" : "id:$report_id:" )
53 . join( '-', @sql_params );
54 $json_text = $cache->get_from_cache($cache_key);
57 unless ($json_text) {
58 my $offset = 0;
59 my $limit = C4::Context->preference("SvcMaxReportRows") || 10;
60 my $sql = $report_rec->savedsql;
62 # convert SQL parameters to placeholders
63 $sql =~ s/(<<.*?>>)/\?/g;
64 $sql =~ s/\[\[(.*?)\|(.*?)\]\]/$1 AS $2/g;
66 my ( $sth, $errors ) =
67 execute_query( $sql, $offset, $limit, \@sql_params, $report_id );
68 if ($sth) {
69 my $lines;
70 if ($report_annotation) {
71 $lines = $sth->fetchall_arrayref({});
73 else {
74 $lines = $sth->fetchall_arrayref;
76 $json_text = encode_json($lines);
78 if ($cache_active) {
79 $cache->set_in_cache( $cache_key, $json_text,
80 { expiry => $report_rec->cache_expiry } );
83 else {
84 $json_text = encode_json($errors);
88 print $query->header(
89 -charset => 'UTF-8',
90 -type => 'application/json'
92 print $json_text;