From 4d354c68338800c23184da3ab386ab7487a55fce Mon Sep 17 00:00:00 2001 From: Sophie Meynieux Date: Thu, 22 Mar 2012 15:51:43 +0100 Subject: [PATCH] Bug 1993: Improving task scheduler Remove "url" as format as it is not implemented Add csv as output format Use MIME:Lite to send email Call runreport.pl with right parameters Signed-off-by: Kyle M Hall Bug 1993 followup : get_saved_report API has changed --- .../prog/en/modules/tools/scheduler.tt | 2 +- misc/cronjobs/runreport.pl | 54 ++++++++++++++------ tools/scheduler.pl | 9 ++-- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/scheduler.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/scheduler.tt index 31a0084..6f26627 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/scheduler.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/scheduler.tt @@ -54,7 +54,7 @@
  • diff --git a/misc/cronjobs/runreport.pl b/misc/cronjobs/runreport.pl index 2357ed8..1d0a406 100755 --- a/misc/cronjobs/runreport.pl +++ b/misc/cronjobs/runreport.pl @@ -25,7 +25,7 @@ use C4::Context; use Getopt::Long qw(:config auto_help auto_version); use Pod::Usage; -use Mail::Sendmail; +use MIME::Lite; use Text::CSV_XS; use CGI; use Carp; @@ -149,6 +149,9 @@ my $from = ""; my $subject = 'Koha Saved Report'; my $separator = ','; my $quote = '"'; +my $message = ""; +my $mail; +my $outfile; GetOptions( 'help|?' => \$help, @@ -169,11 +172,6 @@ unless ($format) { $format = 'text'; } -if ($format eq 'tsv' || $format eq 'text') { - $format = 'csv'; - $separator = "\t"; -} - if ($to or $from or $email) { $email = 1; $from or $from = C4::Context->preference('KohaAdminEmailAddress'); @@ -190,7 +188,7 @@ unless (scalar(@ARGV)) { foreach my $report_id (@ARGV) { my $report = get_saved_report($report_id); unless ($report) { - warn "ERROR: No saved report $report_id found"; + carp "ERROR: No saved report $report_id found"; next; } my $sql = $report->{savedsql}; @@ -206,7 +204,7 @@ foreach my $report_id (@ARGV) { { $subject = 'Koha Saved Report'; } - # my $results = execute_query($sql, undef, 0, 99999, $format, $report_id); + # my $results = execute_query($sql, undef, 0, 99999, $format, $report); my ($sth) = execute_query($sql); # execute_query(sql, , 0, 20, , ) my $count = scalar($sth->rows); @@ -216,8 +214,7 @@ foreach my $report_id (@ARGV) { } $verbose and print "$count results from execute_query\n"; - my $message; - if ($format eq 'html') { + if ($format eq 'text') { my $cgi = CGI->new(); my @rows = (); while (my $line = $sth->fetchrow_arrayref) { @@ -225,6 +222,15 @@ foreach my $report_id (@ARGV) { push @rows, $cgi->TR( join('', $cgi->td($line)) ) . "\n"; } $message = $cgi->table(join "", @rows); + if ( $email ) { + $mail = MIME::Lite->new ( + To => $to, + From => $from, + Subject => $subject, + Data => $message, + Type => 'text/html' + ); + } } elsif ($format eq 'csv') { my $csv = Text::CSV_XS->new({ quote_char => $quote, @@ -240,19 +246,33 @@ foreach my $report_id (@ARGV) { # $message .= join ($separator, @$line) . "\n"; $message .= $csv->string() . "\n"; } + if ($email) { + $outfile = "/tmp/report$report.csv"; + open( my $fh, ">:encoding(UTF-8)", $outfile ) or die "unable to open $outfile: $!"; + print $fh $message; + close $fh; + + $mail = MIME::Lite->new ( + From => $from, + To => $to, + Subject => $subject, + Type => 'multipart/mixed', + ); + $mail->attach ( + Type => 'application/csv', + Path => $outfile, + Filename => "report$report.csv", + ); + } } if ($email){ - my %mail = ( - To => $to, - From => $from, - Subject => encode('utf8', $subject ), - Message => encode('utf8', $message ) - ); - sendmail(%mail) or carp 'mail not sent:' . $Mail::Sendmail::error; + $mail->send or carp 'mail not sent:'; + unlink $outfile if ($outfile); } else { print $message; } + # my @xmlarray = ... ; # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id"; # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray ); diff --git a/tools/scheduler.pl b/tools/scheduler.pl index 99210ac..195674c 100755 --- a/tools/scheduler.pl +++ b/tools/scheduler.pl @@ -67,9 +67,12 @@ if ( $mode eq 'job_add' ) { my $report = $input->param('report'); my $format = $input->param('format'); my $email = $input->param('email'); - my $command = - "EXPORT KOHA_CONF=\"$CONFIG_NAME\"; " . $base - . "/tools/runreport.pl $report $format $email"; + my $command = "export KOHA_CONF=\"$CONFIG_NAME\"; "; + $command .= $base . "/misc/cronjobs/runreport.pl"; + $command .= " --format $format" if ( $format ); + $command .= " --to $email" if ( $email ); + $command .= " $report"; + if ($recurring) { my $frequency = $input->param('frequency'); -- 1.7.10.4