From 6bb9e055e500bd6bf3d84c5cdd4053f4005982d1 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 --- .../prog/en/modules/tools/scheduler.tt | 2 +- misc/cronjobs/runreport.pl | 60 ++++++++++++++----- tools/scheduler.pl | 9 ++- 3 files changed, 51 insertions(+), 20 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 09c943a..e0d24a6 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/scheduler.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/scheduler.tt @@ -58,7 +58,7 @@
  • diff --git a/misc/cronjobs/runreport.pl b/misc/cronjobs/runreport.pl index 6a8cce7..b30b02e 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; @@ -148,6 +148,9 @@ my $from = ""; my $subject = 'Koha Saved Report'; my $separator = ','; my $quote = '"'; +my $message = ""; +my $mail; +my $outfile; GetOptions( 'help|?' => \$help, @@ -168,11 +171,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'); @@ -187,12 +185,20 @@ unless (scalar(@ARGV)) { foreach my $report (@ARGV) { - my ($sql, $type) = get_saved_report($report); + my ($sql, $type, $report_name) = get_saved_report($report); unless ($sql) { carp "ERROR: No saved report $report found"; next; } $verbose and print "SQL: $sql\n\n"; + if (defined($report_name) and $report_name ne "") + { + $subject = $report_name ; + } + else + { + $subject = 'Koha Saved Report'; + } # my $results = execute_query($sql, undef, 0, 99999, $format, $report); my ($sth) = execute_query($sql); # execute_query(sql, , 0, 20, , ) @@ -203,8 +209,7 @@ foreach my $report (@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) { @@ -212,6 +217,15 @@ foreach my $report (@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, @@ -227,19 +241,33 @@ foreach my $report (@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 => $subject, - Message => $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 4d732db..5bd2e39 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.5.4