Bug 26669: (QA follow-up) Update last run when report run by name
[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 qw( get_template_and_user );
24 use C4::Reports::Guided qw( execute_query );
25 use Koha::Reports;
26 use JSON qw( encode_json decode_json );
27 use CGI qw ( -utf8 );
28
29 use Koha::Caches;
30
31
32 my $query  = CGI->new();
33 my $report_id = $query->param('id');
34 my $report_name = $query->param('name');
35 my $report_annotation = $query->param('annotated');
36
37 my $report_recs = Koha::Reports->search( $report_name ? { 'report_name' => $report_name } : { 'id' => $report_id } );
38
39 if (!$report_recs || $report_recs->count == 0 ) { die "There is no such report.\n"; }
40 my $report_rec = $report_recs->next();
41
42 $report_id = $report_rec->id;
43
44 my @sql_params  = $query->multi_param('sql_params');
45 my @param_names  = $query->multi_param('param_names');
46
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48     {
49         template_name   => "intranet-main.tt",
50         query           => $query,
51         type            => "intranet",
52         flagsrequired   => { catalogue => 1, },
53     }
54 );
55
56 my $cache = Koha::Caches->get_instance();
57 my $cache_active = $cache->is_cache_active;
58 my ($cache_key, $json_text);
59 if ($cache_active) {
60     $cache_key = "intranet:report:".($report_name ? "report_name:$report_name:" : "id:$report_id:")
61     . join( '-', @sql_params )
62       . join( '_'. @param_names );
63     $json_text = $cache->get_from_cache($cache_key);
64 }
65
66 unless ($json_text) {
67     my $limit  = C4::Context->preference("SvcMaxReportRows") || 10;
68
69     my ( $sql, undef ) = $report_rec->prep_report( \@param_names, \@sql_params );
70
71     my ( $sth, $errors ) = execute_query(
72         {
73             sql        => $sql,
74             offset     => 0,
75             limit      => $limit,
76             report_id  => $report_id,
77         }
78     );
79     if ($sth) {
80         my $lines;
81         if ($report_annotation) {
82             $lines = $sth->fetchall_arrayref({});
83         }
84         else {
85             $lines = $sth->fetchall_arrayref;
86         }
87         $json_text = encode_json($lines);
88
89         if ($cache_active) {
90             $cache->set_in_cache( $cache_key, $json_text, { expiry => $report_rec->cache_expiry } );
91         }
92     }
93     else {
94         $json_text = encode_json($errors);
95     }
96 }
97
98 print $query->header(
99     -charset    => 'UTF-8',
100     -type       => 'application/json'
101 );
102 print $json_text;