Bug 17189: Add the ability to define several memcached namespaces - replace existing...
[koha.git] / svc / report
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2011  Chris Cormack <chris@bigballofwax.co.nz>
6 # Copyright (C) 2013  Mark Tompsett
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use C4::Auth;
24 use C4::Reports::Guided;
25 use JSON;
26 use CGI qw ( -utf8 );
27
28 use Koha::Cache;
29
30
31 my $query  = CGI->new();
32 my $report_id = $query->param('id');
33 my $report_name = $query->param('name');
34 my $report_annotation = $query->param('annotated');
35
36 my $report_rec = get_saved_report( $report_name ? { 'name' => $report_name } : { 'id' => $report_id } );
37 if (!$report_rec) { die "There is no such report.\n"; }
38
39 my @sql_params  = $query->param('sql_params');
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {
43         template_name   => "intranet-main.tt",
44         query           => $query,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => { catalogue => 1, },
48     }
49 );
50
51 my $cache = Koha::Caches->get_instance();
52 my $cache_active = $cache->is_cache_active;
53 my ($cache_key, $json_text);
54 if ($cache_active) {
55     $cache_key = "intranet:report:".($report_name ? "name:$report_name" : "id:$report_id")
56     . join( '-', @sql_params );
57     $json_text = $cache->get_from_cache($cache_key);
58 }
59
60 unless ($json_text) {
61     my $offset = 0;
62     my $limit  = C4::Context->preference("SvcMaxReportRows") || 10;
63     my $sql = $report_rec->{savedsql};
64
65     # convert SQL parameters to placeholders
66     $sql =~ s/(<<.*?>>)/\?/g;
67
68     my ( $sth, $errors ) = execute_query( $sql, $offset, $limit, \@sql_params );
69     if ($sth) {
70         my $lines;
71         if ($report_annotation) {
72             $lines = $sth->fetchall_arrayref({});
73         }
74         else {
75             $lines = $sth->fetchall_arrayref;
76         }
77         $json_text = encode_json($lines);
78
79         if ($cache_active) {
80             $cache->set_in_cache( $cache_key, $json_text, $report_rec->{cache_expiry} );
81         }
82     }
83     else {
84         $json_text = encode_json($errors);
85     }
86 }
87
88 print $query->header(
89     -charset    => 'UTF-8',
90     -type       => 'application/json'
91 );
92 print $json_text;