From 865eea224598b2a1e7c2d18902b5b5410b9c1978 Mon Sep 17 00:00:00 2001 From: Cris Date: Tue, 29 May 2018 14:13:00 +0200 Subject: [PATCH] Added PHRASES translation for javascript added: . PhrasesLib.js: translate method for retreiving phrasentexte in users language with JS . PhrasesLib.php: methods to retrieve data from Phrase_model and return as JSON . Phrase_model: method to perform query for categories AND phrases-array AND language . FHC-Header.php: -- included js lib -- generate global FHC_JS_PHRASES_STORAGE_OBJECT Changes of other files: loading libs, inits,... --- application/core/FHC_Controller.php | 4 +- application/libraries/PhrasesLib.php | 43 +++++-- application/models/system/Phrase_model.php | 34 ++++++ .../views/system/infocenter/infocenter.php | 6 + .../system/infocenter/infocenterDetails.php | 2 + application/views/templates/FHC-Header.php | 29 ++++- public/js/PhrasesLib.js | 115 ++++++++++++++++++ system/phrasesupdate.php | 21 ++++ 8 files changed, 244 insertions(+), 10 deletions(-) create mode 100644 public/js/PhrasesLib.js diff --git a/application/core/FHC_Controller.php b/application/core/FHC_Controller.php index fc200d935..65509b890 100644 --- a/application/core/FHC_Controller.php +++ b/application/core/FHC_Controller.php @@ -25,11 +25,11 @@ class FHC_Controller extends CI_Controller * NOTE: The library is loaded with the alias 'p', so must me used with this alias in the rest of the code. * EX: $this->p->t(, ) */ - public function loadPhrases($categories, $language = null) + protected function loadPhrases($categories, $language = null) { $this->load->library('PhrasesLib', array($categories, $language), 'p'); } - + /** * Sets the unique id for the called controller * NOTE: it is only working with HTTP GET request, not neeaded with POST diff --git a/application/libraries/PhrasesLib.php b/application/libraries/PhrasesLib.php index 1194e769c..25d4d1004 100644 --- a/application/libraries/PhrasesLib.php +++ b/application/libraries/PhrasesLib.php @@ -259,7 +259,7 @@ class PhrasesLib * - language: optional parameter must be a string. It's used to load phrases */ private function _extend_construct($params) - { + { // Checks if the $params is an array with at least one element if (is_array($params) && count($params) > 0) { @@ -287,15 +287,44 @@ class PhrasesLib $language = $this->_ci->PersonModel->getLanguage(getAuthUID()); } - // Loads phrases - $phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndLanguage($categories, $language); + //checks if categories is associative or indexed array + if (ctype_digit(implode('', array_keys($categories)))) + { + // Loads phrases + $phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndLanguage($categories, $language); - // If there are phrases loaded then store them in the property _phrases - if (hasData($phrases)) + // If there are phrases loaded then store them in the property _phrases + if (hasData($phrases)) + { + $this->_phrases = $phrases->retval; + } + } + else { - $this->_phrases = $phrases->retval; + // Loads specific phrasentexte by category and phrases + $phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndPhrasesAndLanguage($categories, $language); + + // If there are phrases loaded then store them in the property _phrases + if (hasData($phrases)) + { + $this->_phrases = $phrases->retval; + } } } - } + } } + + public function getJSON() + { + if (is_array($this->_phrases) && !is_null($this->_phrases)) + { + return json_encode($this->_phrases); + } + else + { + //...CECK: better a return than console.log? + console.log('No phrases set to be returned to JS.'); + } + } + } diff --git a/application/models/system/Phrase_model.php b/application/models/system/Phrase_model.php index dff8bebaf..3a364bc2b 100644 --- a/application/models/system/Phrase_model.php +++ b/application/models/system/Phrase_model.php @@ -77,6 +77,40 @@ class Phrase_model extends DB_Model return $this->execQuery($query, array($categories, $language)); } + + /** + * + */ + public function getPhrasesByCategoryAndPhrasesAndLanguage($phrasesParams, $language) + { + $qry = ' + SELECT p.category, p.phrase, pt.orgeinheit_kurzbz, pt.orgform_kurzbz, pt.text + FROM system.tbl_phrase p + INNER JOIN system.tbl_phrasentext pt USING(phrase_id) + WHERE '; + + foreach ($phrasesParams as $category => $phrases) + { + $qry .= ' + (p.category = \'' . $category . '\' + AND phrase IN ('; + foreach ($phrases as $phrase) + { + $qry .= '\'' . $phrase . '\', '; + } + + $qry = rtrim($qry, ', '); + $qry .= ')) AND pt.sprache = \'' . $language . '\' OR '; + } + $qry = rtrim($qry, 'OR '); + + return $this->execQuery($qry, array($phrasesParams, $language)); + + + } + + + /** * Loads all categories diff --git a/application/views/system/infocenter/infocenter.php b/application/views/system/infocenter/infocenter.php index f33abff79..d1bd4ad87 100644 --- a/application/views/system/infocenter/infocenter.php +++ b/application/views/system/infocenter/infocenter.php @@ -5,12 +5,18 @@ 'title' => 'Info Center', 'jquery' => true, 'jqueryui' => true, + 'ajaxlib' => true, 'bootstrap' => true, 'fontawesome' => true, 'sbadmintemplate' => true, 'tablesorter' => true, 'ajaxlib' => true, 'filterwidget' => true, + 'phrases'=> array( + 'person' => array('vorname','nachname'), + 'ui' => array('speichern'), + 'global' => array('mailAnXversandt') + ), 'navigationwidget' => true, 'customCSSs' => 'public/css/sbadmin2/tablesort_bootstrap.css', 'customJSs' => array('public/js/bootstrapper.js', 'public/js/infocenter/infocenterPersonDataset.js') diff --git a/application/views/system/infocenter/infocenterDetails.php b/application/views/system/infocenter/infocenterDetails.php index 700c06569..39da27aad 100755 --- a/application/views/system/infocenter/infocenterDetails.php +++ b/application/views/system/infocenter/infocenterDetails.php @@ -7,6 +7,8 @@ 'bootstrap' => true, 'fontawesome' => true, 'jqueryui' => true, + 'ajaxlib' => true, + 'phraseslib' => true, 'tablesorter' => true, 'tinymce' => true, 'sbadmintemplate' => true, diff --git a/application/views/templates/FHC-Header.php b/application/views/templates/FHC-Header.php index 8b4dc058a..147d84e8f 100755 --- a/application/views/templates/FHC-Header.php +++ b/application/views/templates/FHC-Header.php @@ -11,6 +11,7 @@ $calledMethod = $this->router->method; $title = isset($title) ? $title : null; $customCSSs = isset($customCSSs) ? $customCSSs : null; $customJSs = isset($customJSs) ? $customJSs : null; +$phrases = isset($phrases) ? $phrases : null; // By default set the parameters to false $jquery = isset($jquery) ? $jquery : false; @@ -84,6 +85,26 @@ function _generateJSDataStorageObject($calledPath, $calledMethod) echo $toPrint; } +/** + * Generates global JS-Object to pass phrases to other javascripts + */ +function _generateJSPhrasesStorageObject($phrases) +{ + $ci =& get_instance(); + $ci->load->library('PhrasesLib', array($phrases), 'pj'); + + $toPrint = "\n"; + $toPrint .= ''; + $toPrint .= "\n\n"; + + echo $toPrint; +} + + /** * Generates tags for the javascripts you want to include, the parameter could by a string or an array of strings */ @@ -183,6 +204,9 @@ function _generateAddonsJSsInclude($calledFrom) // Generates the global object to pass useful parameters to other javascripts // NOTE: must be called before any other JS include _generateJSDataStorageObject($calledPath, $calledMethod); + + // Generates the global object to pass phrases to javascripts + _generateJSPhrasesStorageObject($phrases); // JQuery V3 if ($jquery === true) _generateJSsInclude('vendor/components/jquery/jquery.min.js'); @@ -196,6 +220,9 @@ function _generateAddonsJSsInclude($calledFrom) // AjaxLib JS if ($ajaxlib === true) _generateJSsInclude('public/js/AjaxLib.js'); + +// // PhfrasesLib JS + if ($phrases != null) _generateJSsInclude('public/js/PhrasesLib.js'); // Bootstrap JS if ($bootstrap === true) _generateJSsInclude('vendor/twbs/bootstrap/dist/js/bootstrap.min.js'); @@ -230,6 +257,6 @@ function _generateAddonsJSsInclude($calledFrom) // Eventually required JS _generateJSsInclude($customJSs); ?> - + diff --git a/public/js/PhrasesLib.js b/public/js/PhrasesLib.js new file mode 100644 index 000000000..f21026553 --- /dev/null +++ b/public/js/PhrasesLib.js @@ -0,0 +1,115 @@ +/** + * FH-Complete + * + * @package + * @author + * @copyright Copyright (c) 2016 fhcomplete.org + * @license GPLv3 + * @link https://fhcomplete.org + * @since Version 1.0.0 + */ + +/** + * Definition and initialization of object FHC_PhraseLib + */ + +var FHC_PhraseLib = { + + //------------------------------------------------------------------------------------------------------------------ + // Public methods + + /** + Returns the phrase-text in the user's language + * @param {String} category : phrase-category + * @param {String} phrase : phrase-name + * @param {array} params : String-parameters to be set in variables in phrasentext + * @returns {String} : phrase-text + */ + t: function(category, phrase, params = []) + { + var category_found = false; + var phrase_found = false; + + //check category and phrase first + if(typeof(category) == "undefined" || category === null + || typeof(phrase) == "undefined" || phrase === null) + { + console.log('Category and/or phrase not found. \n\ + 1. Check params in PhrasesLib.js t-method\n\ + 2. Check params in FHC-Header.php phrases-array'); + return; + } + + //loop through global JS PHRASES STORAGE OBJECT and search for phrase + for(i in FHC_JS_PHRASES_STORAGE_OBJECT) + { + e = FHC_JS_PHRASES_STORAGE_OBJECT[i]; + if (e.category === category) + { + category_found = true; + if (e.phrase === phrase) + { + phrase_found = true; + + //replace if params are set + if ($.isArray(params) && typeof params !== 'undefined' && params !== null) + { + if (params.length !== 0) + { + e.text = replacePhraseVariable(e.text, params); + } + } + else + { + console.log('Could not replace variable. \n\ + Replace-params should be an array.') + } + + return e.text; + } + } + } + + //show error messages for missing categories/phrases + if (!category_found) + { + console.log('Category not found. \n\ + 1. Check params in PhrasesLib.js t-method\n\ + 2. Check params in FHC-Header.php phrases-array'); + } + + if (category_found && !phrase_found) + { + console.log('Phrase not found. \n\ + 1. Check params in PhrasesLib.js t-method\n\ + 2. Check params in FHC-Header.php phrases-array'); + } + } +} + + +//------------------------------------------------------------------------------------------------------------------ +// Helper methods + +/** + Returns phrase with variables being replaced + * @param {String} phrase : phrasen-text (with one ore more variables) + * @param {array} replaceStringArr : String-array to be set in variables in phrasentext (order matters) + * @returns {String} : replaced phrasen-text + */ +function replacePhraseVariable(phrase, replaceStringArr) +{ + for (var i = 0; i < replaceStringArr.length; i++) + { + phrase = phrase.replace(/\{(.*?)\}/, replaceStringArr[i]); + } + return phrase; +} + + + +//...TEST +console.log(FHC_PhraseLib.t('global', 'mailAnXversandt', new Array('c@yahoo.de', 'test'))); + + + diff --git a/system/phrasesupdate.php b/system/phrasesupdate.php index 585830b9d..37d0e2b9e 100644 --- a/system/phrasesupdate.php +++ b/system/phrasesupdate.php @@ -909,6 +909,27 @@ $phrases = array( ) ), + array( + 'app' => 'core', + 'category' => 'global', + 'phrase' => 'mailAnXversandt', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Mail an {email} versandt.', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'Mail was sent to {email}.', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + //******************************* CORE/ui array(