<?php
require("../../inc/lib.inc");
/*************************************************
File name:  saveParameter.php
Author:     JianHui Zheng
Version:    9.00.00
Date:       2007-10-20
Description:这是一个后台程序，供operWebFunc.inc调用来完成参数的保存
			接收的参数
			$sysId
			$oidStr
			$instanceNo
			$newValue
Calls:		
Called:

History:
No.1:
Author:
Date:
Version:
Description:
*************************************************/
function getLevelWhere($objIdStr)
{
	$levelArr=explode('.',$objIdStr);
	for($j=0;$j<sizeof($levelArr);$j++){
		$levelNo=$j+1;
		${"level_$levelNo"}=$levelArr[$j];
		if($Debug) echo "<BR>levelArr[$j]={$levelArr[$j]}";
		if($j == 0){
			$levelWhere ="level_$levelNo='${"level_$levelNo"}'";
		}else{
			$levelWhere .=" AND level_$levelNo='${"level_$levelNo"}'";
		}
	}
	return($levelWhere);
}

function getNameByOID($sysTypeNo,$oidwhere)
{
	global $objDbConn;
	$tb="OBJ_".$sysTypeNo.".paramConf";
	$sqlstr="select name_2 from $tb where $oidwhere";
	$res = @mysqli_query( $objDbConn,$sqlstr);
	$row = @mysqli_fetch_array($res);
	return $row[name_2];
}

function adjustShowName($name)
{
	$len=strlen($name);
	$result="";
	
	if($name[0]>='a' && $name[0]<='z')
		$result.=chr(ord($name[0])-32);	
	for($i=1;$i<$len;$i++)
	{	
		if($i != 1)
			if($name[$i]>='A' && $name[$i]<='Z' && $name[$i-1]>='a' && $name[$i-1]<='z')
				$result.=" ";
			
		if($name[$i]>='A' && $name[$i]<='Z' && $name[$i+1]>='a' && $name[$i+1]<='z' && $name[$i-1]>='A' && $name[$i-1]<='Z')
			$result.=" ";
				
		$result.=$name[$i];	
		
		if(($name[$i]<'0' || $name[$i]>'9')&& ($name[$i+1]>='0' && $name[$i+1]<='9'))
			$result.=" ";
		if(($name[$i]>='0' && $name[$i]<='9')&& ($name[$i+1]<'0' || $name[$i+1]>'9'))
			$result.=" ";
	}
	
	$result=str_replace("Table","",$result);
	$result=str_replace("Entry","",$result);
	
	return $result;
}

function getOptVal($sysTypeNo,$oidwhere,$value)
{
	global $objDbConn;
	$tb="OBJ_".$sysTypeNo.".paramConf";
	$sqlstr="select * from $tb where $oidwhere";
	$res = @mysqli_query($objDbConn,$sqlstr);
	$row = @mysqli_fetch_array($res);
	
	if($row[operType] == 1 && $row[valueType] == 3 && strstr($row[setTemplate_2],"="))
	{
		$template=$row[setTemplate_2];
		if(trim($template) == "")
			return $value;
		$options=explode(";",$template);
		for($ii=0;$ii<sizeof($options);$ii++)
		{
			$tmp=explode("=",$options[$ii]);
			$val=$tmp[0];
			$opt=$tmp[1];
			if($value == $opt)
				return $val;
		}
	}
	else
	{
		$ret=$value; 
	}
	return $ret;
}
global $OMC_server;


$userName = $_REQUEST['userName'];
$sysId = $_REQUEST['sysId'];
$oidStr = $_REQUEST['oidStr'];
$instanceNo = $_REQUEST['instanceNo'];
$newValue = $_REQUEST['newValue'];
$paraTable = $_REQUEST['paraTable'];


	$newValue=urldecode($newValue);
	//system("echo ------------------------------------------------------ >>./temptemp.txt");
	//system("date >>./temptemp.txt");
	//system("echo sysId=$sysId oidStr=$oidStr instanceNo=$instanceNo newValue=($newValue) >>./temptemp.txt");
	
	

	
	$sysIdArr=explode('_',$sysId);
	$sysTypeNo=$sysIdArr[0];
	$sysNo=$sysIdArr[1];
	$subSysNo=$sysIdArr[2];

	$database='OBJ_'.$sysTypeNo;
	$objDbConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], $database);

	$paraConfTable=$database.'.paramConf';
	
	if(checkIndependSystem($sysTypeNo))//Plat
		$paraTable=$database.".param_".$sysNo;
	else
		$paraTable=$database.".param_99";

	
	
	$oidwhere=getLevelWhere($oidStr);
	$updSql = "UPDATE $paraTable SET initValue='$newValue' where $oidwhere and instanceNo='$instanceNo' ";

	//ISSUE-3
	if(checkPlatWhoHaveSubSysNo($sysTypeNo,$sysNo))
	{
		$updSql.="and subSysNo='$subSysNo' ";
	}
	
	mysqli_query($objDbConn,$updSql);
	//system("echo $updSql >>/tmp/temptemp.txt");
	//compare the initValue and readValue
	
	$selSql = "select initValue,readValue from $paraTable where $oidwhere and instanceNo='$instanceNo' ";
	
	if(checkPlatWhoHaveSubSysNo($sysTypeNo,$sysNo))
	{
		$selSql.="and subSysNo='$subSysNo' ";
	}
	
	$selRes = @mysqli_query($objDbConn,$selSql);
	$selRow = @mysqli_fetch_array($selRes);
	$initValue=$selRow[0];
	$readValue=$selRow[1];
	//echo "$initValue $readValue";

	if(strcmp($initValue,$readValue) == 0)
	{
		echo "same";
		$isSaveFlag=0;
	}
	else
	{
		echo "different";
		$isSaveFlag=1;
	}
	

	//insert log
	$preoidwhere=$oidwhere;
	$tmpstr=getNameByOID($sysTypeNo,$oidwhere);
	$tmplen=strlen($tmpstr);
	for($i=0;$i<$tmplen;$i++)
	{
		if($tmpstr[$i] >= 'A' && $tmpstr[$i] <= 'Z')
			break;
	}
	$fieldName=substr($tmpstr,$i);


	$oidwhere=getLevelWhere(substr($oidStr,0,strlen($oidStr)-2));
	$tableName=getNameByOID($sysTypeNo,$oidwhere);
	$tableName=str_replace("Entry","",$tableName);
	$sysName=getSystemNameBySysTypeNo($sysTypeNo);
	if($isSaveFlag)
	{
		$tableName=trim(adjustShowName($tableName));
		$readValue=getOptVal($sysTypeNo,$preoidwhere,$readValue);
		$initValue=getOptVal($sysTypeNo,$preoidwhere,$initValue);
		$log="Change $sysName: $tableName $instanceNo's $fieldName, $readValue -> $initValue";
		//$handle=fopen("/tmp/test.txt","a");
		//fwrite($handle,$oidStr);
		//fwrite($handle,"\n");
		//fclose($handle);
		insertLog($log);
	}
	

?>
