Skip to content

Commit

Permalink
Fixes openemr#7621 validation save errors w/ translations (openemr#7622)
Browse files Browse the repository at this point in the history
* Fixes openemr#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
  • Loading branch information
adunsulag authored Aug 8, 2024
1 parent be599d6 commit 4d74dbf
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
7 changes: 4 additions & 3 deletions library/js/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -558,4 +559,4 @@ function isValidEmail(emailAddress) {
} else {
return false;
}
}
}
12 changes: 2 additions & 10 deletions portal/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
Expand Down
4 changes: 4 additions & 0 deletions portal/patient/libs/Controller/OnsiteDocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 16 additions & 3 deletions portal/patient/templates/OnsiteDocumentListView.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@
echo "<script>var formNamesWhitelist=" . json_encode(CoreFormToPortalUtility::getListPortalCompliantEncounterForms()) . ";</script>";

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']);
}
?>
<link rel="stylesheet" href="<?php echo $GLOBALS['web_root']; ?>/portal/sign/css/signer_modal.css?v=<?php echo $GLOBALS['v_js_includes']; ?>">
Expand All @@ -111,7 +111,20 @@
$LAB.script("<?php echo $GLOBALS['assets_static_relative']; ?>/underscore/underscore-min.js").script("<?php echo $GLOBALS['assets_static_relative']; ?>/moment/moment.js").script(
"<?php echo $GLOBALS['assets_static_relative']; ?>/backbone/backbone-min.js").script("<?php echo $GLOBALS['web_root']; ?>/portal/patient/scripts/app.js?v=<?php echo $GLOBALS['v_js_includes']; ?>").script(
"<?php echo $GLOBALS['web_root']; ?>/portal/patient/scripts/model.js?v=<?php echo $GLOBALS['v_js_includes']; ?>").wait().script(
"<?php echo $GLOBALS['web_root']; ?>/portal/patient/scripts/view.js?v=<?php echo $GLOBALS['v_js_includes']; ?>").wait()
"<?php echo $GLOBALS['web_root']; ?>/portal/patient/scripts/view.js?v=<?php echo $GLOBALS['v_js_includes']; ?>").wait();
i18next.init({
lng: 'selected',
debug: false,
nsSeparator: false,
keySeparator: false,
resources: {
selected: {
translation: <?php echo js_escape($this->language_defs ?? []); ?>
}
}
}).catch(error => {
console.log(error.message);
});
</script>
<style>
@media print {
Expand Down
37 changes: 37 additions & 0 deletions src/Services/Utils/TranslationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Helper class for dealing with language translations.
*
* @package OpenEMR
* @link https://www.open-emr.org
* @author Jerry Padgett <sjpadgett@gmail.com>
* @author Stephen Nielson <snielson@discoverandchange.com>
* @copyright Copyright (c) 2016-2024 Jerry Padgett <sjpadgett@gmail.com>
* @copyright Copyright (c) 2024 Open Plan IT Ltd. <support@openplanit.com>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/

namespace OpenEMR\Services\Utils;

class TranslationService
{
public static function getLanguageDefinitionsForSession()
{
$language = $_SESSION['language_choice'] ?? '1'; // defaults english
return self::getLanguageDefinitionsForLanguage($language);
}

public static function getLanguageDefinitionsForLanguage(int $languageId)
{
$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, $languageId);
$language_defs = array();
while ($row = SqlFetchArray($tarns)) {
$language_defs[$row['constant_name']] = $row['definition'];
}
return $language_defs;
}
}

0 comments on commit 4d74dbf

Please sign in to comment.