Bug 3066 - Overhaul guided reports
[koha.git] / misc / cronjobs / runreport.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2008 Liblime
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 use strict;
21 use warnings;
22
23 use C4::Reports::Guided; # 0.12
24 use C4::Context;
25
26 use Getopt::Long qw(:config auto_help auto_version);
27 use Pod::Usage;
28 use Mail::Sendmail;
29 use Text::CSV_XS;
30 use CGI;
31
32 use vars qw($VERSION);
33
34 BEGIN {
35     # find Koha's Perl modules
36     # test carefully before changing this
37     use FindBin;
38     eval { require "$FindBin::Bin/../kohalib.pl" };
39     $VERSION = 0.21;
40 }
41
42 =head1 NAME
43
44 runreport.pl - Run a pre-existing saved report.
45
46 =head1 SYNOPSIS
47
48 runreport.pl [ -v ] 
49
50  Options:
51    -h --help             brief help message
52    -m --man              full documentation, same as --help --verbose
53    -v --verbose          verbose output
54
55 =head1 OPTIONS
56
57 =over 8
58
59 =item B<-help>
60
61 Print a brief help message and exits.
62
63 =item B<-man>
64
65 Prints the manual page and exits.
66
67 =item B<-v>
68
69 Verbose. Without this flag set, only fatal errors are reported.
70
71 =back
72
73 =head1 DESCRIPTION
74
75 This script is designed to run an existing Saved Report.
76
77 =head1 USAGE EXAMPLES
78
79 B<runreport.pl> 16
80
81 In the most basic form, runs the report specified by ID number from 
82 saved_sql.id, in this case #16, outputting the results to STDOUT.  
83
84 =head1 SEE ALSO
85
86 Reports - Guided Reports
87
88 =cut
89
90 # These variables can be set by command line options,
91 # initially set to default values.
92
93 my $help    = 0;
94 my $man     = 0;
95 my $verbose = 0;
96 my $format  = "";
97 my $to      = C4::Context->preference('KohaAdminEmailAddress');
98 my $from    = C4::Context->preference('KohaAdminEmailAddress');
99 my $subject = 'Koha Saved Report';
100
101 GetOptions(
102     'help|?'     => \$help,
103     'man'        => \$man,
104     'verbose'    => \$verbose,
105     'format'     => \$format,
106     'to'         => \$to,
107     'from'       => \$from,
108 ) or pod2usage(2);
109 pod2usage( -verbose => 2 ) if ($man);
110 pod2usage( -verbose => 2 ) if ($help and $verbose);
111 pod2usage(1) if $help;
112
113 unless ($format) {
114     $verbose and print STDERR "No format specified, assuming 'text'\n";
115     $format = '';
116     # $format = 'text';
117 }
118
119 unless (scalar(@ARGV)) {
120     print STDERR "ERROR: No reports specified\n";
121     pod2usage(1);
122 }
123 print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
124
125 my $email;
126
127 foreach my $report (@ARGV) {
128     my ($sql, $type) = get_saved_report($report);
129     unless ($sql) {
130         warn "ERROR: No saved report $report found";
131         next;
132     }
133     $verbose and print "SQL: $sql\n\n";
134     # my $results = execute_query($sql, undef, 0, 99999, $format, $report); 
135     my ($sth) = execute_query($sql);
136     # execute_query(sql, , 0, 20, , )
137     my $count = scalar($sth->rows);
138     unless ($count) {
139         print "NO OUTPUT: 0 results from execute_query\n";
140         next;
141     }
142     $verbose and print "$count results from execute_query\n";
143
144     my $cgi = CGI->new();
145     my @rows = ();
146     while (my $line = $sth->fetchrow_arrayref) {
147         foreach (@$line) { defined($_) or $_ = ''; }    # catch undef values, replace w/ ''
148         push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n";
149     }
150     my $message = $cgi->table(join "", @rows);
151
152     if ($email){
153         my %mail = (
154             To      => $to,
155             From    => $from,
156             Subject => $subject,
157             Message => $message 
158         );
159         sendmail(%mail) or warn "mail not sent";
160     } else {
161         print $message;
162     }
163     # my @xmlarray = ... ;
164     # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
165     # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
166     # store_results($id,$xml);
167 }