From 4d74dbfa38483ebeb658e9cba41906703eaa1117 Mon Sep 17 00:00:00 2001 From: Stephen Nielson Date: Wed, 7 Aug 2024 21:26:21 -0600 Subject: [PATCH] Fixes #7621 validation save errors w/ translations (#7622) * Fixes #7621 validation save errors w/ translations The translation system was not setup in the OnSiteDocumentController and a bug in utility.js would blow up preventing the questionnaire from saving. I added more safety checks to the xl translation function. Injected the language translations into the OnSiteDocumentController and abstracted the language definition retrievals into a service utility class. * Update copyright with correct verbiage. * Accidently had commented out line --- library/js/utility.js | 7 ++-- portal/home.php | 12 +----- .../Controller/OnsiteDocumentController.php | 4 ++ .../templates/OnsiteDocumentListView.tpl.php | 19 ++++++++-- src/Services/Utils/TranslationService.php | 37 +++++++++++++++++++ 5 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 src/Services/Utils/TranslationService.php diff --git a/library/js/utility.js b/library/js/utility.js index e44047b1631..e39451fe9c9 100644 --- a/library/js/utility.js +++ b/library/js/utility.js @@ -12,9 +12,10 @@ /* We should really try to keep this library jQuery free ie javaScript only! */ // Translation function -// This calls the i18next.t function that has been set up in main.php +// This calls the i18next.t function that has been set up in main.php, portal/base.html.twig, etc. function xl(string) { - if (typeof top.i18next.t == 'function') { + // safety check if for some reason the i18next is not included. + if (top.i18next && typeof top.i18next.t == 'function') { return top.i18next.t(string); } else { // Unable to find the i18next.t function, so log error @@ -558,4 +559,4 @@ function isValidEmail(emailAddress) { } else { return false; } -} \ No newline at end of file +} diff --git a/portal/home.php b/portal/home.php index ec1930ab3ab..9a038643eeb 100644 --- a/portal/home.php +++ b/portal/home.php @@ -27,6 +27,7 @@ use OpenEMR\Events\PatientPortal\AppointmentFilterEvent; use OpenEMR\Events\PatientPortal\RenderEvent; use OpenEMR\Services\LogoService; +use OpenEMR\Services\Utils\TranslationService; use Twig\Error\LoaderError; use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; @@ -46,16 +47,7 @@ // Get language definitions for js -$language = $_SESSION['language_choice'] ?? '1'; // defaults english -$sql = "SELECT c.constant_name, d.definition FROM lang_definitions as d - JOIN lang_constants AS c ON d.cons_id = c.cons_id - WHERE d.lang_id = ?"; -$tarns = sqlStatement($sql, $language); -$language_defs = array(); -while ($row = SqlFetchArray($tarns)) { - $language_defs[$row['constant_name']] = $row['definition']; -} - +$language_defs = TranslationService::getLanguageDefinitionsForSession(); $whereto = $_SESSION['whereto'] ?? null; $user = $_SESSION['sessionUser'] ?? 'portal user'; diff --git a/portal/patient/libs/Controller/OnsiteDocumentController.php b/portal/patient/libs/Controller/OnsiteDocumentController.php index 432cb0f0756..cd83c33d27f 100644 --- a/portal/patient/libs/Controller/OnsiteDocumentController.php +++ b/portal/patient/libs/Controller/OnsiteDocumentController.php @@ -13,6 +13,7 @@ /** import supporting libraries */ use OpenEMR\Services\DocumentTemplates\DocumentTemplateRender; +use OpenEMR\Services\Utils\TranslationService; require_once("AppBasePortalController.php"); require_once("Model/OnsiteDocument.php"); @@ -75,6 +76,9 @@ public function ListView() unset($_GET['auto_render_name']); unset($_GET['audit_render_id']); + $language_defs = TranslationService::getLanguageDefinitionsForSession(); + + $this->Assign("language_defs", $language_defs); $this->Assign('doc_edit', $doc_edit); $this->Assign('recid', $recid); $this->Assign('help_id', $help_id); diff --git a/portal/patient/templates/OnsiteDocumentListView.tpl.php b/portal/patient/templates/OnsiteDocumentListView.tpl.php index 36244499d86..e0232d6ea76 100644 --- a/portal/patient/templates/OnsiteDocumentListView.tpl.php +++ b/portal/patient/templates/OnsiteDocumentListView.tpl.php @@ -93,9 +93,9 @@ echo ""; if ($is_portal) { - Header::setupHeader(['no_main-theme', 'portal-theme', 'datetime-picker']); + Header::setupHeader(['no_main-theme', 'portal-theme', 'datetime-picker', 'i18next']); } else { - Header::setupHeader(['datetime-picker']); + Header::setupHeader(['datetime-picker', 'i18next']); } ?> @@ -111,7 +111,20 @@ $LAB.script("/underscore/underscore-min.js").script("/moment/moment.js").script( "/backbone/backbone-min.js").script("/portal/patient/scripts/app.js?v=").script( "/portal/patient/scripts/model.js?v=").wait().script( - "/portal/patient/scripts/view.js?v=").wait() + "/portal/patient/scripts/view.js?v=").wait(); + i18next.init({ + lng: 'selected', + debug: false, + nsSeparator: false, + keySeparator: false, + resources: { + selected: { + translation: language_defs ?? []); ?> + } + } + }).catch(error => { + console.log(error.message); + });