Merge remote-tracking branch 'origin/new/bug_8315'
[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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_id = $query->param('id');
32 my $report_name = $query->param('name');
33
34 my $cache;
35 my $sql;
36 my $type;
37 my $notes;
38 my $cache_expiry;
39 my $public;
40
41 ( $sql, $type, $report_name, $notes, $cache_expiry, $public, $report_id ) =
42   get_saved_report($report_name ? { 'name' => $report_name } : { 'id' => $report_id } );
43 die "Sorry this report is not public\n" unless $public;
44
45 if (Koha::Cache->is_cache_active) {
46     $cache = Koha::Cache->new(
47     );
48     my $page = $cache->get_from_cache("opac:report:$report_id");
49     if ($page) {
50         print $query->header;
51         print $page;
52         exit;
53     }
54 }
55
56 print $query->header;
57 if ($sql) {
58     my $offset = 0;
59     my $limit  = C4::Context->preference("SvcMaxReportRows") || 10;
60     my ( $sth, $errors ) = execute_query( $sql, $offset, $limit );
61     my $lines     = $sth->fetchall_arrayref;
62     my $json_text = to_json($lines);
63     print $json_text;
64
65     if (Koha::Cache->is_cache_active) {
66         $cache->set_in_cache( "opac:report:$report_id", $json_text, $cache_expiry );
67     }
68 }