Bug 16675: fix breakage of t/Languages.t
[koha.git] / opac / svc / report
blobbfc84e5c2a89e8dc23a8e6e89e3f048a40489684
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 JSON;
28 use CGI qw ( -utf8 );
30 use Koha::Cache;
32 my $query = CGI->new();
33 my $report_id = $query->param('id');
34 my $report_name = $query->param('name');
35 my $report_annotation = $query->param('annotated');
37 my $report_rec = get_saved_report( $report_name ? { 'name' => $report_name } : { 'id' => $report_id } );
38 if (!$report_rec) { die "There is no such report.\n"; }
40 die "Sorry this report is not public\n" unless $report_rec->{public};
42 my @sql_params = $query->param('sql_params');
44 my $cache = Koha::Cache->get_instance();
45 my $cache_active = $cache->is_cache_active;
46 my ($cache_key, $json_text);
47 if ($cache_active) {
48 $cache_key =
49 "opac:report:"
50 . ( $report_name ? "name:$report_name" : "id:$report_id" )
51 . join( '-', @sql_params );
52 $json_text = $cache->get_from_cache($cache_key);
55 unless ($json_text) {
56 my $offset = 0;
57 my $limit = C4::Context->preference("SvcMaxReportRows") || 10;
58 my $sql = $report_rec->{savedsql};
60 # convert SQL parameters to placeholders
61 $sql =~ s/(<<.*?>>)/\?/g;
63 my ( $sth, $errors ) =
64 execute_query( $sql, $offset, $limit, \@sql_params );
65 if ($sth) {
66 my $lines;
67 if ($report_annotation) {
68 $lines = $sth->fetchall_arrayref({});
70 else {
71 $lines = $sth->fetchall_arrayref;
73 $json_text = encode_json($lines);
75 if ($cache_active) {
76 $cache->set_in_cache( $cache_key, $json_text,
77 { expiry => $report_rec->{cache_expiry} } );
80 else {
81 $json_text = encode_json($errors);
85 print $query->header(
86 -charset => 'UTF-8',
87 -type => 'application/json'
89 print $json_text;