From 00f81042c43491694c4d775b15988c1acb2f9ea3 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Fri, 9 Sep 2022 17:12:36 -0400 Subject: [PATCH] LP#1986479: (follow-up) do some refactoring - don't bother checking the reactor; instead, assume that if the event definition includes a context user path that evaluates to a discernable user record, there is reason to check for the user's preferred locale and for alternative templates - make this section of code more idiomatic Signed-off-by: Galen Charlton Signed-off-by: Mike Rylander Signed-off-by: Galen Charlton --- .../lib/OpenILS/Application/Trigger/Event.pm | 74 +++++++++------------- 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Event.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Event.pm index 9c6c490760..323a76bfa0 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Event.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Event.pm @@ -500,55 +500,41 @@ sub build_environment { # ... so that we can see if the event output should use # an alternative template in the patron's preferred locale - my ($usr_locale, $alt_templates, $query, $query_result, $new_template_id); - my $reactor = $self->environment->{event}->event_def->reactor; - $query = { - select => { atevalt => ['id', 'locale'] }, - from => 'atevalt', - where => { - event_def => $self->environment->{event}->event_def->id, - active => 't' - } - }; - my $e = new_editor(xact=>1); - if ($reactor && $self->environment->{context}->{usr}) { - if ( $reactor eq 'SendEmail' - or $reactor eq 'ProcessTemplate' - or $reactor eq 'SendSMS') { - $query_result = $e->json_query($query); - $alt_templates = $query_result; - $query = { - select => { au => ['locale'] }, - from => 'au', - where => { id => $self->environment->{context}->{usr}->id } - }; - $query_result = $e->json_query($query); - $usr_locale = @$query_result[0]->{locale}; - if ($alt_templates and @$alt_templates and $usr_locale) { - foreach (@$alt_templates) { - if ($_->{locale} eq $usr_locale) { + if ($self->environment->{context}->{usr}) { + my ($usr_locale, $alt_templates, $query_result, $new_template_id); + my $editor = new_editor(xact=>1); + $alt_templates = $editor->json_query({ + select => { atevalt => ['id', 'locale'] }, + from => 'atevalt', + where => { + event_def => $self->environment->{event}->event_def->id, + active => 't' + } + }); + $usr_locale = $self->environment->{context}->{usr}->locale; + if ($alt_templates and @$alt_templates and $usr_locale) { + foreach (@$alt_templates) { + if ($_->{locale} eq $usr_locale) { + $new_template_id = $_->{id}; + $self->environment->{tt_locale} = $_->{locale}; + last; + } else { #attempt a lanuage if not locale match + if ((split /\p{Dash}/,$_->{locale})[0] eq (split /\p{Dash}/,$usr_locale)[0]) { $new_template_id = $_->{id}; $self->environment->{tt_locale} = $_->{locale}; - last; - } else { #attempt a lanuage if not locale match - if ((split /\p{Dash}/,$_->{locale})[0] eq (split /\p{Dash}/,$usr_locale)[0]) { - $new_template_id = $_->{id}; - $self->environment->{tt_locale} = $_->{locale}; - } } } } - if ($new_template_id) { - $query = { - select => { atevalt => ['template','message_template','message_title'] }, - from => 'atevalt', - where => { id => $new_template_id } - }; - $query_result = $e->json_query($query); - $self->environment->{template} = @$query_result[0]->{template}; - $self->environment->{usr_message}{template} = @$query_result[0]->{message_template}; - $self->environment->{usr_message}{title} = @$query_result[0]->{message_title}; - } + } + if ($new_template_id) { + $query_result = $editor->json_query({ + select => { atevalt => ['template','message_template','message_title'] }, + from => 'atevalt', + where => { id => $new_template_id } + }); + $self->environment->{template} = @$query_result[0]->{template}; + $self->environment->{usr_message}{template} = @$query_result[0]->{message_template}; + $self->environment->{usr_message}{title} = @$query_result[0]->{message_title}; } } $current_environment = $self->environment; -- 2.11.0