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
57 reportID report ID Number from saved_sql.id, multiple ID's may be specified
65 Print a brief help message and exits.
69 Prints the manual page and exits.
73 Verbose. Without this flag set, only fatal errors are reported.
79 This script is designed to run existing Saved Reports.
85 In the most basic form, runs the report specified by ID number from
86 saved_sql.id, in this case #16, outputting the results to STDOUT.
90 Same as above, but also runs report #17.
98 Complete testing for Sendmail related options: --email, --to, and --from.
102 Allow Saved Results option.
106 Possible --format option for CSV or tab-delimited output.
112 Reports - Guided Reports
116 # These variables can be set by command line options,
117 # initially set to default values.
126 my $subject = 'Koha Saved Report';
131 'verbose' => \$verbose,
132 'format' => \$format,
137 pod2usage( -verbose => 2 ) if ($man);
138 pod2usage( -verbose => 2 ) if ($help and $verbose);
139 pod2usage(1) if $help;
142 $verbose and print STDERR "No format specified, assuming 'text'\n";
147 if ($to or $from or $email) {
149 $from or $from = C4::Context->preference('KohaAdminEmailAddress');
150 $to or $to = C4::Context->preference('KohaAdminEmailAddress');
153 unless (scalar(@ARGV)) {
154 print STDERR "ERROR: No reportID(s) specified\n";
157 ($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
160 foreach my $report (@ARGV) {
161 my ($sql, $type) = get_saved_report($report);
163 carp "ERROR: No saved report $report found";
166 $verbose and print "SQL: $sql\n\n";
167 # my $results = execute_query($sql, undef, 0, 99999, $format, $report);
168 my ($sth) = execute_query($sql);
169 # execute_query(sql, , 0, 20, , )
170 my $count = scalar($sth->rows);
172 print "NO OUTPUT: 0 results from execute_query\n";
175 $verbose and print "$count results from execute_query\n";
177 my $cgi = CGI->new();
179 while (my $line = $sth->fetchrow_arrayref) {
180 foreach (@$line) { defined($_) or $_ = ''; } # catch undef values, replace w/ ''
181 push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n";
183 my $message = $cgi->table(join "", @rows);
192 sendmail(%mail) or carp 'mail not sent:' . $Mail::Sendmail::error;
196 # my @xmlarray = ... ;
197 # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
198 # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
199 # store_results($id,$xml);