Bug 7249: Syspref to control number of rows in web service results
[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 my $query  = CGI->new();
30 my $report = $query->param('id');
31
32 my $cache;
33 my $usecache = C4::Context->ismemcached;
34
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {
37         template_name   => "intranet-main.tmpl",
38         query           => $query,
39         type            => "intranet",
40         authnotrequired => 0,
41         flagsrequired   => { catalogue => 1, },
42     }
43 );
44
45 if ($usecache) {
46     require Koha::Cache;
47     Koha::Cache->import();
48     $cache = Koha::Cache->new(
49         {
50             'cache_type'    => 'memcached',
51             'cache_servers' => $ENV{'MEMCACHED_SERVERS'}
52         }
53     );
54     my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha';
55     my $page = $cache->get_from_cache("$namespace:intranet:report:$report");
56     if ($page) {
57         print $query->header;
58         print $page;
59         exit;
60     }
61 }
62
63 print $query->header;
64
65 # $public isnt used for intranet
66 my ( $sql, $type, $name, $notes, $cache_expiry, $public ) =
67   get_saved_report($report);
68 my $offset = 0;
69 my $limit  = C4::Context->preference("SvcMaxReportRows") || 10;
70 my ( $sth, $errors ) = execute_query( $sql, $offset, $limit );
71 my $lines     = $sth->fetchall_arrayref;
72 my $json_text = to_json($lines);
73 print $json_text;
74
75 if ($usecache) {
76     my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha';
77     $cache->set_in_cache( "$namespace:intranet:report:$report",
78         $json_text, $cache_expiry );
79 }