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_id (@ARGV) {
190 my $report = get_saved_report($report_id);
192 warn "ERROR: No saved report $report_id found";
195 my $sql => $report->{savedsql};
196 my $report_name => $report->{report_name};
197 my $type => $report->{type};
199 $verbose and print "SQL: $sql\n\n";
200 if (defined($report_name) and $report_name ne "")
202 $subject = $report_name ;
206 $subject = 'Koha Saved Report';
208 # my $results = execute_query($sql, undef, 0, 99999, $format, $report_id);
209 my ($sth) = execute_query($sql);
210 # execute_query(sql, , 0, 20, , )
211 my $count = scalar($sth->rows);
213 print "NO OUTPUT: 0 results from execute_query\n";
216 $verbose and print "$count results from execute_query\n";
219 if ($format eq 'html') {
220 my $cgi = CGI->new();
222 while (my $line = $sth->fetchrow_arrayref) {
223 foreach (@$line) { defined($_) or $_ = ''; } # catch undef values, replace w/ ''
224 push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n";
226 $message = $cgi->table(join "", @rows);
227 } elsif ($format eq 'csv') {
228 my $csv = Text::CSV_XS->new({
229 quote_char => $quote,
230 sep_char => $separator,
232 while (my $line = $sth->fetchrow_arrayref) {
233 $csv->combine(@$line);
235 # defined($_) or $_ = '';
236 # $_ =~ s/$quote/\\$quote/g;
237 # $_ = "$quote$_$quote";
238 # } # catch undef values, replace w/ ''
239 # $message .= join ($separator, @$line) . "\n";
240 $message .= $csv->string() . "\n";
251 sendmail(%mail) or carp 'mail not sent:' . $Mail::Sendmail::error;
255 # my @xmlarray = ... ;
256 # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
257 # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
258 # store_results($id,$xml);