<?php
	fo3_model( "Cache" );

	class CountriesModel {
		public static function getCountryOptions( $choose = true , $lang = 0 ) {
			if (!$lang) $lang = $GLOBALS["languages"][$GLOBALS["language"]];
			$mc = CacheModel::getCacheNameOnParams( "country_options" ,
				array( "lang" => $lang, "choose" => $choose ) );
			$retval = CacheModel::getCacheValueOnString( $mc );
			if (is_array( $retval )) return $retval;
			$retval = array();
			if ($choose) $retval[""]=__("Choose");
			// a 220. language trinidad & tobago, ezert szlovennak ertekeli ez a fostalicska google translatebar
			$query = sprintf( "select country_id,langdef_name from country_langdefs where language_id=%d order by langdef_name" , $lang );
			$data = FNTH_db_get_resultset( $query );
			foreach ($data as $row)
				$retval[$row["country_id"]]=$row["langdef_name"];
			CacheModel::cacheValueOnString( $mc , $retval );
			return $retval;
		}
		
		public function getCountryFromId( $country_id ) {
			$query = sprintf( "select * from countries where country_id='%s'" ,
				FNTH_db_addslashes( $country_id ) );
			return FNTH_db_get_row( $query );
		}
		
		public function getLanguageDefsFromId( $country_id , $language_ids = array() ) {
			$instring = (empty( $language_ids )) ? "" : implode( "," , $language_ids );
			$query = sprintf( "select * from country_langdefs where country_id='%s' %s",
				FNTH_db_addslashes( $country_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["language_id"]]=array(
					"langdef_name" => $row["langdef_name"] );
			}
			return $retval;
		}

		public function updateCountry( $country_id , $datas ) {
			$upd = array();
			// $upd["country_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $country_id ) );
			$upd["country_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $datas["country_name"] ) );
			$query = FNTH_db_generate_update_query( "countries" , $upd , sprintf( "country_id='%s'",FNTH_db_addslashes( $country_id ) ) );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function deleteCountry( $country_id ) {
			$query=sprintf("delete from country_langdefs where country_id='%s'",FNTH_db_addslashes($country_id ));
			FNTH_db_free_result( FNTH_db_query( $query ) );
			$query=sprintf("delete from countries where country_id='%s'",FNTH_db_addslashes($country_id ));
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}
		
		public function addCountry( $datas ) {
			$ins = array();
			$ins["country_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $datas["country_id"] ) );
			$ins["country_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $datas["country_name"] ) );
			$query = FNTH_db_generate_insert_query( "countries" , $ins );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			return $datas["country_id"];
		}

		public function setLanguageDefs( $country_id , $arr = array() ) {
			if (!is_array( $arr )) return;
			if (empty( $arr)) return;
			foreach ($arr as $site_language_id => $lang) {
				$this->setLanguageDef( $country_id , $site_language_id , $lang );
			}
		}
		
		public function setLanguageDef( $country_id , $site_language_id , $data = array() ) {
			$query = sprintf( "select * from country_langdefs where language_id=%d and country_id='%s'",
				$site_language_id , FNTH_db_addslashes( $country_id ) );
			$row = FNTH_db_get_row( $query );
			// prepare
			$d = array();
			$d["language_id"]=sprintf( "%d" , $site_language_id );
			$d["country_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $country_id ) );
			$d["langdef_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $data["langdef_name"] ) );
			$q = ($row) ?
				FNTH_db_generate_update_query( "country_langdefs" , $d ,
				sprintf( "language_id=%d and country_id='%s'" , $site_language_id , FNTH_db_addslashes( $country_id ) ) ) :
				FNTH_db_generate_insert_query( "country_langdefs" , $d );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}
	}

?>
