From 6b8599053e6a0547917291cb2596a4a6b06cdaab Mon Sep 17 00:00:00 2001 From: erickson Date: Thu, 7 Aug 2008 19:33:15 +0000 Subject: [PATCH] added logic for generating a template from the combined overdue data (e.g. xml file). added sample combined xml file. git-svn-id: svn://svn.open-ils.org/ILS/trunk@10306 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/examples/opensrf.xml.example | 5 +- Open-ILS/examples/templates/overdue_7day.example | 11 ++- .../templates/overdue_combined_xml.example | 53 ++++++++++++ Open-ILS/examples/templates/predue_1day.example | 6 +- .../src/support-scripts/generate_circ_notices.pl | 93 ++++++++++++++++------ 5 files changed, 131 insertions(+), 37 deletions(-) create mode 100644 Open-ILS/examples/templates/overdue_combined_xml.example diff --git a/Open-ILS/examples/opensrf.xml.example b/Open-ILS/examples/opensrf.xml.example index d9b13386fc..4cb1e76fba 100644 --- a/Open-ILS/examples/opensrf.xml.example +++ b/Open-ILS/examples/opensrf.xml.example @@ -59,6 +59,7 @@ vim:et:ts=4:sw=4: /openils/var/data/overdue + /openils/var/data/templates/overdue_combined_xml.example @@ -69,7 +70,7 @@ vim:et:ts=4:sw=4: true - + /openils/var/data/templates/overdue_7day.example @@ -85,7 +86,7 @@ vim:et:ts=4:sw=4: 1 day false true - + /openils/var/data/templates/predue_1day.example diff --git a/Open-ILS/examples/templates/overdue_7day.example b/Open-ILS/examples/templates/overdue_7day.example index a79e19bd68..45802b4d03 100644 --- a/Open-ILS/examples/templates/overdue_7day.example +++ b/Open-ILS/examples/templates/overdue_7day.example @@ -1,3 +1,4 @@ +[%- USE date -%] [%- SET user = circ_list.0.usr -%] [%- SET lib = circ_list.0.circ_lib -%] To: [% user.email %] @@ -12,9 +13,8 @@ Our records indicate these items are 7 days overdue: [% FOREACH circ = circ_list %] [% get_bib_attr(circ, 'title') %], by [% get_bib_attr(circ, 'author') %] Call Number: [% circ.target_copy.call_number.label %] - [%- SET date = parse_due_date(circ) %] - Due Date: [% date.year %]-[% date.month %]-[% date.day -%] - [%- IF date.hour -%] [% date.hour %]:[% date.minute %] [%- END %] + [%- SET due_date = parse_due_date(circ) %] + Due Date: [% date.format(due_date, '%Y-%m-%d') %] Barcode: [% circ.target_copy.barcode %] [% END %] @@ -25,9 +25,8 @@ respond to this email. Contact your library for more information: [% lib.name %] -[%- SET addr = lib.mailing_address -%] -[%- IF !addr -%] [%- SET addr = lib.billing_address -%] [%- END %] -[% lib.mailing_address.street1 %] [% lib.mailing_address.street2 %] +[% lib.mailing_address.street1 %] +[% lib.mailing_address.street2 %] [% lib.mailing_address.city %], [% lib.mailing_address.state %] [% lib.mailing_address.post_code %] [% lib.phone %] diff --git a/Open-ILS/examples/templates/overdue_combined_xml.example b/Open-ILS/examples/templates/overdue_combined_xml.example new file mode 100644 index 0000000000..85dea3dc44 --- /dev/null +++ b/Open-ILS/examples/templates/overdue_combined_xml.example @@ -0,0 +1,53 @@ +[% USE date %] + + + [%- FOREACH circ_set = overdues %] + + [%- SET user = circ_set.circ_list.0.usr -%] + [%- SET lib = circ_set.circ_list.0.circ_lib -%] + [%- SET user_addr = user.mailing_address -%] + [%- IF !user_addr -%] + [%- SET user_addr = user.billing_address -%] + [%- END %] + [%- SET lib_addr = lib.mailing_address -%] + [%- IF !lib_addr -%] + [%- SET lib_addr = lib.billing_address -%] + [%- END %] + + [% user.card.barcode %] + [% user.first_given_name %] + [% user.family_name %] + [% escape_xml(user_addr.street1) %] + [% escape_xml(user_addr.street2) %] + [% escape_xml(user_addr.city) %] + [% user_addr.state %] + [% user_addr.post_code %] + [% user.email %] + [% user.id %] + + + [% escape_xml(lib.name) %] + [% lib.phone %] + [% escape_xml(lib_addr.street1) %] + [% escape_xml(lib_addr.street2) %] + [% escape_xml(lib_addr.city) %] + [% lib_addr.state %] + [% lib_addr.post_code %] + [% lib.email %] + [% lib.id %] + + [%- FOREACH circ = circ_set.circ_list %] + [%- SET due_date = parse_due_date(circ) %] + + [% escape_xml(get_bib_attr(circ, 'title')) %] + [% escape_xml(get_bib_attr(circ, 'author')) %] + [% date.format(due_date, '%Y-%m-%d') %] + [% escape_xml(circ.target_copy.call_number.label) %] + [% escape_xml(circ.target_copy.barcode) %] + [% circ.id %] + + [%- END %] + + [%- END %] + + diff --git a/Open-ILS/examples/templates/predue_1day.example b/Open-ILS/examples/templates/predue_1day.example index 30709cf71d..c0823926c6 100644 --- a/Open-ILS/examples/templates/predue_1day.example +++ b/Open-ILS/examples/templates/predue_1day.example @@ -1,3 +1,4 @@ +[%- USE date -%] [%- SET user = circ_list.0.usr -%] [%- SET lib = circ_list.0.circ_lib -%] To: [% user.email %] @@ -12,9 +13,8 @@ Our records indicate these items are due tomorrow: [% FOREACH circ = circ_list %] [% get_bib_attr(circ, 'title') %], by [% get_bib_attr(circ, 'author') %] Call Number: [% circ.target_copy.call_number.label %] - [%- SET date = parse_due_date(circ) %] - Due Date: [% date.year %]-[% date.month %]-[% date.day -%] - [%- IF date.hour -%] [% date.hour %]:[% date.minute %] [%- END %] + [%- SET due_date = parse_due_date(circ) %] + Due Date: [% date.format(due_date, '%Y-%m-%d') %] Barcode: [% circ.target_copy.barcode %] [% END %] diff --git a/Open-ILS/src/support-scripts/generate_circ_notices.pl b/Open-ILS/src/support-scripts/generate_circ_notices.pl index c25033908b..97c451e2fd 100755 --- a/Open-ILS/src/support-scripts/generate_circ_notices.pl +++ b/Open-ILS/src/support-scripts/generate_circ_notices.pl @@ -35,8 +35,10 @@ my $U = 'OpenILS::Application::AppUtils'; my $settings = undef; my $e = OpenILS::Utils::CStoreEditor->new; +my @global_overdue_circs; # all circ collections stored here go into the final global XML file + my ($osrf_config, $send_email, $gen_day_intervals, $days_back) = - ('/openils/conf/opensrf_core.xml', 1, 0, 0); + ('/openils/conf/opensrf_core.xml', 0, 0, 0); GetOptions( 'osrf_osrf_config=s' => \$osrf_config, @@ -85,6 +87,30 @@ sub main { generate_notice_set($_, 'overdue') for @overdues; generate_notice_set($_, 'predue') for @predues; + + generate_global_overdue_file(); +} + +sub generate_global_overdue_file { + $logger->info("notice: processing ".scalar(@global_overdue_circs)." for global template"); + return unless @global_overdue_circs; + + my $tt = Template->new({ABSOLUTE => 1}); + + $tt->process( + $settings->config_value(notifications => overdue => 'combined_template'), + { + overdues => \@global_overdue_circs, + get_bib_attr => \&get_bib_attr, + parse_due_date => \&parse_due_date, # let the templates decide date format + escape_xml => \&escape_xml, + }, + \&global_overdue_output + ) or $logger->error('notice: Template error '.$tt->error); +} + +sub global_overdue_output { + print shift() . "\n"; } @@ -116,7 +142,7 @@ sub generate_notice_set { }; # if a circ duration is defined for this type of notice - if(my $durs = $settings->{circ_duration_range}) { + if(my $durs = $notice->{circ_duration_range}) { $QUERY->{where}->{'+circ'}->{duration} = {between => [$durs->{from}, $durs->{to}]}; } @@ -175,16 +201,27 @@ sub generate_notice { notifications => $type => 'sender_address') || $settings->config_value(notifications => 'sender_address'); - $tt->process( - $notice->{template}, - { circ_list => $circ_list, - get_bib_attr => \&get_bib_attr, - parse_due_date => \&parse_due_date, # let the templates decide date format - smtp_sender => $sender, - smtp_repley => $sender # XXX - }, - \&handle_template_output - ) or $logger->error('notice: Template error '.$tt->error); + my $context = { + circ_list => $circ_list, + get_bib_attr => \&get_bib_attr, + parse_due_date => \&parse_due_date, # let the templates decide date format + smtp_sender => $sender, + smtp_repley => $sender, # XXX + notice => $notice, + }; + + push(@global_overdue_circs, $context) if + $type eq 'overdue' and $notice->{file_append} =~ /always/i; + + if($send_email and $circ_list->[0]->usr->email) { + if(my $tmpl = $notice->{email_template}) { + $tt->process($tmpl, $context, \&email_template_output) + or $logger->error('notice: Template error '.$tt->error); + } + } else { + push(@global_overdue_circs, $context) + if $type eq 'overdue' and $notice->{file_append} =~ /noemail/i; + } } sub get_bib_attr { @@ -201,31 +238,35 @@ sub get_bib_attr { } } +# provides a date that Template::Plugin::Date can parse sub parse_due_date { my $circ = shift; my $due = DateTime::Format::ISO8601->new->parse_datetime(clense_ISO8601($circ->due_date)); - my $info = { - year => $due->year, - month => sprintf("%0.2d",$due->month), - day => sprintf("%0.2d", $due->day) - }; - - # for day-based circulations, hour and minute are not relevant - return $info if (OpenSRF::Utils->interval_to_seconds($circ->duration) % 86400) == 0; + return sprintf( + "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.2d", + $due->hour, + $due->minute, + $due->second, + $due->day, + $due->month, + $due->year + ); +} - $info->{hour} = sprintf("%0.2d",$due->hour); - $info->{minute} = sprintf("%0.2d",$due->minute); - return $info; +sub escape_xml { + my $str = shift; + $str =~ s/&/&/sog; + $str =~ s//>/sog; + return $str; } -sub handle_template_output { +sub email_template_output { my $str = shift; print "$str\n"; } - - sub fetch_circ_data { my @circs = @_; -- 2.11.0