use Modern::Perl;
+use Koha::Script -cron;
use C4::Reports::Guided; # 0.12
use Koha::Reports;
use C4::Context;
use Getopt::Long qw(:config auto_help auto_version);
use Pod::Usage;
use MIME::Lite;
-use Text::CSV_XS;
+use Text::CSV::Encoded;
use CGI qw ( -utf8 );
use Carp;
use Encode;
--to=s e-mail address to send report to
--from=s e-mail address to send report from
--subject=s subject for the e-mail
+ --param=s parameters for the report
--store-results store the result of the report
--csv-header add column names as first line of csv output
Subject for the e-mail message. Defaults to "Koha Saved Report"
+=item B<--param>
+
+Repeatable, should provide one param per param requested for the report.
+Report params are not combined as on the staff side, so you may need to repeat
+params.
+
=item B<--store-results>
Store the result of the report into the saved_reports DB table.
my $to = "";
my $from = "";
my $subject = "";
+my @params = ();
my $separator = ',';
my $quote = '"';
my $store_results = 0;
'to=s' => \$to,
'from=s' => \$from,
'subject=s' => \$subject,
+ 'param=s' => \@params,
'email' => \$email,
'a|attachment' => \$attachment,
'username:s' => \$username,
$subject = 'Koha Saved Report';
}
}
- my ($sth) = execute_query( $sql, undef, undef, undef, $report_id );
+
+ # convert SQL parameters to placeholders
+ my $params_needed = ( $sql =~ s/(<<[^>]+>>)/\?/g );
+ die("You supplied ". scalar @params . " parameter(s) and $params_needed are required by the report") if scalar @params != $params_needed;
+
+ my ($sth) = execute_query( $sql, undef, undef, \@params, $report_id );
my $count = scalar($sth->rows);
unless ($count) {
print "NO OUTPUT: 0 results from execute_query\n";
}
$message = $cgi->table(join "", @rows);
} elsif ($format eq 'csv') {
- my $csv = Text::CSV_XS->new({
+ my $csv = Text::CSV::Encoded->new({
+ encoding_out => 'utf8',
+ binary => 1,
quote_char => $quote,
sep_char => $separator,
});
if ( $csv_header ) {
- my $fields = $sth->{NAME};
- $csv->combine( @$fields );
+ my @fields = map { decode( 'utf8', $_ ) } @{ $sth->{NAME} };
+ $csv->combine( @fields );
$message .= $csv->string() . "\n";
- push @rows_to_store, [@$fields] if $store_results;
+ push @rows_to_store, [@fields] if $store_results;
}
while (my $line = $sth->fetchrow_arrayref) {
$message = "<html><head><style>tr:nth-child(2n+1) { background-color: #ccc;}</style></head><body>$message</body></html>";
$args->{contenttype} = 'text/html';
}
- my $email = Koha::Email->new();
- my %mail = $email->create_message_headers($args);
- $mail{Data} = $message;
- $mail{Auth} = { user => $username, pass => $password, method => $method } if $username;
+ my $email = Koha::Email->create( $args );
+ my %headers = $email->header_pairs;
+ $headers{Data} = $message;
+ $headers{Auth} = { user => $username, pass => $password, method => $method } if $username;
- my $msg = MIME::Lite->new(%mail);
+ my $msg = MIME::Lite->new(%headers);
$msg->attach(
Type => "text/$format",