<?php

/**
 * @class lang
 * @brief Manage, select, and display lang
 * 
 * @author Benjamin Mercier
 * @data 08/03/2009
 * @version 0.1
*/
class lang {

	private $datetime_format = array();

	/**
	 * @brief List all of countrie
	 * @param clause : Additionnal clause at end of the req
	 * @return Array->object of differents countries
	*/
	public function listCountries() 
	{
		$req = "SELECT 
					id,
					flag,
					countrie,
					is_lang
				FROM countries";
		$result = $_SESSION['database']->fetchObject($req);
		return $result;
	} // End of listCountries

	/**
	 * @brief Return lang of current user
	 * @return $lang -> code of the lang (fr/en/de/etc.)
	*/
	public function userGetLang()
	{
		if ( !LANG_USE_OTHERS ) {
			return LANG_DEFAULT;
		}
		if( !is_null($_SESSION['user']->information_user) ) {
			if( !$lang = $_SESSION['user']->userGetLang() ) {
				throw new myException('The user is connected but userLang has returned false');
			}
			$lang_allowed = unserialize(LANG_ALLOWED);
			if ( !in_array($lang, $lang_allowed) ) {
				throw new myException('Lang of the user is not allowed in configuration file');
			}
		} else {
			$lang = LANG_DEFAULT;
		}
		return $lang;
	} // End of getLang

	/**
	 * @brief select word in lang file from key
	 * @param key -> 'msg_not_client' (exemple)
	 * @return -> 'Bienvenue sur votre futur espace membre...'
	 * @return false if key not found
	*/
	public function getWordFromLangFile( $key ) {
		if ( !is_string($key) ) throw new myException('The key word is not an valid string');
		$lang_file = parse_ini_file(PATH_LANG.$this->userGetLang().'/lang');
		if ( !$lang_file ) throw new myException('Cannot open the lang file');
		if ( isset($lang_file[$key]) ) return $lang_file[$key];
		else return NULL;
	}

	/**
	 * @brief return DATE format in lang of the current user
	 * @return date_format
	*/
	public function userGetDateFormat()
	{
		$lang = $_SESSION['user']->userGetLang();
		if ( isset($this->datetime_format[$lang]) ) return $this->datetime_format[$lang]['date'];
		$req = "SELECT date_format,time_format FROM countries WHERE flag = '$lang' and is_lang = 'true'";
		$result = $_SESSION['database']->fetchObject($req);
		if ( count($result) == 0 or count($result) > 1 ) throw new myException('Too many of not found record of lang');
		$this->datetime_format[$lang] = array('date' => $result[0]->date_format, 'time' => $result[0]->time_format);
		return $result[0]->date_format;
	}

	/**
	 * @brief return TIME format in lang of the current user
	 * @return time_format
	*/
	public function userGetTimeFormat()
	{
		$lang = $_SESSION['user']->userGetLang();
		if ( isset($this->datetime_format[$lang]) ) return $this->datetime_format[$lang]['time'];
		$req = "SELECT date_format,time_format FROM countries WHERE flag = '$lang' and is_lang = 'true'";
		$result = $_SESSION['database']->fetchObject($req);
		if ( count($result) == 0 or count($result) > 1 ) throw new myException('Too many of not found record of lang');
		$this->datetime_format[$lang] = array('date' => $result[0]->date_format, 'time' => $result[0]->time_format);
		return $result[0]->time_format;
	}


} // End of class

?>
