Bugzilla – Attachment 66398 Details for
Bug 16149
Generate and send custom notices based on report output
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 16149 - Generate and send custom notices based on report output
Bug-16149---Generate-and-send-custom-notices-based.patch (text/plain), 9.01 KB, created by
Nick Clemens
on 2017-08-23 22:48:30 UTC
(
hide
)
Description:
Bug 16149 - Generate and send custom notices based on report output
Filename:
MIME Type:
Creator:
Nick Clemens
Created:
2017-08-23 22:48:30 UTC
Size:
9.01 KB
patch
obsolete
>From 6945c855d3bb3d0f871abe096202b9c025ef7cd6 Mon Sep 17 00:00:00 2001 >From: Nick Clemens <nick@bywatersolutions.com> >Date: Mon, 17 Apr 2017 11:20:25 -0400 >Subject: [PATCH] Bug 16149 - Generate and send custom notices based on report > output > >Ths patch add an EmailReport function to C4::Reports::Guided > >It accepts a notice (module, code, branch) and a report and attempts to >email notices to patron, generating content using report content. > >Notice must be in template toolkit syntax, only columns in report are >available for notice. > >To test: >1 - Specify various options >2 - Ensure errors are returned if options are incomplete or incorrect >3 - Pass a report containing 'from' and 'email' and 'borrowernumber' >columns and ensure message queue populated as expected >--- > C4/Reports/Guided.pm | 100 +++++++++++++++++++++++++++ > misc/cronjobs/patron_emailer.pl | 149 ++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 249 insertions(+) > create mode 100755 misc/cronjobs/patron_emailer.pl > >diff --git a/C4/Reports/Guided.pm b/C4/Reports/Guided.pm >index 717d44f..ee123bb 100644 >--- a/C4/Reports/Guided.pm >+++ b/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; >@@ -975,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' ) { >diff --git a/misc/cronjobs/patron_emailer.pl b/misc/cronjobs/patron_emailer.pl >new file mode 100755 >index 0000000..f19c0dc >--- /dev/null >+++ b/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 <http://www.gnu.org/licenses>. >+ >+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"; >+ } >+} >-- >2.1.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 16149
:
62217
|
63454
|
63455
|
66398
|
66399
|
67392
|
67393
|
78890
|
78891
|
78896
|
78897
|
79328
|
79329
|
79330
|
83890
|
83891
|
85116
|
85117
|
88231
|
88232