Bug 22343: Adapt controller scripts
[srvgit] / misc / cronjobs / runreport.pl
index b7fc9f8..1f50483 100755 (executable)
@@ -20,6 +20,7 @@
 
 use Modern::Perl;
 
+use Koha::Script -cron;
 use C4::Reports::Guided; # 0.12
 use Koha::Reports;
 use C4::Context;
@@ -30,7 +31,7 @@ use Koha::DateUtils;
 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;
@@ -66,6 +67,7 @@ runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
    --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
 
@@ -121,6 +123,12 @@ E-mail address to send report from. Defaults to KohaAdminEmailAddress.
 
 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.
@@ -174,6 +182,7 @@ my $format  = "text";
 my $to      = "";
 my $from    = "";
 my $subject = "";
+my @params = ();
 my $separator = ',';
 my $quote = '"';
 my $store_results = 0;
@@ -191,6 +200,7 @@ GetOptions(
     'to=s'              => \$to,
     'from=s'            => \$from,
     'subject=s'         => \$subject,
+    'param=s'           => \@params,
     'email'             => \$email,
     'a|attachment'      => \$attachment,
     'username:s'        => \$username,
@@ -253,7 +263,12 @@ foreach my $report_id (@ARGV) {
             $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";
@@ -273,16 +288,18 @@ foreach my $report_id (@ARGV) {
         }
         $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) {
@@ -301,12 +318,12 @@ foreach my $report_id (@ARGV) {
             $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",