From 148ebdefda2243a4e5f1f2b53bcccf781b7ceb66 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Thu, 24 Oct 2024 16:34:27 +0100 Subject: [PATCH] In category setup, eagerly fetch translations. --- .../FixMyStreet/App/Controller/Report/New.pm | 2 +- perllib/FixMyStreet/DB/ResultSet/Contact.pm | 28 +++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index a4b35a77a1c..001c054c8de 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -767,7 +767,7 @@ sub setup_categories_and_bodies : Private { my $all_areas = $c->stash->{all_areas}; - my @bodies = $c->model('DB::Body')->active->for_areas(keys %$all_areas)->all; + my @bodies = $c->model('DB::Body')->active->for_areas(keys %$all_areas)->translated->all; my %bodies = map { $_->id => $_ } @bodies; $c->cobrand->call_hook(munge_report_new_bodies => \%bodies); diff --git a/perllib/FixMyStreet/DB/ResultSet/Contact.pm b/perllib/FixMyStreet/DB/ResultSet/Contact.pm index 96b6337df41..900baa5f5c7 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Contact.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Contact.pm @@ -52,17 +52,33 @@ sub for_new_reports { $params->{$rs->me('state')} = [ 'unconfirmed', 'confirmed' ]; } - $rs->search($params, { prefetch => 'body' }); + $rs = $rs->search($params, { prefetch => 'body' }); + my $schema = $rs->result_source->schema; + $rs = $rs->search(undef, { + '+columns' => { + 'body.msgstr' => \"COALESCE(translations.msgstr, body.name)", + 'msgstr' => \"COALESCE(translations_2.msgstr, me.category)" + }, + join => [ 'translations', 'translations' ], + bind => [ + 'name', $schema->lang, 'body', + 'category', $schema->lang, 'contact', + ], + }); + return $rs; } sub translated { my $rs = shift; my $schema = $rs->result_source->schema; - $rs->search(undef, { - '+columns' => { 'msgstr' => \"COALESCE(translations.msgstr, me.category)" }, - join => 'translations', - bind => [ 'category', $schema->lang, 'contact' ], - }); + if (!$rs->{attrs}{'+columns'}[0]{msgstr}) { + $rs = $rs->search(undef, { + '+columns' => { 'msgstr' => \"COALESCE(translations.msgstr, me.category)" }, + join => 'translations', + bind => [ 'category', $schema->lang, 'contact' ], + }); + } + return $rs; } sub all_sorted {