From 865eea224598b2a1e7c2d18902b5b5410b9c1978 Mon Sep 17 00:00:00 2001 From: Cris Date: Tue, 29 May 2018 14:13:00 +0200 Subject: [PATCH 1/5] 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( From 7c5995c31890a51c73b5eca10d45ef2701368295 Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 29 May 2018 15:57:10 +0200 Subject: [PATCH 2/5] Code and comments improved --- application/libraries/PhrasesLib.php | 131 ++++++------------ application/models/system/Phrase_model.php | 55 +++----- .../views/system/infocenter/infocenter.php | 4 +- application/views/templates/FHC-Header.php | 25 ++-- public/js/PhrasesLib.js | 128 ++++++----------- 5 files changed, 122 insertions(+), 221 deletions(-) diff --git a/application/libraries/PhrasesLib.php b/application/libraries/PhrasesLib.php index 25d4d1004..9329bcfc4 100644 --- a/application/libraries/PhrasesLib.php +++ b/application/libraries/PhrasesLib.php @@ -14,6 +14,8 @@ class PhrasesLib { $this->_ci =& get_instance(); + $this->_phrases = null; // set the property _phrases as null by default + // CI parser $this->_ci->load->library('parser'); @@ -78,7 +80,6 @@ class PhrasesLib return $this->_ci->PhraseModel->update($phrase_id, $data); } - /** * getVorlagetextByVorlage() - will load tbl_vorlagestudiengang for a spezific Template. */ @@ -179,75 +180,37 @@ class PhrasesLib } /** - * + * Retrives a phrases from the the property _phrases with the given parameters + * It also replace parameters inside the phrase if they are provided */ public function t($category, $phrase, $parameters = array(), $orgeinheit_kurzbz = null, $orgform_kurzbz = null) { - if (isset($this->_phrases) && is_array($this->_phrases)) + // If the property _phrases is populated + if (is_array($this->_phrases)) { + // Loops through the _phrases property for ($i = 0; $i < count($this->_phrases); $i++) { - - $_phrase = $this->_phrases[$i]; - + $_phrase = $this->_phrases[$i]; // single phrase + + // If the single phrase match the given parameters and is not an empty string if ($_phrase->category == $category && $_phrase->phrase == $phrase && $_phrase->orgeinheit_kurzbz == $orgeinheit_kurzbz - && $_phrase->orgform_kurzbz== $orgform_kurzbz - && (!empty($_phrase->text))) - { - if ($parameters == null) - $parameters = array(); - - return $this->_ci->parser->parse_string($_phrase->text, $parameters, true); - } - } - - //fallback 1: if phrase not found in phrases-array, try with default language - $default_language = DEFAULT_LANGUAGE; - $categories = $this->_ci->PhraseModel->getCategories(); - - if (hasData($categories)) - { - $categories = $categories->retval; - foreach($categories as $cat) - $all_categories[] = $cat->category; - } - - $phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndLanguage($all_categories, $default_language); - - if (hasData($phrases)) - { - $default_phrases = $phrases->retval; - } - - if (isset($default_phrases) && is_array($default_phrases)) - { - for ($i = 0; $i < count($default_phrases); $i++) + && $_phrase->orgform_kurzbz == $orgform_kurzbz + && (!empty(trim($_phrase->text)))) { - $_phrase = $default_phrases[$i]; -// var_dump($_phrase); - -// echo $phrase . "
"; -// echo $_phrase->phrase . "

"; + if (!is_array($parameters)) $parameters = array(); // if params is not an array - if ($_phrase->category == $category - && $_phrase->phrase == $phrase - && $_phrase->orgeinheit_kurzbz == $orgeinheit_kurzbz - && $_phrase->orgform_kurzbz== $orgform_kurzbz) - { - if ($parameters == null) - $parameters = array(); - return $this->_ci->parser->parse_string($_phrase->text, $parameters, true); - } + return $this->_ci->parser->parse_string($_phrase->text, $parameters, true); // parsing } } - - //fallback 2: if phrase not found at all, return phrasename - $phrase = '<< PHRASE ' . $phrase . ' >>'; - return $this->_ci->parser->parse_string($phrase, $parameters, true); - } + } + + // If a valid phrase is not found + return '<< PHRASE '.$phrase.' >>'; } + // ----------------------------------------------------------------------------------------------------------------- // Private methods @@ -255,11 +218,13 @@ class PhrasesLib * Extends the functionalities of the constructor of this class * This is a workaround to use more parameters in the construct since PHP doesn't support many constructors * The new accepted parameters are: - * - categories: could be a string or an array of strings. These are the categories used to load phrases + * - categories: + * - could be a string or an array of strings. These are the categories used to load phrases + * - could be an array of categories, and for each category there is an array of phrases * - 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,44 +252,32 @@ class PhrasesLib $language = $this->_ci->PersonModel->getLanguage(getAuthUID()); } - //checks if categories is associative or indexed array + // 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)) - { - $this->_phrases = $phrases->retval; - } - } - else { - // Loads specific phrasentexte by category and phrases + // Loads phrases + $phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndLanguage($categories, $language); + } + else + { + // 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; - } + // 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.'); - } } + /** + * Returns the property _phrases JSON encoded + */ + public function getJSON() + { + return json_encode($this->_phrases); + } } diff --git a/application/models/system/Phrase_model.php b/application/models/system/Phrase_model.php index 3a364bc2b..da9da1a7e 100644 --- a/application/models/system/Phrase_model.php +++ b/application/models/system/Phrase_model.php @@ -77,49 +77,38 @@ class Phrase_model extends DB_Model return $this->execQuery($query, array($categories, $language)); } - + /** - * + * Loads phrases using category(s) and language as keys using associative category array + * that contains also phrases for each category + * They are ordered by p.category, p.phrase, pt.orgeinheit_kurzbz DESC and pt.orgform_kurzbz DESC' */ public function getPhrasesByCategoryAndPhrasesAndLanguage($phrasesParams, $language) - { - $qry = ' + { + $query = ' 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 '; - + FROM system.tbl_phrase p + INNER JOIN system.tbl_phrasentext pt USING(phrase_id) + WHERE '; + foreach ($phrasesParams as $category => $phrases) { - $qry .= ' + $query .= ' (p.category = \'' . $category . '\' - AND phrase IN ('; + AND phrase IN ('; foreach ($phrases as $phrase) { - $qry .= '\'' . $phrase . '\', '; + $query .= '\'' . $phrase . '\', '; } - - $qry = rtrim($qry, ', '); - $qry .= ')) AND pt.sprache = \'' . $language . '\' OR '; + + $query = rtrim($query, ', '); + $query .= ')) AND pt.sprache = \'' . $language . '\' OR '; } - $qry = rtrim($qry, 'OR '); - - return $this->execQuery($qry, array($phrasesParams, $language)); - + $query = rtrim($query, 'OR '); + + $query .= ' ORDER BY p.category, p.phrase, pt.orgeinheit_kurzbz DESC, pt.orgform_kurzbz DESC'; + + return $this->execQuery($query, array($phrasesParams, $language)); } - - - - - /** - * Loads all categories - */ - public function getCategories() - { - $query = 'SELECT DISTINCT category - FROM system.tbl_phrase'; - - return $this->execQuery($query); - } -} \ No newline at end of file +} diff --git a/application/views/system/infocenter/infocenter.php b/application/views/system/infocenter/infocenter.php index d1bd4ad87..b747d9810 100644 --- a/application/views/system/infocenter/infocenter.php +++ b/application/views/system/infocenter/infocenter.php @@ -13,10 +13,10 @@ 'ajaxlib' => true, 'filterwidget' => true, 'phrases'=> array( - 'person' => array('vorname','nachname'), + '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/templates/FHC-Header.php b/application/views/templates/FHC-Header.php index 147d84e8f..fcfc4cdf6 100755 --- a/application/views/templates/FHC-Header.php +++ b/application/views/templates/FHC-Header.php @@ -92,11 +92,11 @@ function _generateJSPhrasesStorageObject($phrases) { $ci =& get_instance(); $ci->load->library('PhrasesLib', array($phrases), 'pj'); - + $toPrint = "\n"; $toPrint .= ''; $toPrint .= "\n\n"; @@ -104,7 +104,6 @@ function _generateJSPhrasesStorageObject($phrases) echo $toPrint; } - /** * Generates tags for the javascripts you want to include, the parameter could by a string or an array of strings */ @@ -204,8 +203,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 + // NOTE: must be called before including the PhrasesLib.js _generateJSPhrasesStorageObject($phrases); // JQuery V3 @@ -218,12 +218,6 @@ function _generateAddonsJSsInclude($calledFrom) _generateJSsInclude('vendor/components/jqueryui/ui/i18n/datepicker-de.js'); // datepicker german language file } - // 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'); @@ -245,6 +239,13 @@ function _generateAddonsJSsInclude($calledFrom) _generateJSsInclude('vendor/BlackrockDigital/startbootstrap-sb-admin-2/dist/js/sb-admin-2.min.js'); } + // AjaxLib JS + // NOTE: must be called before including others JS libraries that use it + if ($ajaxlib === true) _generateJSsInclude('public/js/AjaxLib.js'); + + // PhrasesLib JS + if ($phrases != null) _generateJSsInclude('public/js/PhrasesLib.js'); + // FilterWidget JS if($filterwidget === true) _generateJSsInclude('public/js/FilterWidget.js') ; @@ -257,6 +258,6 @@ function _generateAddonsJSsInclude($calledFrom) // Eventually required JS _generateJSsInclude($customJSs); ?> - + diff --git a/public/js/PhrasesLib.js b/public/js/PhrasesLib.js index f21026553..a676bb7c8 100644 --- a/public/js/PhrasesLib.js +++ b/public/js/PhrasesLib.js @@ -12,104 +12,62 @@ /** * 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) + * 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 = []) { + + // Checks if FHC_JS_PHRASES_STORAGE_OBJECT is an array + if ($.isArray(FHC_JS_PHRASES_STORAGE_OBJECT)) { - 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) + // loop through global JS PHRASES STORAGE OBJECT and search for phrase + for (var i in FHC_JS_PHRASES_STORAGE_OBJECT) { - category_found = true; - if (e.phrase === phrase) + var phraseObj = FHC_JS_PHRASES_STORAGE_OBJECT[i]; // Single phrase object + + // If the single phrase match the given parameters and is not an empty string + if (phraseObj.category == category + && phraseObj.phrase == phrase + && phraseObj.text != null + && phraseObj.text.trim() != '') { - phrase_found = true; - - //replace if params are set - if ($.isArray(params) && typeof params !== 'undefined' && params !== null) + // If params is null or not an array + if (params == null || (params != null && $.isArray(params))) { - if (params.length !== 0) - { - e.text = replacePhraseVariable(e.text, params); - } + params = []; } - else - { - console.log('Could not replace variable. \n\ - Replace-params should be an array.') - } - - return e.text; + + return = FHC_PhraseLib._replacePhraseVariable(phraseObj.text, params); // parsing } } } - - //show error messages for missing categories/phrases - if (!category_found) + + return '<< PHRASE ' + phraseObj.phrase + ' >>'; + }, + + //------------------------------------------------------------------------------------------------------------------ + // Private 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 + */ + _replacePhraseVariable: function(phrase, replaceStringArr) { + for (var i = 0; i < replaceStringArr.length; i++) { - console.log('Category not found. \n\ - 1. Check params in PhrasesLib.js t-method\n\ - 2. Check params in FHC-Header.php phrases-array'); + phrase = phrase.replace(/\{(.*?)\}/, replaceStringArr[i]); } - - 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; } - return phrase; -} - - - -//...TEST -console.log(FHC_PhraseLib.t('global', 'mailAnXversandt', new Array('c@yahoo.de', 'test'))); - - - +}; From efebfddd580d1b457504f1462e0cbc064844c98e Mon Sep 17 00:00:00 2001 From: Cris Date: Wed, 6 Jun 2018 10:00:17 +0200 Subject: [PATCH 3/5] Added fallback to default language for phrases . added fallback to default language in PhrasesLib.php . fixed small issues concerning right translation in PhrasesLib.php . all other files: adding translation for Infocenter overview + details page (client- and serverside) --- application/libraries/PhrasesLib.php | 83 +++- .../views/system/infocenter/infocenter.php | 15 +- .../system/infocenter/infocenterDetails.php | 52 ++- .../system/infocenter/studiengangZgvInfo.php | 6 +- .../views/system/infocenter/zgvpruefungen.php | 14 +- application/views/templates/FHC-Header.php | 2 +- .../views/widgets/filter/saveFilter.php | 4 +- .../views/widgets/filter/selectFields.php | 2 +- .../views/widgets/filter/selectFilters.php | 4 +- public/js/FilterWidget.js | 4 +- public/js/PhrasesLib.js | 14 +- public/js/infocenter/infocenterDetails.js | 22 +- public/js/tablesort/tablesort.js | 2 +- system/phrasesupdate.php | 363 ++++++++++++++++-- 14 files changed, 501 insertions(+), 86 deletions(-) diff --git a/application/libraries/PhrasesLib.php b/application/libraries/PhrasesLib.php index 9329bcfc4..be106e6af 100644 --- a/application/libraries/PhrasesLib.php +++ b/application/libraries/PhrasesLib.php @@ -182,6 +182,10 @@ class PhrasesLib /** * Retrives a phrases from the the property _phrases with the given parameters * It also replace parameters inside the phrase if they are provided + * @param string $category Category name which is used to categorize the phrase. + * @param string $phrase Phrase name. + * @param array $parameters Array of String var(s) to be set into phrases' placeholder values (order matters). + * @return string Phrase text */ public function t($category, $phrase, $parameters = array(), $orgeinheit_kurzbz = null, $orgform_kurzbz = null) { @@ -217,18 +221,21 @@ class PhrasesLib /** * Extends the functionalities of the constructor of this class * This is a workaround to use more parameters in the construct since PHP doesn't support many constructors - * The new accepted parameters are: - * - categories: + * @param (array) $params Array of categories and (optional) language. + * categories: * - could be a string or an array of strings. These are the categories used to load phrases * - could be an array of categories, and for each category there is an array of phrases - * - language: optional parameter must be a string. It's used to load phrases + * language: optional parameter must be a string. It's used to load phrases + * stores phrases-array in property $_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) { - $parameters = $params[0]; // temporary variable + $parameters = $params[0]; // temporary variable + $indexArray = false; //flag for indexed array + $assocArray = false; //flag for associative array // If there are parameters if (is_array($parameters) && count($parameters) > 0) @@ -252,16 +259,75 @@ class PhrasesLib $language = $this->_ci->PersonModel->getLanguage(getAuthUID()); } - // checks if categories is associative or indexed array + // Checks if categories is associative or indexed array if (ctype_digit(implode('', array_keys($categories)))) { - // Loads phrases + // is indexed array -> Loads phrases + $indexArray = true; $phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndLanguage($categories, $language); } else { - // Loads specific phrasentexte by category and phrases - $phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndPhrasesAndLanguage($categories, $language); + // is assoc array -> Loads specific phrasentexte by category and phrases + $assocArray = true; + $phrases = $this->_ci->PhraseModel + ->getPhrasesByCategoryAndPhrasesAndLanguage($categories, $language); + } + + // If language is not default language and phrasentext is null -> fallback to default language + if ($language != DEFAULT_LANGUAGE) + { + // get array with phrasentexte in the default language + $defaultPhrases = array(); + if ($indexArray) + { + $defaultPhrases = $this->_ci->PhraseModel + ->getPhrasesByCategoryAndLanguage($categories, DEFAULT_LANGUAGE); + } + elseif ($assocArray) + { + $defaultPhrases = $this->_ci->PhraseModel + ->getPhrasesByCategoryAndPhrasesAndLanguage($categories, DEFAULT_LANGUAGE); + } + + // combine array with phrasentexte in users language and in default language + // (default used if phrasentext in users language is null or not set) + $phrasesArr = array(); + if (!empty($phrases->retval)) + { + // loop through phrases in default language + foreach ($defaultPhrases->retval as $defaultPhrase) + { + $phraseObj = new stdClass(); + $found = false; // flag for found phrase + + // loop through phrases in users language + foreach ($phrases->retval as $phrase) + { + // if same phrase and category found and text is not null + // use phrase in users language + if ($phrase->phrase === $defaultPhrase->phrase + && $phrase->category === $defaultPhrase->category + && !is_null($phrase->text)) + { + $found = true; + array_push($phrasesArr, $phrase); + break; + } + } + + // otherwise use phrase in default language + if (!$found) + { + array_push($phrasesArr, $defaultPhrase); + } + } + $phrases->retval = $phrasesArr; + } + else + { + $phrases->retval = $defaultPhrases->retval; + } } // If there are phrases loaded then store them in the property _phrases @@ -275,6 +341,7 @@ class PhrasesLib /** * Returns the property _phrases JSON encoded + * @return json encoded property _phrases */ public function getJSON() { diff --git a/application/views/system/infocenter/infocenter.php b/application/views/system/infocenter/infocenter.php index b747d9810..41431922e 100644 --- a/application/views/system/infocenter/infocenter.php +++ b/application/views/system/infocenter/infocenter.php @@ -12,10 +12,10 @@ 'tablesorter' => true, 'ajaxlib' => true, 'filterwidget' => true, - 'phrases'=> array( + 'phrases' => array( 'person' => array('vorname', 'nachname'), - 'ui' => array('speichern'), - 'global' => array('mailAnXversandt') + 'global' => array('mailAnXversandt'), + 'ui' => array('bitteEintragWaehlen') ), 'navigationwidget' => true, 'customCSSs' => 'public/css/sbadmin2/tablesort_bootstrap.css', @@ -33,12 +33,17 @@
- +
load->view('system/infocenter/infocenterData.php', array('fhc_controller_id' => $fhc_controller_id)); + $this->load->view( + 'system/infocenter/infocenterData.php', + array('fhc_controller_id' => $fhc_controller_id) + ); ?>
diff --git a/application/views/system/infocenter/infocenterDetails.php b/application/views/system/infocenter/infocenterDetails.php index 39da27aad..069cd99c7 100755 --- a/application/views/system/infocenter/infocenterDetails.php +++ b/application/views/system/infocenter/infocenterDetails.php @@ -24,8 +24,33 @@ array( 'public/js/bootstrapper.js', 'public/js/tablesort/tablesort.js', - 'public/js/infocenter/infocenterDetails.js') + 'public/js/infocenter/infocenterDetails.js' + ), + 'phrases' => + array( + 'infocenter' => + array( + 'notizHinzufuegen', + 'notizAendern', + 'bewerberParken', + 'bewerberAusparken', + 'nichtsZumAusparken', + 'fehlerBeimAusparken', + 'fehlerBeimParken', + 'bewerberGeparktBis' + ), + 'ui' => + array( + 'gespeichert', + 'fehlerBeimSpeichern' + ), + 'global' => + array( + 'bis', + 'zeilen' + ) ) + ) ); ?> @@ -44,14 +69,17 @@
- p->t('global', 'wirdBearbeitetVon') . ':' ?> + p->t('global', 'wirdBearbeitetVon').':' ?>    -  p->t('ui', 'freigeben')) ?> + +   + p->t('ui', 'freigeben')) ?> +
@@ -61,7 +89,9 @@
-

