3 # Copyright 2008 Liblime
5 # This file is part of Koha.
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
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.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 use C4::Reports::Guided; # 0.12
26 use Getopt::Long qw(:config auto_help auto_version);
33 use vars qw($VERSION);
36 # find Koha's Perl modules
37 # test carefully before changing this
39 eval { require "$FindBin::Bin/../kohalib.pl" };
45 runreport.pl - Run pre-existing saved reports
49 runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
52 -h --help brief help message
53 -m --man full documentation, same as --help --verbose
54 -v --verbose verbose output
56 --format=s selects format. Choice of text, html, csv, or tsv
58 -e --email whether to use e-mail (implied by --to or --from)
59 --to=s e-mail address to send report to
60 --from=s e-mail address to send report from
61 --subject=s subject for the e-mail
65 reportID report ID Number from saved_sql.id, multiple ID's may be specified
73 Print a brief help message and exits.
77 Prints the manual page and exits.
81 Verbose. Without this flag set, only fatal errors are reported.
85 Current options are text, html, csv, and tsv. At the moment, text and tsv both produce tab-separated tab-separated output.
89 Whether to use e-mail (implied by --to or --from).
93 E-mail address to send report to. Defaults to KohaAdminEmailAddress.
97 E-mail address to send report from. Defaults to KohaAdminEmailAddress.
101 Subject for the e-mail message. Defaults to "Koha Saved Report"
107 This script is designed to run existing Saved Reports.
109 =head1 USAGE EXAMPLES
113 In the most basic form, runs the report specified by ID number from
114 saved_sql.id, in this case #16, outputting the results to STDOUT.
116 B<runreport.pl 16 17>
118 Same as above, but also runs report #17.
127 Allow Saved Results option.
134 Reports - Guided Reports
138 # These variables can be set by command line options,
139 # initially set to default values.
148 my $subject = 'Koha Saved Report';
155 'verbose' => \$verbose,
156 'format=s' => \$format,
159 'subject=s' => \$subject,
162 pod2usage( -verbose => 2 ) if ($man);
163 pod2usage( -verbose => 2 ) if ($help and $verbose);
164 pod2usage(1) if $help;
167 $verbose and print STDERR "No format specified, assuming 'text'\n";
171 if ($format eq 'tsv' || $format eq 'text') {
176 if ($to or $from or $email) {
178 $from or $from = C4::Context->preference('KohaAdminEmailAddress');
179 $to or $to = C4::Context->preference('KohaAdminEmailAddress');
182 unless (scalar(@ARGV)) {
183 print STDERR "ERROR: No reportID(s) specified\n";
186 ($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
189 foreach my $report (@ARGV) {
190 my ($sql, $type) = get_saved_report($report);
192 carp "ERROR: No saved report $report found";
195 $verbose and print "SQL: $sql\n\n";
196 # my $results = execute_query($sql, undef, 0, 99999, $format, $report);
197 my ($sth) = execute_query($sql);
198 # execute_query(sql, , 0, 20, , )
199 my $count = scalar($sth->rows);
201 print "NO OUTPUT: 0 results from execute_query\n";
204 $verbose and print "$count results from execute_query\n";
207 if ($format eq 'html') {
208 my $cgi = CGI->new();
210 while (my $line = $sth->fetchrow_arrayref) {
211 foreach (@$line) { defined($_) or $_ = ''; } # catch undef values, replace w/ ''
212 push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n";
214 $message = $cgi->table(join "", @rows);
215 } elsif ($format eq 'csv') {
216 my $csv = Text::CSV_XS->new({
217 quote_char => $quote,
218 sep_char => $separator,
220 while (my $line = $sth->fetchrow_arrayref) {
221 $csv->combine(@$line);
223 # defined($_) or $_ = '';
224 # $_ =~ s/$quote/\\$quote/g;
225 # $_ = "$quote$_$quote";
226 # } # catch undef values, replace w/ ''
227 # $message .= join ($separator, @$line) . "\n";
228 $message .= $csv->string() . "\n";
239 sendmail(%mail) or carp 'mail not sent:' . $Mail::Sendmail::error;
243 # my @xmlarray = ... ;
244 # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
245 # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
246 # store_results($id,$xml);