]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor.pm
Patch from Joe Atzberger to add a line item 'worksheet' print template and a helper...
[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 # helper functions inserted into the TT environment
19 my $_TT_helpers = {
20
21     # turns a date into something TT can understand
22     format_date => sub {
23         my $date = shift;
24         $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
25         return sprintf(
26             "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
27             $date->hour,
28             $date->minute,
29             $date->second,
30             $date->day,
31             $date->month,
32             $date->year
33         );
34     },
35
36     # escapes a string for inclusion in an XML document.  escapes &, <, and > characters
37     escape_xml => sub {
38         my $str = shift;
39         $str =~ s/&/&amp;/sog;
40         $str =~ s/</&lt;/sog;
41         $str =~ s/>/&gt;/sog;
42         return $str;
43     },
44
45     # returns the calculated user locale
46     get_user_locale => sub { 
47         my $user_id = shift;
48         return $U->get_user_locale($user_id);
49     },
50
51     # returns the calculated copy price
52     get_copy_price => sub {
53         my $copy_id = shift;
54         return $U->get_copy_price(new_editor(), $copy_id);
55     },
56
57     # given a copy, returns the title and author in a hash
58     get_copy_bib_basics => sub {
59         my $copy_id = shift;
60         my $copy = new_editor()->retrieve_asset_copy([
61             $copy_id,
62             {
63                 flesh => 2,
64                 flesh_fields => {
65                     acp => ['call_number'],
66                     acn => ['record']
67                 }
68             }
69         ]);
70         if($copy->call_number->id == -1) {
71             return {
72                 title => $copy->dummy_title,
73                 author => $copy->dummy_author,
74             };
75         } else {
76             my $mvr = $U->record_to_mvr($copy->call_number->record);
77             return {
78                 title => $mvr->title,
79                 author => $mvr->author
80             };
81         }
82     },
83
84     # returns the org unit setting value
85     get_org_setting => sub {
86         my($org_id, $setting) = @_;
87         return $U->ou_ancestor_setting_value($org_id, $setting);
88     },
89
90     # returns matching line item attribute, or undef
91     get_li_attr => sub {
92         my ($name, $type, $attr) = @_;
93         # use Data::Dumper; $logger->warn("get_li_attr: " . Dumper($attr));
94         ($name and @$attr) or return;
95         foreach (@$attr) {
96             $_->attr_name eq $name or next;
97             return $_->attr_value if (! $type) or $type eq $_->attr_type;
98         }
99         return;
100     },
101 };
102
103
104 # processes templates.  Returns template output on success, undef on error
105 sub run_TT {
106     my $self = shift;
107     my $env = shift;
108     my $nostore = shift;
109     return undef unless $env->{template};
110
111     my $error;
112     my $output = '';
113     my $tt = Template->new;
114     $env->{helpers} = $_TT_helpers;
115
116     unless( $tt->process(\$env->{template}, $env, \$output) ) {
117         $output = undef;
118         ($error = $tt->error) =~ s/\n/ /og;
119         $logger->error("Error processing Trigger template: $error");
120     }
121
122     if ( $error or (!$nostore && $output) ) {
123         my $t_o = Fieldmapper::action_trigger::event_output->new;
124         $t_o->data( ($error) ? $error : $output );
125         $t_o->is_error( ($error) ? 't' : 'f' );
126
127         $env->{EventProcessor}->editor->xact_begin;
128         $t_o = $env->{EventProcessor}->editor->create_action_trigger_event_output( $t_o );
129
130         my $state = (ref $$env{event} eq 'ARRAY') ? $$env{event}->[0]->state : $env->{event}->state;
131         my $key = ($error) ? 'error_output' : 'template_output';
132         $env->{EventProcessor}->update_state( $state, { $key => $t_o->id } );
133     }
134         
135     return $output;
136 }
137
138
139 1;