Bug 7248 follow-up (alternative)
[koha.git] / opac / svc / report
1 #!/usr/bin/perl
2
3 # Copyright 2011 Chris Cormack <chris@bigballofwax.co.nz>
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19 #
20
21 use strict;
22 use warnings;
23
24 use C4::Reports::Guided;
25 use JSON;
26 use CGI;
27
28 use Koha::Cache;
29
30 my $query  = CGI->new();
31 my $report = $query->param('id');
32
33 my $cache;
34
35 my ( $sql, $type, $name, $notes, $cache_expiry, $public ) =
36   get_saved_report($report);
37 die "Sorry this report is not public\n" unless $public;
38
39 if (Koha::Cache->is_cache_active) {
40     $cache = Koha::Cache->new(
41     );
42     my $page = $cache->get_from_cache("opac:report:$report");
43     if ($page) {
44         print $query->header;
45         print $page;
46         exit;
47     }
48 }
49
50 print $query->header;
51 my $offset = 0;
52 my $limit  = C4::Context->preference("SvcMaxReportRows") || 10;
53 my ( $sth, $errors ) = execute_query( $sql, $offset, $limit );
54 my $lines     = $sth->fetchall_arrayref;
55 my $json_text = to_json($lines);
56 print $json_text;
57
58 if (Koha::Cache->is_cache_active) {
59     $cache->set_in_cache( "opac:report:$report", $json_text, $cache_expiry );
60 }