<?php
	fo3_model( "Cache" );
	class CountiesModel {
		public static function getCountyOptions( $choose = true , $padding = true ) {
			$mc = CacheModel::getCacheNameOnParams( "county_options" ,
				array( "lang" => $GLOBALS["language"], "choose" => $choose , "padding" => $padding ) );
			$retval = CacheModel::getCacheValueOnString( $mc );
			if (is_array( $retval )) return $retval;
			$retval = array();
			if ($choose) $retval[""]=__("Choose");
			$query = sprintf( "select * from counties where county_id>0 order by meta_l" , $GLOBALS["languages"][$GLOBALS["language"]] );
			$data = FNTH_db_get_resultset( $query );
			foreach ($data as $row) {
				$retval[$row["county_id"]]=($padding) ?
					(str_repeat( "&nbsp;" , 3 * ($row["county_level"]-1) )) : "";
				$retval[$row["county_id"]].=$row["county_name"];
			}
			CacheModel::cacheValueOnString( $mc , $retval );
			return $retval;
		}

		public static function getCountyParentId( $county_id ) {
			return FNTH_db_get_field(
				sprintf( "select parentid from counties where county_id=%d" , $county_id ),
				"parentid" );
		}

		public static function getCountySubTree( $county_id , $itself = true ) {
			$query = sprintf( "select * from counties where county_id=%d" , $county_id );
			$row = FNTH_db_get_row( $query );
			$query = sprintf( "select county_id from counties where meta_l>%s%d and meta_r<%s%d",
				($itself) ? "=" : "",
				$row["meta_l"],
				($itself) ? "=" : "",
				$row["meta_r"] );
			return FNTH_db_get_column( $query , "county_id" );
		}
	}


?>
