@@ -, +, @@ output --- C4/Reports/Guided.pm | 91 ++++++++++++++++++++++++++- misc/cronjobs/patron_emailer.pl | 133 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 223 insertions(+), 1 deletion(-) create mode 100755 misc/cronjobs/patron_emailer.pl --- a/C4/Reports/Guided.pm +++ a/C4/Reports/Guided.pm @@ -27,6 +27,7 @@ use C4::Context; use C4::Templates qw/themelanguage/; use C4::Koha; use Koha::DateUtils; +use Koha::Patrons; use C4::Output; use C4::Debug; use C4::Log; @@ -854,7 +855,6 @@ sub get_sql { sub get_results { my ( $report_id ) = @_; my $dbh = C4::Context->dbh; - warn $report_id; return $dbh->selectall_arrayref(q| SELECT id, report, date_run FROM saved_reports @@ -976,6 +976,95 @@ sub ValidateSQLParameters { return \@problematic_parameters; } +=head2 EmailReport + + EmailReport($report_id, $letter_code, $module, $branch, $email) + +Take a report and use it to process a Template Toolkit formatted notice + +=cut + +sub EmailReport { + + my $params = shift; + my $report = $params->{report}; + my $from = $params->{from}; + my $email = $params->{email}; + my $module = $params->{module}; + my $code = $params->{code}; + my $branch = $params->{branch} || ""; + + return ("You must provide a report, notice and module at minium") unless ($report && $module && $code); + + return ("Specified letter not found") unless + my $letter = Koha::Notice::Templates->find({ + module =>$module, + code => $code, + branchcode => $branch, + message_transport_type => 'email', + })->unblessed; + + return ("Report not found!") unless my $sql = get_saved_report( $report )->{savedsql}; + + my ( $sth, $errors ) = execute_query( $sql ); #don't pass offset or limit, hardcoded limit of 999,999 will be used + return ("Report failed: $errors") unless $sth; + + my $counter = 0; + my $template = $letter->{content}; + my @errors; + + while ( my $row = $sth->fetchrow_hashref() ) { + my $err_count = scalar @errors; + push ( @errors, "No borrowernumber column in report at row $counter" ) unless defined $row->{borrowernumber}; + push ( @errors, "Expected email column not found in report at row $counter" ) unless ( (defined $email && defined $row->{$email}) || defined $row->{email} ); + push ( @errors, "No 'from' email column found in report at row $counter") unless defined ( $from || $row->{from} ); + push ( @errors, "Borrower not found for borrowernumber ".$row->{borrowernumber}.", skipping." ) unless Koha::Patrons->find({borrowernumber=>$row->{borrowernumber}}); + + my $from_address = $from || $row->{from}; + my $to_address = $email ? $row->{$email} : $row->{email}; + push ( @errors, "Unable to parse content at row $counter" ) unless my $content = _process_row_TT( $row, $template ); + $letter->{content}=$content; + + $counter++; + next if scalar @errors > $err_count; + + C4::Letters::EnqueueLetter({ + letter => $letter, + borrowernumber => $row->{borrowernumber}, + message_transport_type => 'email', + from_address => $from_address, + to_address => $to_address, + }); + + } + + return @errors; + +} + + + +=head2 ProcessRowTT + + my $content = ProcessRowTT($row_hashref, $template); + +Accepts a hashref containing values and processes them against Template Toolkit +to produce content + +=cut + +sub _process_row_TT { + + my ($row, $template) = @_; + + return 0 unless ($row && $template); + my $content; + my $processor = Template->new(); + $processor->process( \$template, $row, \$content); + return $content; + +} + sub _get_display_value { my ( $original_value, $column ) = @_; if ( $column eq 'periodicity' ) { --- a/misc/cronjobs/patron_emailer.pl +++ a/misc/cronjobs/patron_emailer.pl @@ -0,0 +1,133 @@ +#!/usr/bin/perl + +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +BEGIN { + # find Koha's Perl modules + # test carefully before changing this + use FindBin; + eval { require "$FindBin::Bin/../kohalib.pl" }; +} + +use Getopt::Long; +use Pod::Usage; + +use C4::Reserves; +use C4::Log; +use C4::Reports::Guided; +use Koha::Holds; +use Koha::Calendar; +use Koha::DateUtils; +use Koha::Libraries; +use Koha::Notice::Templates; + +cronlogaction(); + +=head1 NAME + +patron_emailer.pl + +=head1 SYNOPSIS + +patron_emailer.pl + [-report][-notice][-module][-library][-from] + + Options: + -help brief help + -report report to use as data for email template + -notice specific notice code to use + -module which module to find the above notice in + -library specified branch for selecting notice, will use all libraries by default + -from specified email for 'from' address, report column 'from' used if not specified + -email specified column to use as 'to' email address, report column 'email' used if not specified + +head1 OPTIONS + +=over 8 + +=item B<-help> + +Print brief help and exit. + +=item B<-man> + +Print full documentation and exit. + +=item B<-report> + +Specify a saved SQL report in the Koha system to user for the emails. All, and only, + columns in the report will be available for notice template variables + +=item B<-notice> + +Specific notice (CODE) to select + +=item B<-module> + +Which module to find the specified notice in + +=item B<-library> + +Option to specify which branches notice should be used, 'All libraries' is used if not specified + +=item B<-from> + +Specify the sender address of the email, if not specified a 'from' column in the report will be used. + +=item B<-from> + +Specify the column to find recipient address of the email, if not specified an 'email' column in the report will be used. + +=back + +=cut + +my $help = 0; +my $report; +my $notice; +my $module; #this is only for selecting correct report - report itself defines available columns, not module +my $library; #as above, determines which notice to use, will use 'all libraries' if not specified +my $email; #to specify which column should be used as email in report will use 'email' from borrwers table +my $from; #to specify from address, will expect 'from' column in report if not specified +my $verbose = 0; + +GetOptions( + 'help|?' => \$help, + 'report=i' => \$report, + 'notice=s' => \$notice, + 'module=s' => \$module, + 'library=s' => \$library, + 'email=s' => \$email, + 'from=s' => \$from, +) or pod2usage(1); +pod2usage(1) if $help; + +my @errors = C4::Reports::Guided::EmailReport({ + email => $email, + from => $from, + report => $report, + module => $module, + code => $notice, + branchcode => $library, +}); + +foreach my $error ( @errors ){ + print $error."\n"; +} + --