Bug 7249 : Allow reports to be called through a restful interface
[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 my $query  = CGI->new();
29 my $report = $query->param('id');
30
31 my $cache;
32 my $usecache = C4::Context->ismemcached;
33
34 my ( $sql, $type, $name, $notes, $cache_expiry, $public ) =
35   get_saved_report($report);
36 die "Sorry this report is not public\n" unless $public;
37
38 if ($usecache) {
39     require Koha::Cache;
40     Koha::Cache->import();
41     $cache = Koha::Cache->new(
42         {
43             'cache_type'    => 'memcached',
44             'cache_servers' => $ENV{'MEMCACHED_SERVERS'}
45         }
46     );
47     my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha';
48     my $page = $cache->get_from_cache("$namespace:opac:report:$report");
49     if ($page) {
50         print $query->header;
51         print $page;
52         exit;
53     }
54 }
55
56 print $query->header;
57 my $offset = 0;
58 my $limit  = 10;
59 my ( $sth, $errors ) = execute_query( $sql, $offset, $limit );
60 my $lines     = $sth->fetchall_arrayref;
61 my $json_text = to_json($lines);
62 print $json_text;
63
64 if ($usecache) {
65     my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha';
66     $cache->set_in_cache( "$namespace:opac:report:$report",
67         $json_text, $cache_expiry );
68 }