Merge remote-tracking branch 'origin/new/bug_8315'
[koha.git] / 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::Auth;
25 use C4::Reports::Guided;
26 use JSON;
27 use CGI;
28
29 use Koha::Cache;
30
31
32 my $query  = CGI->new();
33 my $report_id = $query->param('id');
34 my $report_name = $query->param('name');
35
36 my $cache;
37 my $sql;
38 my $type;
39 my $notes;
40 my $cache_expiry;
41 my $public;
42
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {
45         template_name   => "intranet-main.tmpl",
46         query           => $query,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { catalogue => 1, },
50     }
51 );
52
53 if (Koha::Cache->is_cache_active) {
54     if ($report_name) { # When retrieving by name, we have to hit the
55                         # database to get the ID before we can check
56                         # the cache. Yuck.
57         ( $sql, $type, $report_name, $notes, $cache_expiry, $public, $report_id ) =
58             get_saved_report( { 'name' => $report_name } );
59     }
60
61     $cache = Koha::Cache->new();
62     my $page = $cache->get_from_cache("intranet:report:$report_id");
63     if ($page) {
64         print $query->header;
65         print $page;
66         exit;
67     }
68 }
69
70 print $query->header;
71
72 # $public isnt used for intranet
73 unless ($sql) {
74     ( $sql, $type, $report_name, $notes, $cache_expiry, $public, $report_id ) =
75         get_saved_report($report_name ? { 'name' => $report_name } : { 'id' => $report_id } );
76 }
77 if ($sql) {
78     my $offset = 0;
79     my $limit  = C4::Context->preference("SvcMaxReportRows") || 10;
80     my ( $sth, $errors ) = execute_query( $sql, $offset, $limit );
81     my $lines     = $sth->fetchall_arrayref;
82     my $json_text = to_json($lines);
83     print $json_text;
84
85     if (Koha::Cache->is_cache_active) {
86         $cache->set_in_cache( "intranet:report:$report_id", $json_text, $cache_expiry );
87     }
88 }