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);
32 use vars qw($VERSION);
35 # find Koha's Perl modules
36 # test carefully before changing this
38 eval { require "$FindBin::Bin/../kohalib.pl" };
44 runreport.pl - Run pre-existing saved reports
48 runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
51 -h --help brief help message
52 -m --man full documentation, same as --help --verbose
53 -v --verbose verbose output
56 reportID report ID Number from saved_sql.id, multiple ID's may be specified
64 Print a brief help message and exits.
68 Prints the manual page and exits.
72 Verbose. Without this flag set, only fatal errors are reported.
78 This script is designed to run existing Saved Reports.
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.
89 Same as above, but also runs report #17.
97 Complete testing for Sendmail related options: --email, --to, and --from.
101 Allow Saved Results option.
105 Possible --format option for CSV or tab-delimited output.
111 Reports - Guided Reports
115 # These variables can be set by command line options,
116 # initially set to default values.
125 my $subject = 'Koha Saved Report';
130 'verbose' => \$verbose,
131 'format' => \$format,
136 pod2usage( -verbose => 2 ) if ($man);
137 pod2usage( -verbose => 2 ) if ($help and $verbose);
138 pod2usage(1) if $help;
141 $verbose and print STDERR "No format specified, assuming 'text'\n";
146 if ($to or $from or $email) {
148 $from or $from = C4::Context->preference('KohaAdminEmailAddress');
149 $to or $to = C4::Context->preference('KohaAdminEmailAddress');
152 unless (scalar(@ARGV)) {
153 print STDERR "ERROR: No reportID(s) specified\n";
156 ($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
159 foreach my $report (@ARGV) {
160 my ($sql, $type) = get_saved_report($report);
162 warn "ERROR: No saved report $report found";
165 $verbose and print "SQL: $sql\n\n";
166 # my $results = execute_query($sql, undef, 0, 99999, $format, $report);
167 my ($sth) = execute_query($sql);
168 # execute_query(sql, , 0, 20, , )
169 my $count = scalar($sth->rows);
171 print "NO OUTPUT: 0 results from execute_query\n";
174 $verbose and print "$count results from execute_query\n";
176 my $cgi = CGI->new();
178 while (my $line = $sth->fetchrow_arrayref) {
179 foreach (@$line) { defined($_) or $_ = ''; } # catch undef values, replace w/ ''
180 push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n";
182 my $message = $cgi->table(join "", @rows);
191 sendmail(%mail) or warn "mail not sent";
195 # my @xmlarray = ... ;
196 # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
197 # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
198 # store_results($id,$xml);