]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor.pm
added template callback for getting user fines summary
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Trigger / Reactor.pm
1 package OpenILS::Application::Trigger::Reactor;
2 use strict; use warnings;
3 use Template;
4 use DateTime;
5 use DateTime::Format::ISO8601;
6 use OpenSRF::Utils qw/:datetime/;
7 use OpenSRF::Utils::Logger qw(:logger);
8 use OpenILS::Application::AppUtils;
9 use OpenILS::Utils::CStoreEditor qw/:funcs/;
10 my $U = 'OpenILS::Application::AppUtils';
11
12 sub fourty_two { return 42 }
13 sub NOOP_True { return 1 }
14 sub NOOP_False { return 0 }
15
16
17
18
19 # helper functions inserted into the TT environment
20 my $_TT_helpers = {
21
22     # turns a date into something TT can understand
23     format_date => sub {
24         my $date = shift;
25         $date = DateTime::Format::ISO8601->new->parse_datetime(clense_ISO8601($date));
26         return sprintf(
27             "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
28             $date->hour,
29             $date->minute,
30             $date->second,
31             $date->day,
32             $date->month,
33             $date->year
34         );
35     },
36
37     # escapes a string for inclusion in an XML document.  escapes &, <, and > characters
38     escape_xml => sub {
39         my $str = shift;
40         $str =~ s/&/&amp;/sog;
41         $str =~ s/</&lt;/sog;
42         $str =~ s/>/&gt;/sog;
43         return $str;
44     },
45
46     # returns the calculated user locale
47     get_user_locale => sub { 
48         my $user_id = shift;
49         return $U->get_user_locale($user_id);
50     },
51
52     # returns the calculated copy price
53     get_copy_price => sub {
54         my $copy_id = shift;
55         return $U->get_copy_price(new_editor(), $copy_id);
56     },
57
58     # returns the org unit setting value
59     get_org_setting => sub {
60         my($org_id, $setting) = @_;
61         return $U->ou_ancestor_setting_value($org_id, $setting);
62     },
63
64     # returns fines summary information for open transactions
65     get_user_fines_summary => sub {
66         my $user_id = shift;
67         return $U->simplereq(
68             'open-ils.storage', 
69             'open-ils.storage.money.open_user_summary.search', $user_id);
70     }
71 };
72
73
74 # processes templates.  Returns template output on success, undef on error
75 sub run_TT {
76     my $self = shift;
77     my $env = shift;
78     my $nostore = shift;
79     return undef unless $env->{template};
80
81     my $error;
82     my $output = '';
83     my $tt = Template->new;
84     $env->{helpers} = $_TT_helpers;
85
86     unless( $tt->process(\$env->{template}, $env, \$output) ) {
87         $output = undef;
88         ($error = $tt->error) =~ s/\n/ /og;
89         $logger->error("Error processing Trigger template: $error");
90     }
91
92     if ( $error or (!$nostore && $output) ) {
93         my $t_o = Fieldmapper::action_trigger::event_output->new;
94         $t_o->data( ($error) ? $error : $output );
95         $t_o->is_error( ($error) ? 't' : 'f' );
96
97         $env->{EventProcessor}->editor->xact_begin;
98         $t_o = $env->{EventProcessor}->editor->create_action_trigger_event_output( $t_o );
99
100         my $state = (ref $$env{event} eq 'ARRAY') ? $$env{event}->[0]->state : $env->{event}->state;
101         my $key = ($error) ? 'error_output' : 'template_output';
102         $env->{EventProcessor}->update_state( $state, { $key => $t_o->id } );
103     }
104         
105     return $output;
106 }
107
108
109 1;