_code = $message_code; $this->_lang = $lang_code; $this->_setData = $set_data; $this->_dbTable = $db_table; $oPDOLink = ClassConfig::databaseConnect(); if(!isset($_SESSION['translations'][$this->_code]) or $_SESSION['translations'][$this->_code]['source']==''){ $row = $this->getTranslationInDatabase($this->_code, $this->_lang); $_SESSION['translations'][$this->_code][$this->_lang] = $row[$this->_lang]; $_SESSION['translations'][$this->_code]['source'] = $row['source']; } if($_SESSION['translations'][$this->_code][$this->_lang] != ''){ $translation = $_SESSION['translations'][$this->_code][$this->_lang]; } else { $translation = $_SESSION['translations'][$this->_code]['source']; } $replacement_regex = '/\[data\]/'; foreach($this->_setData as $k=>$v){ $translation = preg_replace($replacement_regex, $this->_setData[$k], $translation, 1); } return $translation; } // used in files /index.php & /themes/beestrap/index.php public function listTranslations($lang_code='source'){ $this->_langCode = $lang_code; $oPDOLink = ClassConfig::databaseConnect(); $res = array(); $sql = " SELECT id, code, source, ".$this->_langCode." FROM core_translation; "; $execSQL = $oPDOLink->prepare($sql); $execSQL->execute(array()); $rows = $execSQL->fetchAll(PDO::FETCH_ASSOC); foreach($rows as $k=>$v){ $res[$rows[$k]['code']] = array('source'=>$rows[$k]['source'], $this->_langCode=>$rows[$k][$this->_langCode]); } return $res; } public function getMessage($message_code, $lang_code, $set_data){ $this->_messageCode = $message_code; $this->_langCode = $lang_code; $this->_setData = $set_data; $oTrans = new ClassTranslation(); $message = $oTrans->getTranslation($this->_messageCode, $this->_langCode); $replacement_regex = '/\[data\]/'; foreach($this->_setData as $k=>$v){ $message = preg_replace($replacement_regex, $this->_setData[$k], $message, 1); } return $message; } public function getTranslationWithData($code, $lang, $set_data){ $this->_code = $code; $this->_lang = $lang; $this->_setData = $set_data; if(!isset($_SESSION['translations'][$this->_code]) or $_SESSION['translations'][$this->_code]['source']==''){ $row = $this->getTranslationInDatabase($this->_code, $this->_lang); $_SESSION['translations'][$this->_code][$this->_lang] = $row[$this->_lang]; $_SESSION['translations'][$this->_code]['source'] = $row['source']; } if($_SESSION['translations'][$this->_code][$this->_lang] != ''){ $translation = $_SESSION['translations'][$this->_code][$this->_lang]; } else { $translation = $_SESSION['translations'][$this->_code]['source']; } $replacement_regex = '/\[data\]/'; foreach($this->_setData as $k=>$v){ $translation = preg_replace($replacement_regex, $this->_setData[$k], $translation, 1); } return $translation; } public function getTranslationInDatabase($code, $lang, $db_table="core_translation"){ $this->_code = $code; $this->_lang = $lang; $this->_dbTable = $db_table; $oPDOLink = ClassConfig::databaseConnect(); $sql=" SELECT source, ".$this->_lang." FROM ".$this->_dbTable." WHERE code=:code LIMIT 1 "; $execSQL = $oPDOLink->prepare($sql); $execSQL->execute(array(':code'=>$this->_code)); $row = $execSQL->fetch(PDO::FETCH_ASSOC); return $row; } public function getTranslation($code, $lang = null, $set_data = array()){ $this->_code = $code; $this->_lang = $lang ?? $_SESSION['config']['default_lang_code']; $this->_setData = $set_data; //if translation not in SESSION, then go get it from DB if(!isset($_SESSION['translations'][$this->_code]) or $_SESSION['translations'][$this->_code]['source']==''){ $row = $this->getTranslationInDatabase($this->_code, $this->_lang); $_SESSION['translations'][$this->_code][$this->_lang] = $row[$this->_lang]; $_SESSION['translations'][$this->_code]['source'] = $row['source']; } //if translation in current language, display it. Else, display source if($_SESSION['translations'][$this->_code][$this->_lang] != ''){ $translation = $_SESSION['translations'][$this->_code][$this->_lang]; } else { $translation = $_SESSION['translations'][$this->_code]['source']; } //if [data] in content, get values set in array $set_data $replacement_regex = '/\[data\]/'; foreach($this->_setData as $k=>$v){ $translation = preg_replace($replacement_regex, $this->_setData[$k], $translation, 1); } return $translation; } //public function getTranslation($code, $lang){ // $this->_code = $code; // $this->_lang = $lang; // // if(!isset($_SESSION['translations'][$this->_code]) or $_SESSION['translations'][$this->_code]['source']==''){ // $row = $this->getTranslationInDatabase($this->_code, $this->_lang); // $_SESSION['translations'][$this->_code][$this->_lang] = $row[$this->_lang]; // $_SESSION['translations'][$this->_code]['source'] = $row['source']; // } // // if($_SESSION['translations'][$this->_code][$this->_lang] != ''){ // return $_SESSION['translations'][$this->_code][$this->_lang]; // } else { // return $_SESSION['translations'][$this->_code]['source']; // } //} public function getTranslationById($id, $lang){ //TODO get translation from APC cache $this->_id = $id; $this->_lang = $lang; $oPDOLink = ClassConfig::databaseConnect(); $sql=" SELECT source, ".$this->_lang." FROM core_translation WHERE id=:id LIMIT 1 "; $execSQL = $oPDOLink->prepare($sql); $execSQL->execute(array(':id'=>$this->_id)); $row = $execSQL->fetch(PDO::FETCH_ASSOC); if($row[$this->_lang] != ''){ return $row[$this->_lang]; } else { return $row['source']; } } public function getTranslationForEditing($translation_id, $lang){ $this->_translationId = $translation_id; $this->_lang = $lang; $oPDOLink = ClassConfig::databaseConnect(); $sql=" SELECT code, source, ".$this->_lang." FROM core_translation WHERE id=:translation_id LIMIT 1; "; $execSQL = $oPDOLink->prepare($sql); $execSQL->execute(array(':translation_id'=>$this->_translationId)); $row = $execSQL->fetch(PDO::FETCH_ASSOC); return $row; } public function translate($translation_id, $lang_code, $data){ $this->_translationId = $translation_id; $this->_langCode = $lang_code; $this->_data = $data; $oPDOLink = ClassConfig::databaseConnect(); $sql=" UPDATE core_translation SET source=:source, ".$this->_langCode."=:translation_text WHERE id=:translation_id; "; $execSQL = $oPDOLink->prepare($sql); if($execSQL->execute(array( ':source'=>$this->_data['source'], ':translation_text'=>$this->_data['translation'], ':translation_id'=>$this->_translationId ))){ $message['state'] = 'success'; } else{ $message['state'] = 'failed'; } return $message; } public function listAllTranslations($lang_code){ $this->_langCode = $lang_code; $oPDOLink = ClassConfig::databaseConnect(); $sql = " SELECT id, code, source, ".$this->_langCode." FROM core_translation; "; $execSQL = $oPDOLink->prepare($sql); $execSQL->execute(array()); $rows = $execSQL->fetchAll(PDO::FETCH_ASSOC); return $rows; } }