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);
34 use vars
qw($VERSION);
37 # find Koha's Perl modules
38 # test carefully before changing this
40 eval { require "$FindBin::Bin/../kohalib.pl" };
46 runreport.pl - Run pre-existing saved reports
50 runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
53 -h --help brief help message
54 -m --man full documentation, same as --help --verbose
55 -v --verbose verbose output
57 --format=s selects format. Choice of text, html, csv, or tsv
59 -e --email whether to use e-mail (implied by --to or --from)
60 --to=s e-mail address to send report to
61 --from=s e-mail address to send report from
62 --subject=s subject for the e-mail
66 reportID report ID Number from saved_sql.id, multiple ID's may be specified
74 Print a brief help message and exits.
78 Prints the manual page and exits.
82 Verbose. Without this flag set, only fatal errors are reported.
86 Current options are text, html, csv, and tsv. At the moment, text and tsv both produce tab-separated tab-separated output.
90 Whether to use e-mail (implied by --to or --from).
94 E-mail address to send report to. Defaults to KohaAdminEmailAddress.
98 E-mail address to send report from. Defaults to KohaAdminEmailAddress.
102 Subject for the e-mail message. Defaults to "Koha Saved Report"
108 This script is designed to run existing Saved Reports.
110 =head1 USAGE EXAMPLES
114 In the most basic form, runs the report specified by ID number from
115 saved_sql.id, in this case #16, outputting the results to STDOUT.
117 B<runreport.pl 16 17>
119 Same as above, but also runs report #17.
128 Allow Saved Results option.
135 Reports - Guided Reports
139 # These variables can be set by command line options,
140 # initially set to default values.
149 my $subject = 'Koha Saved Report';
156 'verbose' => \$verbose,
157 'format=s' => \$format,
160 'subject=s' => \$subject,
163 pod2usage( -verbose => 2 ) if ($man);
164 pod2usage( -verbose => 2 ) if ($help and $verbose);
165 pod2usage(1) if $help;
168 $verbose and print STDERR "No format specified, assuming 'text'\n";
172 if ($format eq 'tsv' || $format eq 'text') {
177 if ($to or $from or $email) {
179 $from or $from = C4::Context->preference('KohaAdminEmailAddress');
180 $to or $to = C4::Context->preference('KohaAdminEmailAddress');
183 unless (scalar(@ARGV)) {
184 print STDERR "ERROR: No reportID(s) specified\n";
187 ($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
190 foreach my $report_id (@ARGV) {
191 my $report = get_saved_report($report_id);
193 warn "ERROR: No saved report $report_id found";
196 my $sql = $report->{savedsql};
197 my $report_name = $report->{report_name};
198 my $type = $report->{type};
200 $verbose and print "SQL: $sql\n\n";
201 if (defined($report_name) and $report_name ne "")
203 $subject = $report_name ;
207 $subject = 'Koha Saved Report';
209 # my $results = execute_query($sql, undef, 0, 99999, $format, $report_id);
210 my ($sth) = execute_query($sql);
211 # execute_query(sql, , 0, 20, , )
212 my $count = scalar($sth->rows);
214 print "NO OUTPUT: 0 results from execute_query\n";
217 $verbose and print "$count results from execute_query\n";
220 if ($format eq 'html') {
221 my $cgi = CGI->new();
223 while (my $line = $sth->fetchrow_arrayref) {
224 foreach (@$line) { defined($_) or $_ = ''; } # catch undef values, replace w/ ''
225 push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n";
227 $message = $cgi->table(join "", @rows);
228 } elsif ($format eq 'csv') {
229 my $csv = Text::CSV_XS->new({
230 quote_char => $quote,
231 sep_char => $separator,
233 while (my $line = $sth->fetchrow_arrayref) {
234 $csv->combine(@$line);
236 # defined($_) or $_ = '';
237 # $_ =~ s/$quote/\\$quote/g;
238 # $_ = "$quote$_$quote";
239 # } # catch undef values, replace w/ ''
240 # $message .= join ($separator, @$line) . "\n";
241 $message .= $csv->string() . "\n";
249 Subject => encode('utf8', $subject ),
250 Message => encode('utf8', $message )
252 sendmail(%mail) or carp 'mail not sent:' . $Mail::Sendmail::error;
256 # my @xmlarray = ... ;
257 # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
258 # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
259 # store_results($id,$xml);