<?php
	fo3_model( "Cache" );
	class LanguagesModel {
		public static function getLanguageOptions( $choose = true , $lang = 0 , $cache = true ) {
			if (!$lang) $lang = $GLOBALS["languages"][$GLOBALS["language"]];
			$mc = CacheModel::getCacheNameOnParams( "language_options" ,
				array( "lang" => $lang, "choose" => $choose ) );
			if ($cache) {
				$retval = CacheModel::getCacheValueOnString( $mc );
				if (is_array( $retval )) return $retval;
			}
			$retval = array();
			if ($choose) $retval[""]=__("Choose");

			$query = sprintf( "select language_id,langdef_name from language_langdefs where site_language_id=%d order by langdef_ordering" , $lang );
			$data = FNTH_db_get_resultset( $query );
			foreach ($data as $row)
				$retval[$row["language_id"]]=$row["langdef_name"];
			CacheModel::cacheValueOnString( $mc , $retval );
			return $retval;
		}


		public static function getSiteLanguages( $choose = true , $original = false ) {
			$retval = array();
			if ($choose) $retval[""]=__("Choose");
			$query = ($original) ?
				sprintf(
					"select language_id,language_name as name
						from languages
					where
						language_id in (select site_language_id from site_languages)
					order by language_id" ) :
				sprintf(
					"select language_id,langdef_name as name
						from language_langdefs
					where
						site_language_id=%d and
						language_id in (select site_language_id from site_languages)
					order by langdef_ordering",
					$GLOBALS["languages"][$GLOBALS["language"]] );
			$data = FNTH_db_get_resultset( $query );
			foreach ($data as $row)
				$retval[$row["language_id"]]=$row["name"];
			return $retval;
		}

		public function getLanguageFromId( $language_id ) {
			$query = sprintf( "select language_name from languages where language_id=%d" , $language_id );
			return FNTH_db_get_row( $query );
		}

		public function addLanguage( $datas ) {
			$ins = array();
			$ins["language_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $datas["language_name"] ) );
			FNTH_db_free_result( FNTH_db_query( FNTH_db_generate_insert_query( "languages" , $ins ) ) );
			$query = sprintf( "select currval('languages_language_id_seq') as num" );
			return FNTH_db_get_field( $query , "num" );
		}

		public function getLanguageDefsFromId( $language_id , $language_ids = array() ) {
			$instring = (empty( $language_ids )) ? "" : implode( "," , $language_ids );
			$query = sprintf( "select * from language_langdefs where language_id=%d %s",
				$language_id,
				(empty($instring)) ? "" : sprintf( " and site_language_id in (%s)",$instring ) );
			$ds = FNTH_db_get_resultset( $query );
			$retval = array();
			foreach ($ds as $row) {
				$retval[$row["site_language_id"]]=array(
					"langdef_name" => $row["langdef_name"],
					"langdef_aliasname" => $row["langdef_aliasname"],
					"langdef_ordering" => $row["langdef_ordering"] );
			}
			return $retval;
		}

		public function unsetLanguageDefs( $language_id ) {
			FNTH_db_free_result(
				FNTH_db_query( sprintf( "delete from language_langdefs where language_id=%d" , $language_id ) ) );
		}
		
		public function unsetLanguageDef( $language_id , $site_language_id ) {
			FNTH_db_free_result(
				FNTH_db_query(
					sprintf( "delete from language_langdefs where language_id=%di and site_language_id=%d",
						$language_id,$site_language_id ) ) );
		}

		public function setLanguageDef( $language_id , $site_language_id , $data = array() ) {
			$query = sprintf( "select * from language_langdefs where site_language_id=%d and language_id=%d",
				$site_language_id , $language_id );
			$row = FNTH_db_get_row( $query );
			// prepare
			$d = array();
			$d["site_language_id"]=sprintf( "%d" , $site_language_id );
			$d["language_id"]=sprintf( "%d" , $language_id );
			$d["langdef_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $data["langdef_name"] ) );
			$q = ($row) ?
				FNTH_db_generate_update_query( "language_langdefs" , $d ,
				sprintf( "site_language_id=%d and language_id=%d" , $site_language_id , $language_id ) ) :
				FNTH_db_generate_insert_query( "language_langdefs" , $d );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}

		public function updateLanguage( $language_id , $data = array() ) {
			$d = array();
			$d["language_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $data["language_name"] ) );
			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_update_query( "languages" , $d , sprintf( "language_id=%d" , $language_id ) )
				)
			);
		}

		public function deleteLanguage( $language_id ) {
			FNTH_db_free_result(
				FNTH_db_query( sprintf( "select sp_delete_language( %d )" , $language_id ) ) );
			/*
			$this->unsetLanguageDefs( $language_id );
			FNTH_db_free_result(
				FNTH_db_query( sprintf( "delete from languages where language_id=%d" , $language_id ) ) );
			*/
		}

		public function setLanguageDefs( $language_id , $arr = array() ) {
			if (!is_array( $arr )) return;
			if (empty( $arr)) return;
			foreach ($arr as $site_language_id => $lang) {
				$this->setLanguageDef( $language_id , $site_language_id , $lang );
			}
		}

		public function setLanguageSorting( $language_id , $langids = array() ) {
			FNTH_db_begin_transaction();
			$i = 1;
			foreach ($langids as $langid) {
				$q = sprintf( "update language_langdefs
					set langdef_ordering=%d 
					where site_language_id=%d
					and language_id=%d",
					$i,
					$language_id,
					$langid );
				FNTH_db_free_result( FNTH_db_query( $q ) );
				$i++;
			}
			FNTH_db_commit_transaction();
		}

	}

?>
