<?php
$MAX_STEP_COUNT=24;
function GetMibSystem()
{
	$mibSystem[SystemCount]=0;
	$sql="select T1.sysTypeNo,T2.sysTypeName,T1.entryOid,T2.maxSysNum,T2.maxSubSysNum from OMC_PUB.sysMibConf as T1, OMC_PUB.sysConf as T2 where T1.isEnable=1 and T1.sysTypeNo=T2.sysTypeNo ";
	//echo "$sql<br>";
	$res=mysqli_query($pubConn,$sql);
	$mibSystem[SystemCount]=mysqli_num_rows($res);
	if($mibSystem[SystemCount] == 0)
		return $mibSystem;

	for($i=0;$i<$mibSystem[SystemCount];$i++)
	{
		$row=@mysqli_fetch_array($res);
		$mibSystem[$i][SysTypeNo]=$row[sysTypeNo]+0;
		$mibSystem[$i][SysTypeName]=$row[sysTypeName];
		$mibSystem[$i][EntryOID]=$row[entryOid];
		$mibSystem[$i][MaxSysNum]=$row[maxSysNum];
		$mibSystem[$i][MaxSubSysNum]=$row[maxSubSysNum];
	}
	return $mibSystem;
}

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 getOIDByRow($row)
{
	$oid="";
	for($i=1;$i<=10;$i++)
	{
		$level="level_".$i;
		if($row[$level] == 0)
		{
			break;
		}
		else
		{
			$oid.=$row[$level].".";
		}
	}
	$oid=substr($oid,0,strlen($oid)-1);
	return $oid;
}

function adjustTableName($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 GetTabConf($sysTypeNo)
{
	$sql="select * from OBJ_{$sysTypeNo}.paramConf where maxAccess=0 and name_2 <> 'parameter' order by level_1,level_2,level_3,level_4,level_5,level_6,level_7,level_8,level_9,level_10 ";
	$res=@mysqli_query($pubConn,$sql);
	$num=@mysqli_num_rows($res);
	//echo "$sql<br>";
	for($i=0;$i<$num;$i++)
	{
		$row=@mysqli_fetch_array($res);
		$tabConf[$i][Name]=adjustTableName($row[name_2]);
		$tabConf[$i][OID]=getOIDByRow($row);
		
		$checkoid=$tabConf[$i][OID].".1.1.0";
		$checkwhere=getLevelWhere($checkoid);
		$checksql="select desc_2 from OBJ_{$sysTypeNo}.paramConf where $checkwhere";
		$checkres=@mysqli_query($pubConn,$checksql);
		$checknum=@mysqli_num_rows($checkres);
		if($checknum == 1)
		{
			$checkrow=@mysqli_fetch_array($checkres);
			$desc=$checkrow[desc_2];
			if(strstr($desc,"Name:"))
			{
				$new_name=explode("Name:",$desc);
				$tabConf[$i][Name]=str_replace(";","",$new_name[sizeof($new_name)-1]);
			}
		}
		
	}
	if($num == 0)
		return 0;
	return $tabConf;
}

function GetWizardList()
{
	$sql="select * from WIZARD_DB.wizardList order by instance ";
	$res=@mysqli_query($pubConn,$sql);
	$num=@mysqli_num_rows($res);
	$wizardList[WizardCount]=$num;
	if($wizardList[WizardCount] == 0)
		return $wizardList;
	for($i=0;$i<$num;$i++)
	{
		$row=@mysqli_fetch_array($res);
		$wizardList[$i][WizardTitle]=$row[func_title];
		$wizardList[$i][WizardInstance]=$row[instance];
	}
	return $wizardList;
}

function GetWizard($wizard_instance)
{
	$sql="select * from WIZARD_DB.wizardList where instance=$wizard_instance ";
	$res=@mysqli_query($pubConn,$sql);
	$num=@mysqli_num_rows($res);
	if($num == 0)
		return 0;
	
	$row=@mysqli_fetch_array($res);
	$wizard[WizardTitle]=$row[func_title];
	$wizard[WizardDescription]=$row[func_desc];
	
	return $wizard;
}

function GetStepList($wizard_instance)
{
	$sql="select * from WIZARD_DB.stepList where wizard_instance={$wizard_instance} order by step_order ";
	$res=@mysqli_query($pubConn,$sql);
	$num=@mysqli_num_rows($res);
	$stepList[StepCount]=$num;
	if($stepList[StepCount] == 0)
		return $stepList;
	for($i=0;$i<$num;$i++)
	{
		$row=@mysqli_fetch_array($res);
		$stepList[$i][StepIndex]=$row[step_order]+1;
		$stepList[$i][StepTitle]=$row[step_title];
		$stepList[$i][StepDesc]=$row[step_desc];
		$stepList[$i][RelatedSystemTypeNo]=$row[related_system]+0;
		$stepList[$i][RelatedTableOID]=$row[related_table];
	}
	return $stepList;
}

function discardReturn($content)
{
	$tmpstr=$content;
	$tmplen=strlen($tmpstr);
	$newstr="";
	for($j=0;$j< $tmplen;$j++)
	{
		if(ord($tmpstr[$j]) != 10 && ord($tmpstr[$j]) != 13)
			$newstr.=$tmpstr[$j];
	}
	return $newstr;
}

?>

