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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 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.
95 ~ Complete testing for Sendmail related options: --email, --to, and --from.
96 ~ Allow Saved Results option.
97 ~ Possible --format option for CSV or tab-delimited output.
103 Reports - Guided Reports
107 # These variables can be set by command line options,
108 # initially set to default values.
117 my $subject = 'Koha Saved Report';
122 'verbose' => \$verbose,
123 'format' => \$format,
128 pod2usage( -verbose => 2 ) if ($man);
129 pod2usage( -verbose => 2 ) if ($help and $verbose);
130 pod2usage(1) if $help;
133 $verbose and print STDERR "No format specified, assuming 'text'\n";
138 if ($to or $from or $email) {
140 $from or $from = C4::Context->preference('KohaAdminEmailAddress');
141 $to or $to = C4::Context->preference('KohaAdminEmailAddress');
144 unless (scalar(@ARGV)) {
145 print STDERR "ERROR: No reportID(s) specified\n";
148 ($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
151 foreach my $report (@ARGV) {
152 my ($sql, $type) = get_saved_report($report);
154 warn "ERROR: No saved report $report found";
157 $verbose and print "SQL: $sql\n\n";
158 # my $results = execute_query($sql, undef, 0, 99999, $format, $report);
159 my ($sth) = execute_query($sql);
160 # execute_query(sql, , 0, 20, , )
161 my $count = scalar($sth->rows);
163 print "NO OUTPUT: 0 results from execute_query\n";
166 $verbose and print "$count results from execute_query\n";
168 my $cgi = CGI->new();
170 while (my $line = $sth->fetchrow_arrayref) {
171 foreach (@$line) { defined($_) or $_ = ''; } # catch undef values, replace w/ ''
172 push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n";
174 my $message = $cgi->table(join "", @rows);
183 sendmail(%mail) or warn "mail not sent";
187 # my @xmlarray = ... ;
188 # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
189 # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
190 # store_results($id,$xml);