@@ -, +, @@ output --- C4/Reports/Guided.pm | 101 ++++++++++++++++++++++++++- misc/cronjobs/patron_emailer.pl | 149 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 249 insertions(+), 1 deletion(-) create mode 100755 misc/cronjobs/patron_emailer.pl --- a/C4/Reports/Guided.pm +++ a/C4/Reports/Guided.pm @@ -27,9 +27,12 @@ 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; +use Koha::Notice::Templates; +use C4::Letters; use Koha::AuthorisedValues; use Koha::Patron::Categories; @@ -854,7 +857,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 +978,103 @@ 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} || ""; + my $verbose = $params->{verbose} || 0; + my $commit = $params->{commit} || 0; + + my @errors; + + return ( { FATAL => "MISSING_PARAMS" } ) unless ($report && $module && $code); + + return ( { FATAL => "NO_LETTER" } ) unless + my $letter = Koha::Notice::Templates->find({ + module =>$module, + code => $code, + branchcode => $branch, + message_transport_type => 'email', + }); + $letter = $letter->unblessed; + push ( @errors, { LETTER_FOUND => 1 } ) if $verbose; + + return ( { FATAL => "NO_REPORT" } ) unless my $sql = get_saved_report( $report )->{savedsql}; + push ( @errors, { REPORT_FOUND => 1 } ) if $verbose; + + my ( $sth, $errors ) = execute_query( $sql ); #don't pass offset or limit, hardcoded limit of 999,999 will be used + return ( { FATAL => "REPORT_FAIL" } ) if $errors; + push ( @errors, { REPORT_SUCCEED => 1 } ) if $verbose; + + my $counter = 0; + my $template = $letter->{content}; + + while ( my $row = $sth->fetchrow_hashref() ) { + my $err_count = scalar @errors; + push ( @errors, { NO_BOR_COL => $counter } ) unless defined $row->{borrowernumber}; + push ( @errors, { NO_EMAIL_COL => $counter } ) unless ( (defined $email && defined $row->{$email}) || defined $row->{email} ); + push ( @errors, { NO_FROM_COL => $counter } ) unless defined ( $from || $row->{from} ); + push ( @errors, { NO_BOR => $row->{borrowernumber} } ) unless Koha::Patrons->find({borrowernumber=>$row->{borrowernumber}}); + + my $from_address = $from || $row->{from}; + my $to_address = $email ? $row->{$email} : $row->{email}; + push ( @errors, { NOT_PARSE => $counter } ) unless my $content = _process_row_TT( $row, $template ); + $letter->{content}=$content; + + $counter++; + next if scalar @errors > $err_count; + push ( @errors, { SENDING_SUCCEED => $counter-1 } ) if $verbose; + + C4::Letters::EnqueueLetter({ + letter => $letter, + borrowernumber => $row->{borrowernumber}, + message_transport_type => 'email', + from_address => $from_address, + to_address => $to_address, + }) if $commit; + + } + + 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,149 @@ +#!/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 + -verbose increased verbosity + -commit send emails, without this script will only report + +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<-email> + +Specify the column to find recipient address of the email, if not specified an 'email' column in the report will be used. + +=item B<-verbose> + +Increased verbosity, reports sucesses and errors. + +=item B<-commit> + +Send emails, if ommited script will only report. + +=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; +my $commit = 0; + +GetOptions( + 'help|?' => \$help, + 'report=i' => \$report, + 'notice=s' => \$notice, + 'module=s' => \$module, + 'library=s' => \$library, + 'email=s' => \$email, + 'from=s' => \$from, + 'verbose' => \$verbose, + 'commit' => \$commit +) 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, + verbose => $verbose, + commit => $commit, +}); + +foreach my $err_hash ( @errors ){ + foreach ( keys %{$err_hash} ){ + print "$_ => ${$err_hash}{$_}\n"; + } +} --