<?php
/*
	Stempora web framework
	copyright (c) 2002-2013 Stempora
		web:  www.stempora.com
		mail: support@stempora.com

*/

// dependencies

/**
* description
*
* @library	
* @author	
* @since	
*/
class CLanguages extends CPlugin{
	
	var $tplvars; 
	/**
	* description
	*
	* @var type
	*
	* @access type
	*/
	var $languages = array();
	

	function CLanguages() {
		//$this->CPlugin($db, $tables , $templates);
	}

	function DoEvents(){
	}


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function ReadVars() {

		global $_LANG , $_LANG_CODE;

		$vars = $this->db->QFetchRowArray("SELECT lang_var , lang_{$_LANG_CODE}_val AS val from {$this->tables['core:languages_vars']}");
	
		if (is_array($vars)) {
			foreach ($vars as $key => $val) {
				$_LANG[$val["lang_var"]] = $val["val"];
			}			
		}		
	}
		

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function DetectLang($code = "") {

		$this->LoadLanguages();		

		//if no code then get the first language
		if (!$code) {
			reset($this->languages);
			$lang = $this->languages[key($this->languages)];
		} else {
	
			if (is_array($this->languages[$code])) {
				$lang = $this->languages[$code];
			} else {
				reset($this->languages);
				$lang = $this->languages[key($this->languages)];
			}
			
		}
						

		global $_LANG_CODE , $_LANG_ID , $_LANG_RAW , $_LANG;


		$_GET["_LANG"] = $_LANG = $_LANG_CODE = $lang["lang_code"];
		$_LANG_ID = $lang["lang_id"];
		$_LANG_RAW = $lang;

		if (!$code) {
			global $_LANG;

			$_LANG = $lang["lang_code"];
		}


		
		
	}


	function DefaultLang() {

		$this->LoadLanguages();		


		$lang = $this->languages[$this->default_lang];


		global $_LANG_CODE , $_LANG_ID , $_LANG_RAW , $_LANG;

		$_LANG = $_LANG_CODE = $lang["lang_code"];
		$_LANG_ID = $lang["lang_id"];
		$_LANG_RAW = $lang;

	}
	
	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access	
	*/
	function Menu($template = null) {
		global $_LANG , $base;

		$this->LoadLanguages();

		$this->__init();

		if ($template != null) {
			$tpl = $template;
		} else {
			$tpl = $this->private->templates["main"];
		}
		

		$langs = $this->languages;


		if (is_array($langs) && (count($langs) > 1)) {

			foreach ($langs as $key => $val) {
				$langs[$key]["selected"] = $val["lang_code"] == $_LANG ? $this->private->templates["main"]->blockReplace("Selected" , array()) : "";

				if ($_LANG == $val["lang_code"]) {
					$selected = $val;
				}
				
			}

			return CTemplateStatic::Replace(
				$base->html->Table(
					$tpl , 
					"" , 
					$langs
				),
				array(
					"current"	=> $tpl->blockREplace("Current" , $selected)
				)
			);

		}
		

	}
	

	function __init() {
		global $_CONF;

		if ($this->__inited) {
			return "";
		}

		$this->__inited = true;
		
		$path = $this->tpl_path;

		$templates = array(
			"main"		=> "main.htm",
		);

		foreach ($templates as $key => $val) {
			$this->private->templates[$key] = new CTemplateDynamic(
				$path . $val
			);
		}
	} 

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function LoadVars() {
		global $_LANG , $_LANG_ID;


		$vars = $this->db->QFetchRowArray(
				"SELECT * FROM 
					{$this->tables['core:languages_vars']} as v,
					{$this->tables['core:languages_vars_lang']} as l
				WHERE
					v.var_id = l.var_id AND 
					l.lang_id = {$_LANG_ID}
					"
			);

		if (is_array($vars)) {
			foreach ($vars as $key => $val) {
		
				$data["LANG:" . strtoupper($val["var_name"])] = $val["var_value"];
			}
			
		}

		return $data;
	}

	
	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function LoadLanguages() {
		$_CONF["url"];

		if (!$this->languages) {

			$langs = $this->db->QFetchRowArray("SELECT * FROM {$this->tables['core:languages']} WHERE lang_status = 1 order by lang_order ASC ");


			if (is_array($langs)) {
				foreach ($langs as $key => $val) {

					if ($val["lang_default"]) {
						$val["link"] = $_CONF["url"];
					} else {
						$val["link"] = $_CONF["url"] . $val["lang_code"] . "/";
					}
					
					$this->languages[$val["lang_code"]] = $val;



					/*
					if ($val["lang_default"]) {

						$this->default_lang = $val["lang_code"];
					}*/
					
				}				
			}			

			$this->default_lang = $this->private->vars->data["set_language"];

			$this->languages_count = count($this->languages);;

		}				
	}
	
}

?>