From c9e918f81bd10f97758c53c481d5469c36e28b58 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 3 Apr 2024 15:33:27 +0000 Subject: [PATCH] Bug 36504: Add attach_only option to run report To test: 1 - Create a report in Koha 2 - Have koha setup to send emails 3 - perl misc/cronjobs/runreport.pl --to="example@example.org" --subject="Report cronjob" --format=csv --csv-header -a 1 4 - Note the email body and attachment contain the results 5 - Apply this patch 6 - Repeat above 7 - Note the email body says "Repost results attached" 8 - Confirm results attached 9 - Adjust report to have 0 results 10 - perl misc/cronjobs/runreport.pl --to="example@example.org" --subject="Report cronjob" --format=csv --csv-header -a 1 --send_empty 11 - Confirm the attachment exists, but message body indicates no results --- misc/cronjobs/runreport.pl | 112 +++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 48 deletions(-) diff --git a/misc/cronjobs/runreport.pl b/misc/cronjobs/runreport.pl index ee807d53cac..1737aa0f78e 100755 --- a/misc/cronjobs/runreport.pl +++ b/misc/cronjobs/runreport.pl @@ -47,31 +47,32 @@ runreport.pl - Run pre-existing saved reports runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ] Options: - -h --help brief help message - -m --man full documentation, same as --help --verbose - -v --verbose verbose output - - --format=s selects format. Choice of text, html, csv or tsv - - -e --email whether to use e-mail (implied by --to or --from) - --send_empty whether to send an email when there are no results from the - specified report - -a --attachment additionally attach the report as a file. cannot be used with html format - --username username to pass to the SMTP server for authentication - --password password to pass to the SMTP server for authentication - --method method is the type of authentication. Ie. LOGIN, DIGEST-MD5, etc. - --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 - --separator separator character for csv - --quote quote character for csv - --csv-header add column names as first line of csv output + -h --help brief help message + -m --man full documentation, same as --help --verbose + -v --verbose verbose output + + --format=s selects format. Choice of text, html, csv or tsv + + -e --email whether to use e-mail (implied by --to or --from) + --send_empty whether to send an email when there are no results from the + specified report + -a --attachment additionally attach the report as a file. cannot be used with html format + -ao --attach_only additionally only send the results as attachment, not in message body + --username username to pass to the SMTP server for authentication + --password password to pass to the SMTP server for authentication + --method method is the type of authentication. Ie. LOGIN, DIGEST-MD5, etc. + --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 + --separator separator character for csv + --quote quote character for csv + --csv-header add column names as first line of csv output Arguments: - reportID report ID Number from saved_sql.id, multiple ID's may be specified + reportID report ID Number from saved_sql.id, multiple ID's may be specified =head1 OPTIONS @@ -106,6 +107,14 @@ Empty string is allowed. Whether to use e-mail (implied by --to or --from). +=item B<--attachment> + +Whether to include the results as an attachment (requires --email|--to|--from) + +=item B<--attach_only> + +Whether to use include the results as an attachment (implies --attachment) + =item B<--username> Username to pass to the SMTP server for authentication @@ -188,6 +197,7 @@ my $verbose = 0; my $send_email = 0; my $send_empty = 0; my $attachment = 0; +my $attach_only = 0; my $format = "text"; my $to = ""; my $from = ""; @@ -207,24 +217,25 @@ my $method = 'LOGIN'; my $command_line_options = join(" ",@ARGV); GetOptions( - 'help|?' => \$help, - 'man' => \$man, - 'verbose' => \$verbose, - 'format=s' => \$format, - 'separator=s' => \$csv_separator, - 'quote=s' => \$csv_quote, - 'to=s' => \$to, - 'from=s' => \$from, - 'subject=s' => \$subject, - 'param=s' => \@params, - 'email' => \$send_email, - 'send_empty' => \$send_empty, - 'a|attachment' => \$attachment, - 'username:s' => \$username, - 'password:s' => \$password, - 'method:s' => \$method, - 'store-results' => \$store_results, - 'csv-header' => \$csv_header, + 'help|?' => \$help, + 'man' => \$man, + 'verbose' => \$verbose, + 'format=s' => \$format, + 'separator=s' => \$csv_separator, + 'quote=s' => \$csv_quote, + 'to=s' => \$to, + 'from=s' => \$from, + 'subject=s' => \$subject, + 'param=s' => \@params, + 'email' => \$send_email, + 'send_empty' => \$send_empty, + 'a|attachment' => \$attachment, + 'ao|attach_only' => \$attach_only, + 'username:s' => \$username, + 'password:s' => \$password, + 'method:s' => \$method, + 'store-results' => \$store_results, + 'csv-header' => \$csv_header, ) or pod2usage(2); pod2usage( -verbose => 2 ) if ($man); @@ -238,6 +249,8 @@ unless ($format) { $format = 'text'; } +$attachment = 1 if $attach_only; + if ($csv_separator) { if ( $format eq 'csv' ) { $separator = "$csv_separator"; @@ -364,14 +377,6 @@ foreach my $report_id (@ARGV) { } ); - if ( $format eq 'html' ) { - $message = "$message"; - $email->html_body($message); - } - else { - $email->text_body($message); - } - $email->attach( Encode::encode_utf8($message), content_type => "text/$format", @@ -379,6 +384,17 @@ foreach my $report_id (@ARGV) { disposition => 'attachment', ) if $attachment; + if ( $attach_only && $count ){ + $message = 'Report results attached to email'; + } + elsif ( $format eq 'html' ) { + $message = "$message"; + $email->html_body($message); + } + else { + $email->text_body($message); + } + my $smtp_server = Koha::SMTP::Servers->get_default; $smtp_server->set( { -- 2.30.2