Bug 26669: (QA follow-up) Update last run when report run by name
[koha.git] / opac / 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 # Updated 2013 by Chris Cormack <chris@bigballofwax.co.nz>
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 3 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 use Modern::Perl;
25
26 use C4::Output qw(output_with_http_headers);
27 use C4::Reports::Guided qw( execute_query );
28 use Koha::Reports;
29 use JSON;
30 use CGI qw ( -utf8 );
31
32 use Koha::Caches;
33
34 my $query       = CGI->new();
35 my $report_id   = $query->param('id');
36 my $report_name = $query->param('name');
37 my $report_annotation = $query->param('annotated');
38
39 my $report_recs = Koha::Reports->search( $report_name ? { 'report_name' => $report_name } : { 'id' => $report_id } );
40
41 if ( !$report_recs || $report_recs->count == 0 ) { die "There is no such report.\n"; }
42 my $report_rec = $report_recs->next();
43
44 $report_id = $report_rec->id;
45
46 die "Sorry this report is not public\n" unless $report_rec->public;
47
48 my @sql_params  = $query->multi_param('sql_params');
49 my @param_names  = $query->multi_param('param_names');
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 =
56         "opac:report:"
57       . ( $report_name ? "name:$report_name:" : "id:$report_id:" )
58       . join( '-', @sql_params )
59       . join( '_'. @param_names );
60     $json_text = $cache->get_from_cache($cache_key);
61 }
62
63 unless ($json_text) {
64     my $limit = C4::Context->preference("SvcMaxReportRows") || 10;
65
66     my ( $sql, undef ) = $report_rec->prep_report( \@param_names, \@sql_params );
67
68     my ( $sth, $errors ) = execute_query(
69         {
70             sql        => $sql,
71             offset     => 0,
72             limit      => $limit,
73             report_id  => $report_id,
74         }
75     );
76     if ($sth) {
77         my $lines;
78         if ($report_annotation) {
79             $lines = $sth->fetchall_arrayref({});
80         }
81         else {
82             $lines = $sth->fetchall_arrayref;
83         }
84         $json_text = encode_json($lines);
85
86         if ($cache_active) {
87             $cache->set_in_cache( $cache_key, $json_text,
88                 { expiry => $report_rec->cache_expiry } );
89         }
90     }
91     else {
92         $json_text = encode_json($errors);
93     }
94 }
95
96 output_with_http_headers( $query, undef, $json_text, 'json');