From 7c5995c31890a51c73b5eca10d45ef2701368295 Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 29 May 2018 15:57:10 +0200 Subject: [PATCH] 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'))); - - - +};