LP#1986479: (follow-up) do some refactoring
authorGalen Charlton <gmc@equinoxOLI.org>
Fri, 9 Sep 2022 21:12:36 +0000 (17:12 -0400)
committerGalen Charlton <gmc@equinoxOLI.org>
Tue, 11 Oct 2022 19:49:38 +0000 (15:49 -0400)
- 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 <gmc@equinoxOLI.org>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@equinoxOLI.org>
Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Event.pm

index 9c6c490..323a76b 100644 (file)
@@ -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;