Bug 3482 changed name of notices file
[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.22;
40 }
41
42 =head1 NAME
43
44 runreport.pl - Run pre-existing saved reports
45
46 =head1 SYNOPSIS
47
48 runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
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  Arguments:
56    reportID        report ID Number from saved_sql.id, multiple ID's may be specified
57
58 =head1 OPTIONS
59
60 =over 8
61
62 =item B<-help>
63
64 Print a brief help message and exits.
65
66 =item B<-man>
67
68 Prints the manual page and exits.
69
70 =item B<-v>
71
72 Verbose. Without this flag set, only fatal errors are reported.
73
74 =back
75
76 =head1 DESCRIPTION
77
78 This script is designed to run existing Saved Reports.
79
80 =head1 USAGE EXAMPLES
81
82 B<runreport.pl 16>
83
84 In the most basic form, runs the report specified by ID number from 
85 saved_sql.id, in this case #16, outputting the results to STDOUT.  
86
87 B<runreport.pl 16 17>
88
89 Same as above, but also runs report #17. 
90
91 =head1 TO DO
92
93 =over 8
94
95  ~ Complete testing for Sendmail related options: --email, --to, and --from.
96  ~ Allow Saved Results option.
97  ~ Possible --format option for CSV or tab-delimited output.
98
99 =back
100
101 =head1 SEE ALSO
102
103 Reports - Guided Reports
104
105 =cut
106
107 # These variables can be set by command line options,
108 # initially set to default values.
109
110 my $help    = 0;
111 my $man     = 0;
112 my $verbose = 0;
113 my $email   = 0;
114 my $format  = "";
115 my $to      = "";
116 my $from    = "";
117 my $subject = 'Koha Saved Report';
118
119 GetOptions(
120     'help|?'     => \$help,
121     'man'        => \$man,
122     'verbose'    => \$verbose,
123     'format'     => \$format,
124     'to'         => \$to,
125     'from'       => \$from,
126     'email'      => \$email,
127 ) or pod2usage(2);
128 pod2usage( -verbose => 2 ) if ($man);
129 pod2usage( -verbose => 2 ) if ($help and $verbose);
130 pod2usage(1) if $help;
131
132 unless ($format) {
133     $verbose and print STDERR "No format specified, assuming 'text'\n";
134     $format = '';
135     # $format = 'text';
136 }
137
138 if ($to or $from or $email) {
139     $email = 1;
140     $from or $from = C4::Context->preference('KohaAdminEmailAddress');
141     $to   or $to   = C4::Context->preference('KohaAdminEmailAddress');
142 }
143
144 unless (scalar(@ARGV)) {
145     print STDERR "ERROR: No reportID(s) specified\n";
146     pod2usage(1);
147 }
148 ($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
149
150
151 foreach my $report (@ARGV) {
152     my ($sql, $type) = get_saved_report($report);
153     unless ($sql) {
154         warn "ERROR: No saved report $report found";
155         next;
156     }
157     $verbose and print "SQL: $sql\n\n";
158     # my $results = execute_query($sql, undef, 0, 99999, $format, $report); 
159     my ($sth) = execute_query($sql);
160     # execute_query(sql, , 0, 20, , )
161     my $count = scalar($sth->rows);
162     unless ($count) {
163         print "NO OUTPUT: 0 results from execute_query\n";
164         next;
165     }
166     $verbose and print "$count results from execute_query\n";
167
168     my $cgi = CGI->new();
169     my @rows = ();
170     while (my $line = $sth->fetchrow_arrayref) {
171         foreach (@$line) { defined($_) or $_ = ''; }    # catch undef values, replace w/ ''
172         push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n";
173     }
174     my $message = $cgi->table(join "", @rows);
175
176     if ($email){
177         my %mail = (
178             To      => $to,
179             From    => $from,
180             Subject => $subject,
181             Message => $message 
182         );
183         sendmail(%mail) or warn "mail not sent";
184     } else {
185         print $message;
186     }
187     # my @xmlarray = ... ;
188     # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
189     # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
190     # store_results($id,$xml);
191 }