setting_main_language=$languageno; $sql = "UPDATE languagesettings SET mainlanguage=%d"; $localdb->Query($sql,$languageno); } // Get the main language - work setting for admin pages function getMainLanguage(){ global $localdb; $sql = "SELECT * FROM languagesettings"; $localdb->Query($sql); //$this->setting_main_language=$localdb->Result(0,'mainlanguage'); return $localdb->Result(0,'mainlanguage'); } // Set default language - user pages function setDefaultLanguage($languageno){ global $localdb; $setting_default_language=$languageno; $sql = "UPDATE languagesettings SET defaultlanguage=%d"; $localdb->Query($sql,$languageno); } // Get default language - user pages function getDefaultLanguage(){ global $localdb; $sql = "SELECT * FROM languagesettings"; $localdb->Query($sql); $setting_default_language=$localdb->Result(0,'defaultlanguage'); return $setting_default_language; } // Set user language - user chooses language function setUserLanguage($languageno){ setcookie('userlanguage', $languageno, time() + 1728000, '/', $GLOBALS['configuration']['cookie_domain']); } // Get user language - language user has chosen or default function getUserLanguage(){ if(!isset($_COOKIE['userlanguage'])){ // If user has not chosen a language Language::setUserLanguage(Language::getDefaultLanguage()); $user_language=Language::getDefaultLanguage(); }else{ $user_language=$_COOKIE['userlanguage']; } return $user_language; } // Get the name of a language in the language specified function getLanguageName($language_id, $display_language){ global $localdb; $sql = "SELECT * FROM languagenames WHERE (languageid=%d) AND (language=%d)"; $localdb->Query($sql, $language_id, $display_language); return $localdb->Result(0,"languagename"); } // Add language to list of languages eventually available to user function addLanguagesAvailable($languageno){ global $localdb; $sql = "INSERT INTO languagesavailable (language) VALUES (%d)"; $localdb->Query($sql,$languageno); // TODO: Prevent duplicate available languages } // remove language from list of languages available to user function removeLanguagesAvailable($languageno){ global $localdb; $sql = "DELETE FROM languagesavailable WHERE (language=%d)"; $localdb->Query($sql,$languageno); } // Set language definitively available to user (as when finished translating) function setLanguageAvailable($language){ global $localdb; $sql = "UPDATE languagesavailable SET available=1 WHERE language=%d"; $localdb->Query($sql,$language); } // Set language not available (as when pages are not translated) function setLanguageUnAvailable($language){ global $localdb; $sql = "UPDATE languagesavailable SET available=0 WHERE language=%d"; $localdb->Query($sql,$language); } // Get a list of languages available to user function getLanguagesAvailableList(){ global $localdb; $sql = "SELECT * FROM languagenames as lnames right JOIN languagesavailable as lavailable on (lavailable.language=lnames.languageid) WHERE (lavailable.available=1) AND (lnames.language=".Language::getMainLanguage().")"; $arr = array(); $result = $localdb->Query($sql); while ($row = $localdb->FetchObject($result)){ $object = new Language; $object->language_available_id=$row->id; $object->language_available_name=$row->languagename; $object->language_available_lid=$row->language; $object->language_available_availability=$row->available; $arr[]=$object; } if(count($arr) > 0){ return $arr; }else{ return false; } } // Get a list of languages not available to user yet function getLanguagesUnAvailableList(){ global $localdb; $sql = "SELECT * FROM languagenames as lnames right JOIN languagesavailable as lavailable on (lavailable.language=lnames.languageid) WHERE (lavailable.available=0) AND (lnames.language=".Language::getMainLanguage().")"; $result = $localdb->Query($sql); while ($row = $localdb->FetchObject($result)){ $object = new Language; $object->language_unavailable_id=$row->id; $object->language_unavailable_name=$row->languagename; $object->language_unavailable_lid=$row->language; $this->languages_unavailable_list[]=$object; } if(isset($this->languages_unavailable_list)){ return $this->languages_unavailable_list; }else{ return false; } } // Get a list of all languages added function getAllLanguagesAvailableList(){ global $localdb; $sql = "SELECT * FROM languagenames as lnames right JOIN languagesavailable as lavailable on (lavailable.language=lnames.languageid) WHERE (lnames.language=".Language::getMainLanguage().")"; $result = $localdb->Query($sql); while ($row = $localdb->FetchObject($result)){ $object = new Language; $object->language_available_id=$row->id; $object->language_available_name=$row->languagename; $object->language_available_lid=$row->language; $object->language_available_availability=$row->available; $this->languages_available_list[]=$object; } if(isset($this->languages_available_list)){ return $this->languages_available_list; }else{ return false; } } // Get a list of all languages in the system function getLanguages(){ global $localdb; $sql = "SELECT * FROM languages"; $result = $localdb->Query($sql); while ($row = $localdb->FetchObject($result)){ $object = new Language; $object->languages_id=$row->languageid; $object->languages_name=$row->languagename; $this->languages_list[]=$object; } if(isset($this->languages_list)){ return $this->languages_list; }else{ return false; } } // Setup: Add a table that contains translatable data function setLanguageTables($tablename,$languageidname,$timecreated,$timelastedit,$tableidname,$displayname){ global $localdb; $sql = "INSERT INTO languagestables (tablename,languageidname,timecreatedname,timelasteditname"; $sql.=",idname,tabledisplayname) VALUES (%s,%s,%s,%s,%s,%s)"; $localdb->Query($sql,$tablename,$languageidname,$timecreated,$timelastedit,$tableidname,$displayname); $res=$localdb->Query("SELECT LAST_INSERT_ID() as lastid"); $lastid=$localdb->Result(0,"lastid"); return $lastid; } // Setup: Add columns that contain translatable data function setLanguageColumns($tableid,$languageidname,$contentname,$contenttype){ global $localdb; $sql = "INSERT INTO languagescolumns (tableid,languageidname, contentname,contenttype) VALUES (%d,%s,%s,%d)"; $localdb->Query($sql,$tableid,$languageidname,$contentname,$contenttype); $res=$localdb->Query("SELECT LAST_INSERT_ID() as lastid"); $lastid=$localdb->Result(0,"lastid"); return $lastid; } // Setup: Add columns that contain non-translatable data function setDirectCopyColumns($tableid,$columnname,$str_or_digit){ global $localdb; $sql = "INSERT INTO languagesdirectcopycolumns (tableid,columnname, str_or_digit) VALUES (%d,%s,%d)"; $localdb->Query($sql,$tableid,$columnname,$str_or_digit); $res=$localdb->Query("SELECT LAST_INSERT_ID() as lastid"); $lastid=$localdb->Result(0,"lastid"); return $lastid; } // Get tables that contain translatable data function getLanguageTables(){ global $localdb; $languagetablelist=array(); $sql = "SELECT * FROM languagestables"; $result = $localdb->Query($sql); while ($row = $localdb->FetchObject($result)){ $object = new Language; $object->language_tables_id=$row->id; $object->language_tables_name=$row->tablename; $object->language_tables_idname=$row->languageidname; $object->language_tables_timecreated_name=$row->timecreatedname; $object->language_tables_timelastedited_name=$row->timelasteditname; $object->language_tables_tableidname=$row->idname; $object->language_tables_tabledisplayname=$row->tabledisplayname; $languagetablelist[]=$object; } return $languagetablelist; } // Get specific table that contain translatable data function getLanguageTable($table){ global $localdb; $languagetablelist=array(); $sql = "SELECT * FROM languagestables WHERE (tablename=%s)"; $result = $localdb->Query($sql,$table); while ($row = $localdb->FetchObject($result)){ $object = new Language; $object->language_tables_id=$row->id; $object->language_tables_name=$row->tablename; $object->language_tables_idname=$row->languageidname; $object->language_tables_timecreated_name=$row->timecreatedname; $object->language_tables_timelastedited_name=$row->timelasteditname; $object->language_tables_tableidname=$row->idname; $object->language_tables_tabledisplayname=$row->tabledisplayname; } return $object; } // Get columns that contain translatable data function getLanguageColumns($table){ global $localdb; $languagecolumnlist=array(); $sql = "SELECT * FROM languagescolumns WHERE (tableid=%d)"; $result = $localdb->Query($sql,$table); while ($row = $localdb->FetchObject($result)){ $object = new Language; $object->language_columns_id=$row->id; $object->language_columns_content_name=$row->contentname; $object->language_columns_content_type=$row->contenttype; $languagecolumnlist[]=$object; } return $languagecolumnlist; } // Get columns that contain non-translatable data function getLanguageDirectColumns($table){ global $localdb; $languagecolumnlist=array(); $sql = "SELECT * FROM languagesdirectcopycolumns WHERE (tableid=%d)"; $result = $localdb->Query($sql,$table); while ($row = $localdb->FetchObject($result)){ $object = new Language; $object->language_direct_copy_columns_tableid=$row->tableid; $object->language_direct_copy_columns_name=$row->columnname; $object->language_direct_copy_columns_str_or_digit=$row->str_or_digit; $languagecolumnlist[]=$object; } return $languagecolumnlist; } // Get a list of data not yet translated function getNotTranslated($language){ global $localdb; $nottranslated=array(); $tables=Language::getLanguageTables(); for($xx=0;$xxlanguage_tables_name." WHERE (".$tables[$xx]->language_tables_idname."=%d) AND (translated=0)"; //echo $tables[$xx]->language_tables_name."\n\r"; $result = $localdb->Query($sql, $language); while ($row = $localdb->FetchObject($result)){ $object = new Language; $tableidnamestr=$tables[$xx]->language_tables_tableidname; $tabledisplaystr=$tables[$xx]->language_tables_tabledisplayname; $object->language_contents_id=$row->$tableidnamestr; $object->language_contents_displayname=$row->$tabledisplaystr; //echo $object->language_contents_displayname; $object->language_contents_language=$language; $object->language_tables_name=$tables[$xx]->language_tables_name; $nottranslatedlist[]=$object; //} //break; } } if(isset($nottranslatedlist)){ return $nottranslatedlist; }else{ return false; } } // Setup: Add tables that reffer to other tables function setRefferencingTables($refferencing,$refferenced,$refferencingid,$refferencedid,$refferencingkey){ global $localdb; $sql = "INSERT INTO languagerefferencetables (refferencingtable,refferencedtable,refferencingtableid,refferencedtableid,refferencingtablekey) VALUES (%s,%s,%s,%s,%s)"; $localdb->Query($sql,$refferencing,$refferenced,$refferencingid,$refferencedid,$refferencingkey); } // Copy language - make new added language ready with data copied from existing language function copyLanguage($language,$fromlanguage){ global $localdb; // Can take time, so time limit is set rediculously high to ensure never to get timeouts set_time_limit(72000000000); // Add language to available table Language::addLanguagesAvailable($language); // Set arrays for refferencing tables $refferencingtables=array(); $refferencedtables=array(); $refferecingtablesid=array(); $refferencedtablesid=array(); $refferencingtableskey=array(); // Get data to fill in refferencing table arrays $sql = "SELECT * FROM languagerefferencetables"; $result = $localdb->Query($sql); while ($row = $localdb->FetchObject($result)){ $refferencingtables[]=$row->refferencingtable; $refferencedtables[]=$row->refferencedtable; $refferecingtablesid[]=$row->refferencingtableid; $refferencedtablesid[]=$row->refferencedtableid; $refferencingtableskey[]=$row->refferencingtablekey; } // Set tables array with data about all tables that contain translatable data $tables=Language::getLanguageTables(); // For every table that contains translatable data do ... for($xx=0;$xxlanguage_tables_id); // Fill directcolumns array with data about all columns in current table that does not contain translatable data $directcolumns=Language::getLanguageDirectColumns($tables[$xx]->language_tables_id); // Get all rows from current table that matches language from which we wish to copy $sql = "SELECT * FROM ".$tables[$xx]->language_tables_name." WHERE (".$tables[$xx]->language_tables_idname."=%d)"; $result = $localdb->Query($sql, $fromlanguage); while ($row = $localdb->FetchObject($result)){ // Get the createid for this row of this table. createid links "identical" rows for different languages $sql2 = "SELECT * FROM languagestableids WHERE (languageid=%d) AND (actualid=%d)"; $tableidnamestr=$tables[$xx]->language_tables_tableidname; $localdb->Query($sql2,$fromlanguage,$row->$tableidnamestr); $createid=$localdb->Result(0, "createid"); // Create column and values part of sql command $cstr=null; $cvalue=null; for($yy=0;$yylanguage_columns_content_name; // add column name $currentstr=$columns[$yy]->language_columns_content_name; // preventing Object to string conversion error $currentvalue=$row->$currentstr; // get the value of the column for current row $cvalue.="'".$currentvalue."'"; // add current column value - we assume it's a string, as anything else wouldn't make sence if(sizeof($columns)>$yy+1){ // add comma if more columns $cstr.=","; $cvalue.=","; } } // add columns and values that is not translatable if(sizeof($directcolumns)>0){ // if in fact there are such columns, add a comma $cstr.=","; $cvalue.=","; } for($yy=0;$yylanguage_direct_copy_columns_tableid==$tables[$xx]->language_tables_id){ // Make sure the column belongs to the table $cstr.=$directcolumns[$yy]->language_direct_copy_columns_name; // add column name $currentstr=$directcolumns[$yy]->language_direct_copy_columns_name; // preventing Object to string conversion error $currentvalue=$row->$currentstr; // get the value of the column for current row // These columns would mostly contain numbers, but we can't rely on that to be true all the time, so we do a little check and add the value if($directcolumns[$yy]->language_direct_copy_columns_str_or_digit==1){ $cvalue.="'".$currentvalue."'"; }else{ $cvalue.=$currentvalue; } if(sizeof($directcolumns)>$yy+1){ $cstr.=","; $cvalue.=","; } } } // Add timecreated and timelastedited columns if available if($tables[$xx]->language_tables_timecreated_name <> null){ $cstr.=",".$tables[$xx]->language_tables_timecreated_name; $cvalue.=",Now()"; } if($tables[$xx]->language_tables_timelastedited_name <> null){ $cstr.=",".$tables[$xx]->language_tables_timelastedited_name; $cvalue.=",0"; } // Do the insert of the copied data $sql2 = "INSERT INTO ".$tables[$xx]->language_tables_name." (".$tables[$xx]->language_tables_idname.",".$cstr.") VALUES (".$language.",".$cvalue.")"; $localdb->Query($sql2); // Get the id of the inserted row $res=null; $lastid=null; $res=$localdb->Query("SELECT LAST_INSERT_ID() as lastid"); $lastid=$localdb->Result(0,"lastid"); // To keep tabs on "identical" rows in different languages, so missing pages can be found easily, we add relevant data to languagetableids - linking createid to actualid $sql2 = "INSERT INTO languagestableids (createid, tablename, languageid, actualid) VALUES (%d,%s,%d,%d)"; $localdb->Query($sql2,$createid,$tables[$xx]->language_tables_name,$language,$lastid); // Add data to languagerefferenceupdates to enable updating foreign keys later for($kk=0;$kklanguage_tables_name){ $sql3 = "INSERT INTO languagerefferenceupdates (refferencedtable, oldvalue, newvalue, updatetable, updatecolumn, idcolumn) VALUES ('".$refferencedtables[$kk]."',".$row->$refferencedtablesid[$kk].",".$lastid.",'".$refferencingtables[$kk]."','".$refferencingtableskey[$kk]."','".$refferecingtablesid[$kk]."')"; $localdb->Query($sql3); } } } } // Update foreign keys $sql = "SELECT * FROM languagerefferencetables"; $result = $localdb->Query($sql); while ($row = $localdb->FetchObject($result)){ // First get the name of the column the contains the language number $sql2 = "SELECT * FROM languagestables WHERE (tablename='".$row->refferencingtable."') LIMIT 1"; $result2 = $localdb->Query($sql2); while ($row2 = $localdb->FetchObject($result2)){ $languageid=$row2->languageidname; } // Then get the data needed to update the foreign key $sql2 = "SELECT * FROM languagerefferenceupdates WHERE (refferencedtable = '".$row->refferencedtable."') AND (updatetable = '".$row->refferencingtable."') AND (idcolumn = '".$row->refferencingtableid."')"; $result2 = $localdb->Query($sql2); while ($row2 = $localdb->FetchObject($result2)){ // Update the foreign key $sql3 = "UPDATE ".$row->refferencingtable." SET ".$row->refferencingtableid."=".$row2->newvalue." WHERE (".$row->refferencingtableid."=".$row2->oldvalue.") AND (".$languageid."=".$language.")"; $localdb->Query($sql3); } } // Clean up helper table for foreign key updates $sql = "DELETE FROM languagerefferenceupdates"; $localdb->Query($sql); } // Set row translated status - $yesno=0 is not translated, $yesno=1 is translated function setTranslated($table,$columnid,$yesno){ global $localdb; $tableidname=Language::getTableIdName($table); // get the name of the column that holds the id for the row $sql = "UPDATE ".$table." SET translated=%d WHERE (".$tableidname."=%d)"; $localdb->Query($sql,$yesno,$columnid); } // Get column name that holds the id for table function getTableIdName($table){ global $localdb; $sql = "SELECT idname FROM languagestables WHERE (tablename=%s)"; $localdb->Query($sql,$table); return $localdb->Result(0,"idname"); } // Setup: Set createid links for existing site that is converted to use language module function createTableIds(){ global $localdb; // This can take time, so we set time limit rediculously high to prevent timeouts set_time_limit(72000000000); // Set tables array with data on all tables with translatable data $tables=Language::getLanguageTables(); for($xx=0;$xxlanguage_tables_name." WHERE (".$tables[$xx]->language_tables_idname."=%d)"; $result = $localdb->Query($sql, Language::getMainLanguage()); while ($row = $localdb->FetchObject($result)){ // Set the createid to id $sql2 = "INSERT INTO languagestableids (createid, tablename, languageid, actualid) VALUES (%d,%s,%d,%d)"; $idnamestr=$tables[$xx]->language_tables_idname; $tableidnamestr=$tables[$xx]->language_tables_tableidname; $localdb->Query($sql2,$row->$tableidnamestr,$tables[$xx]->language_tables_name,$row->$idnamestr,$row->$tableidnamestr); } } } // Setup: Set all rows in all tables as translated for a given language - usefull for existing site that is converted to use language module function setFullLanguageTranslated($language){ global $localdb; // This can take time, so we set time limit rediculously high to prevent timeouts set_time_limit(72000000000); // Set tables array with data on all tables with translatable data $tables=Language::getLanguageTables(); for($xx=0;$xxlanguage_tables_name." SET translated=1 WHERE (".$tables[$xx]->language_tables_idname."=%d)"; $localdb->Query($sql, $language); } return true; } // Setup: Set all rows in all tables as not translated for a given language - usefull for existing site that is converted to use language module (mostly if a mistake was done) function setFullLanguageNotTranslated($language){ global $localdb; // This can take time, so we set time limit rediculously high to prevent timeouts set_time_limit(72000000000); // Set tables array with data on all tables with translatable data $tables=Language::getLanguageTables(); for($xx=0;$xxlanguage_tables_name." SET translated=0 WHERE (".$tables[$xx]->language_tables_idname."=%d)"; $localdb->Query($sql, $language); } return true; } // Get creation id from actual id function getCreationId($current_id, $tablename){ global $localdb; $sql = "SELECT * FROM languagestableids WHERE (actualid=%d) AND (tablename=%s) LIMIT 1"; $localdb->Query($sql, $current_id, $tablename); return $localdb->Result(0,'createid'); } // Get actual id from creation id and language function getActualId($create_id, $lang, $tablename){ global $localdb; $sql = "SELECT * FROM languagestableids WHERE (createid=%d) AND (languageid=%d) AND (tablename=%s) LIMIT 1"; $localdb->Query($sql, $create_id, $lang, $tablename); return $localdb->Result(0,'actualid'); } } ?>