p->t('global','stammdaten')) ?>

+
+

p->t('global', 'stammdaten')) ?>

+
load->view('system/infocenter/stammdaten.php'); ?> load->view('system/infocenter/anmerkungenZurBewerbung.php'); ?> @@ -75,7 +105,9 @@
-

p->t('infocenter','dokumentenpruefung')) ?>

+
+

p->t('infocenter', 'dokumentenpruefung')) ?>

+
load->view('system/infocenter/dokpruefung.php'); ?>
@@ -89,7 +121,8 @@
-

p->t('infocenter', 'zgv') . ' - ' . ucfirst($this->p->t('lehre','pruefung'))?>

+

p->t('infocenter', 'zgv'). ' - '. + ucfirst($this->p->t('lehre', 'pruefung'))?>

load->view('system/infocenter/zgvpruefungen.php'); ?> @@ -104,7 +137,7 @@
-

p->t('global','nachrichten')) ?>

+

p->t('global', 'nachrichten')) ?>

@@ -123,7 +156,8 @@
-

p->t('global','notizen')) . ' & ' . ucfirst($this->p->t('global','aktivitaeten')) ?>

+

p->t('global', 'notizen')). ' & '. + ucfirst($this->p->t('global', 'aktivitaeten')) ?>

diff --git a/application/views/system/infocenter/studiengangZgvInfo.php b/application/views/system/infocenter/studiengangZgvInfo.php index 6242d0db6..813a87889 100644 --- a/application/views/system/infocenter/studiengangZgvInfo.php +++ b/application/views/system/infocenter/studiengangZgvInfo.php @@ -17,13 +17,13 @@ $this->load->view(
- +
- Keine Zugangsvoraussetzungen für den Studiengang definiert - p->t('infocenter', 'keineZugangsvoraussetzungenTxt'); ?> +
-
+
zgvmanation_bez; @@ -220,7 +220,7 @@
@@ -248,7 +248,7 @@ class="d-inline float-right" required> @@ -322,7 +322,7 @@ data-toggle="modal" data-target="#freigabeModal_prestudent_id ?>" data-toggle="tooltip" title=""> - p->t('ui', 'freigabeAnStudiengang') ?> + p->t('ui', 'freigabeAnStudiengang') ?>
@@ -339,10 +339,10 @@ + p->t('infocenter', 'freigabeBestaetigen') ?>
diff --git a/application/views/templates/FHC-Header.php b/application/views/templates/FHC-Header.php index fcfc4cdf6..33326cb56 100755 --- a/application/views/templates/FHC-Header.php +++ b/application/views/templates/FHC-Header.php @@ -95,7 +95,7 @@ function _generateJSPhrasesStorageObject($phrases) $toPrint = "\n"; $toPrint .= ''; diff --git a/application/views/widgets/filter/saveFilter.php b/application/views/widgets/filter/saveFilter.php index c1f09a413..b338d3f3b 100644 --- a/application/views/widgets/filter/saveFilter.php +++ b/application/views/widgets/filter/saveFilter.php @@ -2,14 +2,14 @@
- Filter description: + p->t('global', 'beschreibung')); ?>: - +
diff --git a/application/views/widgets/filter/selectFields.php b/application/views/widgets/filter/selectFields.php index 73461840a..e511936af 100644 --- a/application/views/widgets/filter/selectFields.php +++ b/application/views/widgets/filter/selectFields.php @@ -2,7 +2,7 @@
- Add field: + p->t('filter', 'feldHinzufuegen')); ?>: diff --git a/application/views/widgets/filter/selectFilters.php b/application/views/widgets/filter/selectFilters.php index ffb93dfe5..babf34423 100644 --- a/application/views/widgets/filter/selectFilters.php +++ b/application/views/widgets/filter/selectFilters.php @@ -4,7 +4,7 @@
- Add filter: + p->t('filter', 'filterHinzufuegen')); ?>: @@ -12,6 +12,6 @@ - +
diff --git a/public/js/FilterWidget.js b/public/js/FilterWidget.js index 004f4ddac..60ca22f2f 100644 --- a/public/js/FilterWidget.js +++ b/public/js/FilterWidget.js @@ -78,7 +78,7 @@ var FHC_FilterWidget = { $("#filterSelectFieldsDnd").append(strHtml); } - var strDropDown = ''; + var strDropDown = ''; $("#addField").append(strDropDown); if (data.allSelectedFields != null) @@ -126,7 +126,7 @@ var FHC_FilterWidget = { if (data != null) { - var strDropDown = ''; + var strDropDown = ''; $("#addFilter").append(strDropDown); if (data.selectedFilters != null) diff --git a/public/js/PhrasesLib.js b/public/js/PhrasesLib.js index a676bb7c8..4818c7de4 100644 --- a/public/js/PhrasesLib.js +++ b/public/js/PhrasesLib.js @@ -24,13 +24,13 @@ var FHC_PhraseLib = { * @param {array} params : String-parameters to be set in variables in phrasentext * @returns {String} : phrase-text */ - t: function(category, phrase, params = []) { + t: function (category, phrase, params = []) { // Checks if FHC_JS_PHRASES_STORAGE_OBJECT is an array if ($.isArray(FHC_JS_PHRASES_STORAGE_OBJECT)) { // loop through global JS PHRASES STORAGE OBJECT and search for phrase - for (var i in FHC_JS_PHRASES_STORAGE_OBJECT) + for (i in FHC_JS_PHRASES_STORAGE_OBJECT) { var phraseObj = FHC_JS_PHRASES_STORAGE_OBJECT[i]; // Single phrase object @@ -41,17 +41,17 @@ var FHC_PhraseLib = { && phraseObj.text.trim() != '') { // If params is null or not an array - if (params == null || (params != null && $.isArray(params))) + if (params == null || (params != null && !$.isArray(params))) { params = []; } - - return = FHC_PhraseLib._replacePhraseVariable(phraseObj.text, params); // parsing + + return FHC_PhraseLib._replacePhraseVariable(phraseObj.text, params); // parsing } } } - return '<< PHRASE ' + phraseObj.phrase + ' >>'; + return '<< PHRASE ' + phrase + ' >>'; }, //------------------------------------------------------------------------------------------------------------------ @@ -63,7 +63,7 @@ var FHC_PhraseLib = { * @param {array} replaceStringArr : String-array to be set in variables in phrasentext (order matters) * @returns {String} : replaced phrasen-text */ - _replacePhraseVariable: function(phrase, replaceStringArr) { + _replacePhraseVariable: function (phrase, replaceStringArr) { for (var i = 0; i < replaceStringArr.length; i++) { phrase = phrase.replace(/\{(.*?)\}/, replaceStringArr[i]); diff --git a/public/js/infocenter/infocenterDetails.js b/public/js/infocenter/infocenterDetails.js index 60b461bc1..93cf49b07 100644 --- a/public/js/infocenter/infocenterDetails.js +++ b/public/js/infocenter/infocenterDetails.js @@ -133,7 +133,7 @@ $(document).ready( var notizTitle = $(this).find("td:eq(1)").text(); var notizContent = this.title; - $("#notizform label:first").text("Notiz ändern").css("color", "red"); + $("#notizform label:first").text(FHC_PhraseLib.t('infocenter', 'notizAendern')).css("color", "red"); $("#notizform :input[type='reset']").css("display", "inline-block"); $("#notizform :input[name='hiddenNotizId']").val(notizId); @@ -243,7 +243,7 @@ var InfocenterDetails = { saveZgv: function (data) { var zgvError = function(){ - $("#zgvSpeichern_" + prestudentid).before("Fehler beim Speichern der ZGV!  "); + $("#zgvSpeichern_" + prestudentid).before("" + FHC_PhraseLib.t('ui', 'fehlerBeimSpeichern') + "  "); }; var prestudentid = data.prestudentid; @@ -258,7 +258,7 @@ var InfocenterDetails = { if (FHC_AjaxClient.hasData(data)) { InfocenterDetails._refreshLog(); - $("#zgvSpeichern_" + prestudentid).before("ZGV erfolgreich gespeichert!  "); + $("#zgvSpeichern_" + prestudentid).before("" + FHC_PhraseLib.t('ui', 'gespeichert') + "  "); } else { @@ -392,11 +392,11 @@ var InfocenterDetails = { if (data.length > 0) InfocenterDetails.getParkedDate(personid); else - $("#unparkmsg").removeClass().addClass("text-warning").text(" Nichts zum Ausparken."); + $("#unparkmsg").removeClass().addClass("text-warning").text(FHC_PhraseLib.t('infocenter', 'nichtsZumAusparken')); } }, errorCallback: function(){ - $("#unparkmsg").removeClass().addClass("text-danger").text(" Fehler beim Ausparken!"); + $("#unparkmsg").removeClass().addClass("text-danger").text(FHC_PhraseLib.t('infocenter', 'fehlerBeimAusparken')); }, veilTimeout: 0 } @@ -440,8 +440,8 @@ var InfocenterDetails = { { $("#parking").html( '
'+ - '     '+ - 'bis  '+ + '     '+ + FHC_PhraseLib.t('global', 'bis') + '  '+ ' '+ ''+ '
'); @@ -467,8 +467,8 @@ var InfocenterDetails = { var parkdate = $.datepicker.parseDate("yy-mm-dd", date); var gerparkdate = $.datepicker.formatDate("dd.mm.yy", parkdate); $("#parking").html( - 'BewerberIn geparkt bis '+gerparkdate+'     '+ - ' '+ + FHC_PhraseLib.t('infocenter', 'bewerberGeparktBis')+'  '+gerparkdate+'     '+ + ' '+ '' ); @@ -491,11 +491,11 @@ var InfocenterDetails = { { $("#notizmsg").empty(); $("#notizform :input[name='hiddenNotizId']").val(""); - $("#notizform label:first").text("Notiz hinzufügen").css("color", "black"); + $("#notizform label:first").text(FHC_PhraseLib.t('infocenter', 'notizHinzufuegen')).css("color", "black"); $("#notizform :input[type='reset']").css("display", "none"); }, _errorSaveNotiz: function() { - $("#notizmsg").text("Fehler beim Speichern der Notiz! "); + $("#notizmsg").text(FHC_PhraseLib.t('ui', 'fehlerBeimSpeichern')); } }; diff --git a/public/js/tablesort/tablesort.js b/public/js/tablesort/tablesort.js index f45b55ca7..1f901e74c 100644 --- a/public/js/tablesort/tablesort.js +++ b/public/js/tablesort/tablesort.js @@ -61,7 +61,7 @@ var Tablesort = { size: size, cssDisabled: 'disabled', savePages: false, - output: '{startRow} – {endRow} / {totalRows} Zeilen' + output: '{startRow} – {endRow} / {totalRows} ' + FHC_PhraseLib.t('global', 'zeilen') } ); } diff --git a/system/phrasesupdate.php b/system/phrasesupdate.php index 37d0e2b9e..ab29ee6e2 100644 --- a/system/phrasesupdate.php +++ b/system/phrasesupdate.php @@ -26,7 +26,7 @@ $new = false; -$phrases = array( +$phrases = array( //******************* CORE/global array( 'app' => 'core', @@ -48,6 +48,26 @@ $phrases = array( ) ) ), + array( + 'app' => 'core', + 'category' => 'global', + 'phrase' => 'zeilen', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Zeilen', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'lines', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), array( 'app' => 'core', 'category' => 'global', @@ -930,6 +950,27 @@ $phrases = array( ) ), + array( + 'app' => 'core', + 'category' => 'global', + 'phrase' => 'beschreibung', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Beschreibung', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'description', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + //******************************* CORE/ui array( @@ -1252,6 +1293,46 @@ $phrases = array( ) ) ), + array( + 'app' => 'core', + 'category' => 'ui', + 'phrase' => 'fehlerBeimSpeichern', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Fehler beim Speichern', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'Error on Saving', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + array( + 'app' => 'core', + 'category' => 'ui', + 'phrase' => 'gespeichert', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Gespeichert', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'Saved', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), //*************************** CORE/filter @@ -1276,6 +1357,70 @@ $phrases = array( ) ), + array( + 'app' => 'core', + 'category' => 'filter', + 'phrase' => 'filterHinzufuegen', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Filter hinzufügen', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'add filter', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + + array( + 'app' => 'core', + 'category' => 'filter', + 'phrase' => 'feldHinzufuegen', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Feld hinzufügen', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'add field', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + + array( + 'app' => 'core', + 'category' => 'filter', + 'phrase' => 'filterBeschreibung', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Filter Beschreibung', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'filter description', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + + //**************************** CORE/person @@ -2205,6 +2350,46 @@ $phrases = array( ) ) ), + array( + 'app' => 'infocenter', + 'category' => 'infocenter', + 'phrase' => 'zugangsvoraussetzungen', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Zugangsvoraussetzungen', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => '', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + array( + 'app' => 'infocenter', + 'category' => 'infocenter', + 'phrase' => 'keineZugangsvoraussetzungenTxt', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Keine Zugangsvoraussetzungen für den Studiengang definiert', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => '', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), array( 'app' => 'infocenter', 'category' => 'infocenter', @@ -2294,8 +2479,8 @@ $phrases = array( array( 'sprache' => 'German', 'text' => 'Bei Absage von InteressentInnen erhalten diese den Status "Abgewiesener" und deren ' - . 'ZGV-Daten können im Infocenter nicht mehr bearbeitet oder freigegeben werden. ' - . 'Alle nicht gespeicherten ZGV-Daten gehen verloren. Fortfahren?', + .'ZGV-Daten können im Infocenter nicht mehr bearbeitet oder freigegeben werden. ' + .'Alle nicht gespeicherten ZGV-Daten gehen verloren. Fortfahren?', 'description' => '', 'insertvon' => 'system' ), @@ -2401,7 +2586,7 @@ $phrases = array( ), array( 'sprache' => 'English', - 'text' => '', + 'text' => 'Approve applicant', 'description' => '', 'insertvon' => 'system' ) @@ -2504,7 +2689,7 @@ $phrases = array( ), array( 'sprache' => 'English', - 'text' => 'change note', + 'text' => 'Change note', 'description' => '', 'insertvon' => 'system' ) @@ -2589,7 +2774,128 @@ $phrases = array( 'insertvon' => 'system' ) ) + ), + array( + 'app' => 'infocenter', + 'category' => 'infocenter', + 'phrase' => 'bewerberParken', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'BewerberIn parken', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'Park applicant', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + array( + 'app' => 'infocenter', + 'category' => 'infocenter', + 'phrase' => 'bewerberAusparken', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'BewerberIn ausparken', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'Unpark applicant', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + array( + 'app' => 'infocenter', + 'category' => 'infocenter', + 'phrase' => 'nichtsZumAusparken', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Nichts zum ausparken', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => '', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + array( + 'app' => 'infocenter', + 'category' => 'infocenter', + 'phrase' => 'fehlerBeimAusparken', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Fehler beim Ausparken', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => '', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + array( + 'app' => 'infocenter', + 'category' => 'infocenter', + 'phrase' => 'fehlerBeimParken', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Fehler beim Parken', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => '', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + array( + 'app' => 'infocenter', + 'category' => 'infocenter', + 'phrase' => 'bewerberGeparktBis', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'BewerberIn geparkt bis', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'Applicant parked until', + 'description' => '', + 'insertvon' => 'system' + ) + ) ) + ); @@ -2602,9 +2908,9 @@ foreach ($phrases as $phrase) $qry = "SELECT phrase_id FROM system.tbl_phrase WHERE - app=" . $db->db_add_param($phrase['app']) . " AND - category=" . $db->db_add_param($phrase['category']) . " AND - phrase=" . $db->db_add_param($phrase['phrase']); + app=". $db->db_add_param($phrase['app']). " AND + category=". $db->db_add_param($phrase['category']). " AND + phrase=". $db->db_add_param($phrase['phrase']); //*** CHECK PHRASE if ($result = $db->db_query($qry)) @@ -2621,11 +2927,11 @@ foreach ($phrases as $phrase) insertvon, category) VALUES(". - $db->db_add_param($phrase['app']) . ','. - $db->db_add_param($phrase['phrase']) . ','. + $db->db_add_param($phrase['app']). ','. + $db->db_add_param($phrase['phrase']). ','. ' now(),'. - $db->db_add_param($phrase['insertvon']) . ','. - $db->db_add_param($phrase['category']) . ');'; + $db->db_add_param($phrase['insertvon']). ','. + $db->db_add_param($phrase['category']). ');'; if ($db->db_query($qry_insert)) { @@ -2639,11 +2945,11 @@ foreach ($phrases as $phrase) $phrase_id = $obj->id; } } - echo 'Kategorie/Phrase: ' . $phrase['category'] . '/' . $phrase['phrase'] . ' hinzugefügt
'; + echo 'Kategorie/Phrase: '. $phrase['category']. '/'. $phrase['phrase']. ' hinzugefügt
'; } else - echo 'Fehler: ' . $phrase['category'] . '/' . $phrase['phrase'] . ' hinzufügen nicht möglich
'; - + echo 'Fehler: '. $phrase['category']. '/'. + $phrase['phrase']. ' hinzufügen nicht möglich
'; } //phrase existing -> get phrase_id else @@ -2652,23 +2958,23 @@ foreach ($phrases as $phrase) { $phrase_id = $obj->phrase_id; } - echo 'Kategorie/Phrase: ' . $phrase['category'] . '/' . $phrase['phrase'] . ' vorhanden.
'; + echo 'Kategorie/Phrase: '. $phrase['category']. '/'. $phrase['phrase']. ' vorhanden.
'; } //*** CHECK PHRASENTEXT //loop through languages foreach ($phrase['phrases'] as $phrase_phrases) - { + { $language = $phrase_phrases['sprache']; //query phrasentext in certain language - $qry_language = + $qry_language = "SELECT * FROM system.tbl_phrasentext WHERE - phrase_id=" . $phrase_id . " AND - sprache='" . $language . "'"; + phrase_id=". $phrase_id. " AND + sprache='". $language. "'"; if ($result_language = $db->db_query($qry_language)) @@ -2686,22 +2992,25 @@ foreach ($phrases as $phrase) insertamum, insertvon) VALUES(". - $db->db_add_param($phrase_id, FHC_INTEGER) . ','. - $db->db_add_param($phrase_phrases['sprache']) . ','. + $db->db_add_param($phrase_id, FHC_INTEGER). ','. + $db->db_add_param($phrase_phrases['sprache']). ','. ' NULL,'. ' NULL,'. - $db->db_add_param($phrase_phrases['text']) . ','. - $db->db_add_param($phrase_phrases['description']) . ','. + $db->db_add_param($phrase_phrases['text']). ','. + $db->db_add_param($phrase_phrases['description']). ','. ' now(),'. - $db->db_add_param($phrase_phrases['insertvon']) . ');'; + $db->db_add_param($phrase_phrases['insertvon']). ');'; if ($db->db_query($qry_insert)) { - echo '-- Phrasentext ' . strtoupper(substr($phrase_phrases['sprache'], 0, 3)) . ': ' . $phrase_phrases['text'] . ' hinzugefügt
'; + echo '-- Phrasentext '. strtoupper(substr($phrase_phrases['sprache'], 0, 3)). ': '. + $phrase_phrases['text']. ' hinzugefügt
'; } else { - echo 'Fehler: Phrasentext ' . strtoupper(substr($phrase_phrases['sprache'], 0, 3)) . ': '. $phrase_phrases['text'] . ' hinzufügen nicht möglich
'; + echo 'Fehler: Phrasentext '. + strtoupper(substr($phrase_phrases['sprache'], 0, 3)). ': '. $phrase_phrases['text']. + ' hinzufügen nicht möglich
'; } } } From 894dc51e13e867ee0f5c08bf4fb732237e57e735 Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 6 Jun 2018 14:53:24 +0200 Subject: [PATCH 4/5] Enhancements --- application/libraries/PhrasesLib.php | 148 +++++++++++---------- application/models/system/Phrase_model.php | 21 ++- 2 files changed, 84 insertions(+), 85 deletions(-) diff --git a/application/libraries/PhrasesLib.php b/application/libraries/PhrasesLib.php index be106e6af..bfc2f8493 100644 --- a/application/libraries/PhrasesLib.php +++ b/application/libraries/PhrasesLib.php @@ -226,7 +226,6 @@ class PhrasesLib * - could be a string or an array of strings. These are the categories used to load phrases * - could be an array of categories, and for each category there is an array of phrases * language: optional parameter must be a string. It's used to load phrases - * stores phrases-array in property $_phrases */ private function _extend_construct($params) { @@ -234,8 +233,7 @@ class PhrasesLib if (is_array($params) && count($params) > 0) { $parameters = $params[0]; // temporary variable - $indexArray = false; //flag for indexed array - $assocArray = false; //flag for associative array + $isIndexArray = false; //flag for indexed array // If there are parameters if (is_array($parameters) && count($parameters) > 0) @@ -259,83 +257,89 @@ class PhrasesLib $language = $this->_ci->PersonModel->getLanguage(getAuthUID()); } - // Checks if categories is associative or indexed array - if (ctype_digit(implode('', array_keys($categories)))) - { - // is indexed array -> Loads phrases - $indexArray = true; - $phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndLanguage($categories, $language); - } - else - { - // is assoc array -> Loads specific phrasentexte by category and phrases - $assocArray = true; - $phrases = $this->_ci->PhraseModel - ->getPhrasesByCategoryAndPhrasesAndLanguage($categories, $language); - } - - // If language is not default language and phrasentext is null -> fallback to default language - if ($language != DEFAULT_LANGUAGE) - { - // get array with phrasentexte in the default language - $defaultPhrases = array(); - if ($indexArray) - { - $defaultPhrases = $this->_ci->PhraseModel - ->getPhrasesByCategoryAndLanguage($categories, DEFAULT_LANGUAGE); - } - elseif ($assocArray) - { - $defaultPhrases = $this->_ci->PhraseModel - ->getPhrasesByCategoryAndPhrasesAndLanguage($categories, DEFAULT_LANGUAGE); - } + $this->_setPhrases($categories, $language); + } + } + } - // combine array with phrasentexte in users language and in default language - // (default used if phrasentext in users language is null or not set) - $phrasesArr = array(); - if (!empty($phrases->retval)) + /** + * + * stores phrases-array in property $_phrases + */ + private function _setPhrases($categories, $language) + { + $phrases = null; + // Checks if categories is associative or indexed array + if (ctype_digit(implode('', array_keys($categories)))) + { + // is indexed array -> Loads phrases + $isIndexArray = true; + $phrases = $this->_ci->PhraseModel->getPhrasesByCategoryAndLanguage($categories, $language); + } + else + { + // is assoc array -> Loads specific phrasentexte by category and phrases + $isIndexArray = false; + $phrases = $this->_ci->PhraseModel + ->getPhrasesByCategoryAndPhrasesAndLanguage($categories, $language); + } + + // If language is not default language and phrasentext is null -> fallback to default language + if ($language != DEFAULT_LANGUAGE) + { + // get array with phrasentexte in the default language + $defaultPhrases = null; + if ($isIndexArray) + { + $defaultPhrases = $this->_ci->PhraseModel + ->getPhrasesByCategoryAndLanguage($categories, DEFAULT_LANGUAGE); + } + else + { + $defaultPhrases = $this->_ci->PhraseModel + ->getPhrasesByCategoryAndPhrasesAndLanguage($categories, DEFAULT_LANGUAGE); + } + + // combine array with phrasentexte in users language and in default language + // (default used if phrasentext in users language is null or not set) + if (hasData($phrases) && hasData($defaultPhrases)) + { + // loop through phrases in default language + foreach ($defaultPhrases->retval as $defaultPhrase) + { + $found = false; // flag for found phrase + + // loop through phrases in users language + foreach ($phrases->retval as $phrase) { - // loop through phrases in default language - foreach ($defaultPhrases->retval as $defaultPhrase) + // if same phrase and category found and text is not null + // use phrase in users language + if ($phrase->phrase == $defaultPhrase->phrase + && $phrase->category == $defaultPhrase->category + && !is_null($phrase->text)) { - $phraseObj = new stdClass(); - $found = false; // flag for found phrase - - // loop through phrases in users language - foreach ($phrases->retval as $phrase) - { - // if same phrase and category found and text is not null - // use phrase in users language - if ($phrase->phrase === $defaultPhrase->phrase - && $phrase->category === $defaultPhrase->category - && !is_null($phrase->text)) - { - $found = true; - array_push($phrasesArr, $phrase); - break; - } - } - - // otherwise use phrase in default language - if (!$found) - { - array_push($phrasesArr, $defaultPhrase); - } + $found = true; + break; } - $phrases->retval = $phrasesArr; } - else - { - $phrases->retval = $defaultPhrases->retval; - } - } - // If there are phrases loaded then store them in the property _phrases - if (hasData($phrases)) - { - $this->_phrases = $phrases->retval; + // otherwise use phrase in default language + if (!$found) + { + array_push($phrases->retval, $defaultPhrase); + } } } + elseif (hasData($defaultPhrases)) + { + $phrases = $defaultPhrases; + } + } + + // If there are phrases loaded then store them in the property _phrases + if (hasData($phrases)) + { + $this->_phrases = $phrases->retval; } } diff --git a/application/models/system/Phrase_model.php b/application/models/system/Phrase_model.php index da9da1a7e..694605ae3 100644 --- a/application/models/system/Phrase_model.php +++ b/application/models/system/Phrase_model.php @@ -89,26 +89,21 @@ class Phrase_model extends DB_Model 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 '; + WHERE pt.sprache = ? AND '; + + $parametersArray = array($language); foreach ($phrasesParams as $category => $phrases) { - $query .= ' - (p.category = \'' . $category . '\' - AND phrase IN ('; - foreach ($phrases as $phrase) - { - $query .= '\'' . $phrase . '\', '; - } - - $query = rtrim($query, ', '); - $query .= ')) AND pt.sprache = \'' . $language . '\' OR '; + $query .= '(category = ? AND phrase IN ?) OR '; + $parametersArray[] = $category; + $parametersArray[] = $phrases; } - $query = rtrim($query, 'OR '); + $query = rtrim($query, ' OR '); $query .= ' ORDER BY p.category, p.phrase, pt.orgeinheit_kurzbz DESC, pt.orgform_kurzbz DESC'; - return $this->execQuery($query, array($phrasesParams, $language)); + return $this->execQuery($query, $parametersArray); } } From 7facb7ab78a526744984c2ad8f68687ae051d328 Mon Sep 17 00:00:00 2001 From: Cris Date: Wed, 6 Jun 2018 15:12:42 +0200 Subject: [PATCH 5/5] Improved PhrasesLib and Phrase_model . optimized code in PhrasesLib.php . secured method in Phrase_model with bindings --- application/libraries/PhrasesLib.php | 9 +++++++-- application/models/system/Phrase_model.php | 1 - 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/application/libraries/PhrasesLib.php b/application/libraries/PhrasesLib.php index bfc2f8493..e50fc4d22 100644 --- a/application/libraries/PhrasesLib.php +++ b/application/libraries/PhrasesLib.php @@ -263,8 +263,13 @@ class PhrasesLib } /** - * - * stores phrases-array in property $_phrases + * Retrieves phrases in the users language. + * If a phrase is not set in the users language it will be retrieved in the default language. + * Stores phrases-array in property $_phrases. + * @param array $categories Could be an: + * - indexed array: string or an array of strings. These are the categories used to load phrases. + * - associative array: of categories, and for each category there is an array of phrases. + * @param string User's language or default language. */ private function _setPhrases($categories, $language) { diff --git a/application/models/system/Phrase_model.php b/application/models/system/Phrase_model.php index 694605ae3..fd121758a 100644 --- a/application/models/system/Phrase_model.php +++ b/application/models/system/Phrase_model.php @@ -81,7 +81,6 @@ class Phrase_model extends DB_Model /** * Loads phrases using category(s) and language as keys using associative category array * that contains also phrases for each category - * They are ordered by p.category, p.phrase, pt.orgeinheit_kurzbz DESC and pt.orgform_kurzbz DESC' */ public function getPhrasesByCategoryAndPhrasesAndLanguage($phrasesParams, $language) {