]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor.pm
aa93eb1830d86c0dce4a33d8917b8140d0f2b485
[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     # given a copy, returns the title and author in a hash
59     get_copy_bib_basics => sub {
60         my $copy_id = shift;
61         my $copy = new_editor()->retrieve_asset_copy([
62             $copy_id,
63             {
64                 flesh => 2,
65                 flesh_fields => {
66                     acp => ['call_number'],
67                     acn => ['record']
68                 }
69             }
70         ]);
71         if($copy->call_number->id == -1) {
72             return {
73                 title => $copy->dummy_title,
74                 author => $copy->dummy_author,
75             };
76         } else {
77             my $mvr = $U->record_to_mvr($copy->call_number->record);
78             return {
79                 title => $mvr->title,
80                 author => $mvr->author
81             };
82         }
83     },
84
85     # returns the org unit setting value
86     get_org_setting => sub {
87         my($org_id, $setting) = @_;
88         return $U->ou_ancestor_setting_value($org_id, $setting);
89     },
90 };
91
92
93 # processes templates.  Returns template output on success, undef on error
94 sub run_TT {
95     my $self = shift;
96     my $env = shift;
97     my $nostore = shift;
98     return undef unless $env->{template};
99
100     my $error;
101     my $output = '';
102     my $tt = Template->new;
103     $env->{helpers} = $_TT_helpers;
104
105     unless( $tt->process(\$env->{template}, $env, \$output) ) {
106         $output = undef;
107         ($error = $tt->error) =~ s/\n/ /og;
108         $logger->error("Error processing Trigger template: $error");
109     }
110
111     if ( $error or (!$nostore && $output) ) {
112         my $t_o = Fieldmapper::action_trigger::event_output->new;
113         $t_o->data( ($error) ? $error : $output );
114         $t_o->is_error( ($error) ? 't' : 'f' );
115
116         $env->{EventProcessor}->editor->xact_begin;
117         $t_o = $env->{EventProcessor}->editor->create_action_trigger_event_output( $t_o );
118
119         my $state = (ref $$env{event} eq 'ARRAY') ? $$env{event}->[0]->state : $env->{event}->state;
120         my $key = ($error) ? 'error_output' : 'template_output';
121         $env->{EventProcessor}->update_state( $state, { $key => $t_o->id } );
122     }
123         
124     return $output;
125 }
126
127
128 1;