<?php

	class LangdefsModel {

		public function unsetLangdefDef( $language_id , $defname ) {
			$query = sprintf( "delete from ldef_defs where site_language_id=%d and ldef_name='%s'",
				$language_id , FNTH_db_addslashes( $defname ) );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			$query = sprintf( "select count(*) as num from ldef_defs where ldef_name='%s'" , FNTH_db_addslashes( $defname ) );
			$num = FNTH_db_get_field( $query , "num" );
			if ($num) return;
			$query = sprintf( "delete from ldefs where ldef_name='%s'" , FNTH_db_addslashes( $defname ) );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function addSetLangdefDef( $language_id , $defname , $defvalue ) {
			if (!strlen( trim( $defname ))) return;
			if (!strlen( trim( $defvalue ))) {
				$this->unsetLangdefDef( $language_id , $defname );
				return;
			}

			$query = sprintf( "select * from ldefs where ldef_name='%s'",
				FNTH_db_addslashes( $defname ) );
			$row = FNTH_db_get_row( $query );
			// insert normal
			$ins = array();
			if (!$row) {
				$ins["ldef_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $defname ) );
				$q = FNTH_db_generate_insert_query( "ldefs" , $ins );
				FNTH_db_free_result( FNTH_db_query( $q ) );
			}

			$query = sprintf( "select * from ldef_defs where site_language_id=%d and ldef_name='%s'",
				$language_id , FNTH_db_addslashes( $defname ) );
			$row = FNTH_db_get_row( $query );
			if (!$row) {
				$ins["ldef_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $defname ) );
				$ins["site_language_id"]=sprintf( "%d" , $language_id );
				$ins["def"]=sprintf( "'%s'" , FNTH_db_addslashes( $defvalue ) );
				$q = FNTH_db_generate_insert_query( "ldef_defs" , $ins );
				FNTH_db_free_result( FNTH_db_query( $q ) );
			}
		}

		public function getLangdefFromId( $ldef_name ) {
			$query = sprintf( "select * from ldefs where ldef_name='%s'" , FNTH_db_addslashes( $ldef_name ) );
			$row = FNTH_db_get_row( $query );
			return $row;
		}

		public function getLangdefDefsFromId( $ldef_name , $language_ids = array() ) {
			$instring = (empty( $language_ids )) ? "" : implode( "," , $language_ids );
			$query = sprintf( "select * from ldef_defs where ldef_name='%s' %s",
				FNTH_db_addslashes( $ldef_name ),
				(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(
					"ldef_name" => $row["ldef_name"],
					"def" => $row["def"] );
			}
			return $retval;
		}

		public function addLangdef( $datas ) {
			if (!isset( $datas["ldef_name"] )) return;
			$ldef_name = trim( $datas["ldef_name"] );
			if (!strlen( $ldef_name )) return;
			$query = sprintf( "select * from ldefs where ldef_name='%s'" , FNTH_db_addslashes( $ldef_name ) );
			$row = FNTH_db_get_row( $query );
			if ($row) return;
			$ins = array();
			$ins["ldef_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $ldef_name ) );
			$q = FNTH_db_generate_insert_query( "ldefs" , $ins );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}

		public function setLangdefDefs( $ldef_name , $datas ) {
			$query = sprintf( "delete from ldef_defs where ldef_name='%s'" , FNTH_db_addslashes( $ldef_name ) );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			foreach ($datas as $site_language_id => $data ) {
				$this->addSetLangdefDef( $site_language_id , $ldef_name , $data["def"] );
			}
		}

		public function deleteLangdef( $ldef_name ) {
			$query = sprintf( "delete from ldef_defs where ldef_name='%s'" , FNTH_db_addslashes( $ldef_name ) );
			FNTH_db_free_result( FNTH_db_query( $query ) );
			$query = sprintf( "delete from ldefs where ldef_name='%s'" , FNTH_db_addslashes( $ldef_name ) );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}
	}
?>
