<?php
class configLoader {
	
	static $db;
	
	
	static function loadConfig($prefix) {
		$db = configLoader::$db;
		
		$db->ChangeDatabase(DB_WARPIT_MAIN);
		$sql = "SELECT cName,cValue FROM _WarpitConfig WHERE prefix = '$prefix'";
		$res = $db->SQLexecute($sql);
		while ($row = $db->fetchRow($res))
		{			
			eval("\$config[\$row[0]] = \"$row[1]\";");
		}
		return $config;
	}
	

	/**
	 * load language package for warpit modules
	 * $module 1=admin part, 2=cient part
	 * $languageKey is prefix from _config table
	 * $cName is cName from _config table
	 * $prefix is key of the language object inside language table
	 * 
	 * If user language is not found inside  translations, default language is used
	 *
	 * @param int $module
	 * @param varchar $languageKey
	 * @param varchar $cName
	 * @param varchar $prefix
	 * 
	 * @return array
	 */
	static function loadLanguage($module,$languageKey,$cName,$prefix) {
		$db = configLoader::$db;
		$db->ChangeDatabase(DB_WARPIT_MAIN);

		$defaultLanguage = configLoader::loadConfig($languageKey);
		
		$sql = "SELECT translate_key, translate_text 
				FROM _languagePack 
				WHERE id_programme = $module AND id_language = '".$_SESSION['UserInfo']['language']."' AND sub_page = '$prefix'";
		$db->LogWrite($sql,false,"load user language:$prefix");
		$res = $db->SQLexecute($sql);
		
		if($db->NumRows($res)) {
			while ($zad = $db->fetchAssoc($res)) {
				$languagePack[$zad['translate_key']] = $zad['translate_text'];
			}
		} else  {
			$sql = "SELECT translate_key, translate_text 
					FROM _languagePack 
					WHERE id_programme = $module AND id_language = '".$defaultLanguage[$cName]."' AND sub_page = '$prefix'";
			$db->LogWrite($sql,false,"load default language:$prefix");
			$res = $db->SQLexecute($sql);
			while ($zad = $db->fetchAssoc($res)) {
				$languagePack[$zad['translate_key']] = $zad['translate_text'];		
			}
		}
		
		return $languagePack;
	}
	
}

?>
