1 package OpenILS::Application::Trigger::Reactor;
2 use strict; use warnings;
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';
12 sub fourty_two { return 42 }
13 sub NOOP_True { return 1 }
14 sub NOOP_False { return 0 }
18 # helper functions inserted into the TT environment
21 # turns a date into something TT can understand
24 $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
26 "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
36 # escapes a string for inclusion in an XML document. escapes &, <, and > characters
39 $str =~ s/&/&/sog;
47 $str =~ s/([\x{0080}-\x{fffd}])/sprintf('\u%0.4x',ord($1))/sgoe;
51 # returns the calculated user locale
52 get_user_locale => sub {
54 return $U->get_user_locale($user_id);
57 # returns the calculated copy price
58 get_copy_price => sub {
60 return $U->get_copy_price(new_editor(), $copy_id);
63 # given a copy, returns the title and author in a hash
64 get_copy_bib_basics => sub {
66 my $copy = new_editor()->retrieve_asset_copy([
71 acp => ['call_number'],
76 if($copy->call_number->id == -1) {
78 title => $copy->dummy_title,
79 author => $copy->dummy_author,
82 my $mvr = $U->record_to_mvr($copy->call_number->record);
85 author => $mvr->author
90 # returns the org unit setting value
91 get_org_setting => sub {
92 my($org_id, $setting) = @_;
93 return $U->ou_ancestor_setting_value($org_id, $setting);
96 # This basically greps/maps out ths isbn string values, but also promotes the first isbn-13 to the
97 # front of the line (so that the EDI translator takes it as primary) if there is one.
103 $_->attr_name eq 'isbn' or next;
104 my $val = $_->attr_value;
105 if (! $primary and length($val) == 13) {
111 $primary and unshift @isbns, $primary;
112 $logger->error("get_li_isbns returning isbns: " . join(', ', @isbns));
116 # helpers.get_li_attr('isbn_13', li.attributes)
117 # returns matching line item attribute, or undef
119 my $name = shift or return; # the first arg is always the name
120 my ($type, $attr) = (scalar(@_) == 1) ? (undef, $_[0]) : @_;
121 # if the next is the last, it's the attributes, otherwise type
122 # use Data::Dumper; $logger->warn("get_li_attr: " . Dumper($attr));
123 ($name and @$attr) or return;
125 $name =~ s/^(\D+)_(\d+)$/$1/ and $length = $2;
127 $_->attr_name eq $name or next;
128 next if $length and $length != length($_->attr_value);
129 return $_->attr_value if (! $type) or $type eq $_->attr_type;
136 # processes templates. Returns template output on success, undef on error
141 return undef unless $env->{template};
145 my $tt = Template->new;
146 # my $tt = Template->new(ENCODING => 'utf8'); # ??
147 $env->{helpers} = $_TT_helpers;
149 unless( $tt->process(\$env->{template}, $env, \$output) ) {
151 ($error = $tt->error) =~ s/\n/ /og;
152 $logger->error("Error processing Trigger template: $error");
155 if ( $error or (!$nostore && $output) ) {
156 my $t_o = Fieldmapper::action_trigger::event_output->new;
157 $t_o->data( ($error) ? $error : $output );
158 $t_o->is_error( ($error) ? 't' : 'f' );
160 $env->{EventProcessor}->editor->xact_begin;
161 $t_o = $env->{EventProcessor}->editor->create_action_trigger_event_output( $t_o );
163 my $state = (ref $$env{event} eq 'ARRAY') ? $$env{event}->[0]->state : $env->{event}->state;
164 my $key = ($error) ? 'error_output' : 'template_output';
165 $env->{EventProcessor}->update_state( $state, { $key => $t_o->id } );