<?php
/*********************************************************      
 程序说明:         
    功能说明:本程序实现参数编辑模板代码的产生，保存，预览

    调用关系:调用: header.inc     
    被调用:      
 
    变量说明:

    返回值:无

 作者:      
      
修改注释:      
NO.1      
姓名:            
时间:      
修改说明:      
*********************************************************/    
require("../../inc/header.inc");
?>
<body onload="javascript:adjust();" onresize="javascript:adjust();" leftmargin="15" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;">

<?php
adjust_head();
adjust_title_head();
echo "<table id=\"table_up\" border=\"0\" width=\"100%\">";
	echo "<tr>";
		echo "<td width=\"50%\">";
		echo "Template > Parameter > Edit";
		echo "</td>";
		echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
		//showHelp($helpId);
		echo "</td>";
	echo "</tr>";
echo "</table>";
adjust_title_tail();
adjust_content_head();
echo "<br>";

$DEBUG = 0;

function omcGetObjDb($sysTypeNo){
	$dbName = 'OBJ_'.$sysTypeNo;
	return($dbName);
}

function omcGetObjTbl($sysTypeNo,$tableType){
	$dbName = 'OBJ_'.$sysTypeNo;
	$tableName = $dbName.'.paramConf';
	return($tableName);
}
$sysConfTable = 'OMC_PUB.sysConf';
$sysTypeNo	= (isset($sysTypeNo)?$sysTypeNo:322);//default is CCF-MSC

$pubDb			='OMC_PUB';
$objDb 			= omcGetObjDb($sysTypeNo);
$paramConfTable	= omcGetObjTbl($sysTypeNo,paramConf);
$maxLevelNum	= 10;

if($DEBUG) echo "<br>$paramConfTable<br>";

//----------------------
//选择语言版本
if($language == 'eng'){
	$objNameField	= name_2;
	$objSetField	= setTemplate_2 ; //语种2
}else{	
	$objNameField	= name_1;
	$objSetField	= setTemplate_1 ; //语种1(默认语种)
}
//-----------------------

/*****************************保存一个新的参数编辑模板*****************************/
if($save){
	if($DEBUG) echo "<br>keyId = $keyId";
	if($keyId > 0){
		$saveSql = "UPDATE $paramConfTable 
					SET $objSetField = '$codeArea'
					WHERE  keyId='$keyId'
					";
		$saveResult = mysqli_query($pubConn,$saveSql);
	}else{
		echo "<br>Error: keyId == NULL";
		exit();
	}
	if($DEBUG) echo "<br>$saveSql";
	echo mysqli_error($pubConn);
	if($saveResult){
		//更新备份文件
		$csvFileName = '/usr/local/apache/htdocs/db_backup/other/'.$paramConfTable.'.csv';
		@unlink($csvFileName);
		$csvFileUpdateSql="	SELECT * 
				INTO OUTFILE '$csvFileName' 
				FIELDS TERMINATED BY ',' 
				LINES TERMINATED BY '\n' 
				FROM $paramConfTable 
			";
		$csvFileUpdateResult = mysqli_query($pubConn ,$csvFileUpdateSql);
		echo mysqli_error($pubConn);
		echo "<script LANGUAGE=\"JavaScript\">";  
		echo "alert('";
		echo "$strEditSuccess";
		echo "')";
		echo "</script>";
	}else{
		echo "<script LANGUAGE=\"JavaScript\">";  
		echo "alert('";
		echo "$strEditFailure";
		echo "')";
		echo "</script>";
	}
}
?>
<?php
/*****************************显示一个对象的编辑模板*****************************/
?>


<script LANGUAGE="JavaScript">  
function chgSysType(){      
	var baseUrl,urlAdd,url; 
	baseUrl='<?php echo"$PHP_SELF?sysTypeNo=";?>';
	urlAdd=document.confForm.sysTypeNo.options[document.confForm.sysTypeNo.selectedIndex].value;      
	url=baseUrl+urlAdd;
	window.location=url; 
}
function chgObj(){      
	var baseUrl,urlAdd,url;      
	baseUrl='<?php echo"$PHP_SELF?sysTypeNo=$sysTypeNo&keyId=";?>';
	urlAdd=document.confForm.keyId.options[document.confForm.keyId.selectedIndex].value;      
	url = baseUrl+urlAdd;
	window.location=url;      
}
function getOffset(){
	var offsetStr;
	var bgnOffset,endOffset;
	var bgnByte = document.confForm.bgnByte.options[document.confForm.bgnByte.selectedIndex].value;
	var bgnBit  = document.confForm.bgnBit.options[document.confForm.bgnBit.selectedIndex].value;
	var endByte = document.confForm.endByte.options[document.confForm.endByte.selectedIndex].value;
	var endBit  = document.confForm.endBit.options[document.confForm.endBit.selectedIndex].value;
	
	if(bgnBit >= 0){
		bgnOffset = bgnByte+"."+bgnBit;
	}else{
		bgnOffset = bgnByte;
	}

	if(endBit >= 0){
		endOffset = endByte+"."+endBit;
	}else{
		endOffset = endByte;
	}
	offsetStr = bgnOffset+"-"+endOffset;
	return(offsetStr);
}

function addInputField()
{
	var objName = document.confForm.objName.value;
	var unitName = document.confForm.unitName.value;
	var offsetStr = getOffset();
	var unitDef = document.confForm.inputEditCode.options[document.confForm.inputEditCode.selectedIndex].value;
	
	if(document.confForm.codeArea.value.length <= 0){
		document.confForm.codeArea.value += objName;
	}
	if(unitName.length <= 0){
		unitName += "unitName"; 
	}	
	document.confForm.codeArea.value += "\n[unit]"+unitName+"[u]"+offsetStr+"[u]input";
	document.confForm.codeArea.value += "\n[v]"+unitDef;
}

function addSelecArea()
{
	var objName = document.confForm.objName.value;
	var unitName = document.confForm.unitName.value;
	var offsetStr = getOffset();
	var unitDef = document.confForm.inputEditCode.options[document.confForm.inputEditCode.selectedIndex].value;
	var optNum = document.confForm.optNum.value;

	if(document.confForm.codeArea.value.length <= 0){
		document.confForm.codeArea.value += objName;
	}
	if(unitName.length <= 0){
		unitName += "unitName"; 
	}
	document.confForm.codeArea.value += "\n[unit]"+unitName+"[u]"+offsetStr+"[u]select";
	for(i=0;i<optNum;i++){
		document.confForm.codeArea.value += "\n[v]DecValue[opt]MenuName";
	}
}


function preview(sysTypeNo,objKeyId,initValue){
	URL="../../configuration/pubParamConf/paramEdit.php?TEST=1&sysTypeNo="+sysTypeNo+"&keyId="+objKeyId+"&setValue="+initValue;
	var msgWindow = open(URL, 'newWin', 'toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,status=yes,width=400,height=350');
	msgWindow.focus();
}


</script>
<?php 
//echo "\n<script language=\"javascript\" src=\"../../inc/expand.js\" > </script>";  
?>
<form method="POST" name="confForm" action="<?php echo"$PHP_SELF";?>" >
<table  border="1" width="100%" cellpadding="2" cellspacing="0"  bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF" > 
	<tr bgcolor="#E6E6E6">    
	<td width="100%" colspan="2" align="center">
		<font size=3><b><?php echo "Parameter's GUI Edit Design"; ?></b></font>
	</td>
    </tr>
	<tr>    
	<td width="20%">
		<?php echo $strSelectDeviceType; ?>
    </td>
    <td width="80%">
		<select size="1" name="sysTypeNo" onChange="javascript:chgSysType()">  
		<?php  
        //显示选择设备下拉菜单
		$sysTypeSql ="SELECT sysTypeNo,sysTypeName,snmpHead FROM $sysConfTable WHERE needParamConf ='1' ORDER BY sysTypeName";
		if($DEBUG) echo "<br>$sysTypeSql";
		$sysTypeResult = mysqli_query($pubConn,$sysTypeSql);
        $sysTypeRows = @mysqli_fetch_array($sysTypeResult);
        do{
      
      if($sysTypeRows[sysTypeNo] == 225 ||
      	 $sysTypeRows[sysTypeNo] == 260 ||
      	 $sysTypeRows[sysTypeNo] == 311 ||
      	 $sysTypeRows[sysTypeNo] == 320 ||
      	 $sysTypeRows[sysTypeNo] == 325 ||
      	 $sysTypeRows[sysTypeNo] == 330 ||
      	 $sysTypeRows[sysTypeNo] == 340 ||
      	 $sysTypeRows[sysTypeNo] == 350 ||
      	 $sysTypeRows[sysTypeNo] == 355 ||
      	 $sysTypeRows[sysTypeNo] == 375 ||
      	 $sysTypeRows[sysTypeNo] == 378 ||
      	 $sysTypeRows[sysTypeNo] == 379 ||
      	 $sysTypeRows[sysTypeNo] == 380 ||
      	 $sysTypeRows[sysTypeNo] == 385 ||
      	 $sysTypeRows[sysTypeNo] == 386 ||
      	 $sysTypeRows[sysTypeNo] == 390
      	 )
      continue;
      
			echo "<option value=\"$sysTypeRows[sysTypeNo]\" ";
			if($sysTypeRows[sysTypeNo] == $sysTypeNo && isset($sysTypeNo)){
				echo"selected";
				$snmpHeadStr = $sysTypeRows[snmpHead];
				$sysTypeName = $sysTypeRows[sysTypeName];
			}
			echo ">";
			echo "$sysTypeRows[sysTypeName]";
			echo "</option>";
		}while($sysTypeRows = mysqli_fetch_array($sysTypeResult));
		?>
		</select>
    </td>
    </tr>
	<tr>
    <td>
		<?php echo $strSelectParaType; ?>
    </td>
    <td>
		
      <select size="1" name="keyId" onChange="javascript:chgObj()">
		<?php
		$orderBy = '';
		for($i=1;$i<=$maxLevelNum;$i++){
			if($i < $maxLevelNum){
				$tempStr = ' level_'.$i.',';
			}else{
				$tempStr = ' level_'.$i;
			}
			$orderBy .= $tempStr;
		}
		
		$objSql = "SELECT * FROM $paramConfTable 
                   ORDER BY $orderBy
                  ";
		$objResult = mysqli_query($pubConn ,$objSql);
		
		//显示选择参数类型下拉菜单
		echo"<option value=\"new\">------------------New------------------</option>";
		while($objRows = mysqli_fetch_array($objResult)){
			echo "<option value=\"$objRows[keyId]\" ";
			if($objRows[keyId] == $keyId && $keyId > 0){
				echo "selected";
			}
			echo ">";
			$spaceStr = '';
			$oidStr='';
			for($i= $maxLevelNum; $i>=1; $i--){
				$levelEndId = 'level_'.$i;
				if($objRows[$levelEndId] > 0){
					for($j=1;$j<=$i;$j++){
						$spaceStr .='&nbsp&nbsp';
						$levelBgnId = 'level_'.$j;
						$oidStr .=$objRows[$levelBgnId].'.';
					}
					$spaceStr .='|--';
					break;
				}
			}
			echo "$spaceStr $oidStr $objRows[$objNameField]";
			echo"</option>";
		}
		?>
		</select>
		<?php 
		if($DEBUG) echo "<br>selected Id = $keyId"; 
		if($DEBUG) echo "<br>objSql = $objSql";
		if($DEBUG) echo mysqli_error($pubConn);
		?>
     </td>
     </tr>
 <?php
	$objSql = "SELECT $objNameField,$objSetField,length,initValue,valueType FROM $paramConfTable WHERE keyId = '$keyId'";
	$objResult	= mysqli_query($pubConn ,$objSql);
	$objRows	= @mysqli_fetch_array($objResult);
	$length		= $objRows[length];
	$objName	= $objRows[$objNameField];
	$initValue	= $objRows[initValue];
	$valueType  = $objRows[valueType];
	if($DEBUG) echo "<br>valueType=$valueType";
 ?>
	 
    <tr bgcolor="#E6E6E6">      
      <td colspan="2" width="100%">
	  <font size=2><b>Design Area</b></font>
	  <?php if($DEBUG) echo "<br>objSql=$objSql<br>";?>
	  <?php echo mysqli_error($pubConn);?>
	  </td>      
    </tr>   
    <?php //------参数名----------- ?>
	<tr>    
     <td>Name</td>
      <td><input type="text" name="objName" size="40" value="<?php echo "$objName";?>"></td>
    </tr>
    <?php //------参数偏移量----------- ?>
	<tr>
      <td>offset</td>
      <td>
      From <select size="1" name="bgnByte">                                                    
	<?php 
		if($valueType == '2'){
			echo "<option value=\"0\">0</option>";
		}else{
			for($i=0;$i<$length;$i++){
				$i = (strlen($i) < 2)?'0'.$i:$i;
				echo "<option value=\"$i\">$i</option>";
			}
		}
	?>
        </select>                                                
        byte                                                   
      <select size="1" name="bgnBit"><option value="0">0</option>                                                     
          <option value="1">1</option><option value="2">2</option>
		  <option value="3">3</option><option value="4">4</option>
		  <option value="5">5</option><option value="6">6</option>
		  <option value="7">7</option>
        </select>bit&nbsp;&nbsp;&nbsp;
      To<select size="1" name="endByte">                                                     
	<?php 
		if($valueType == '2'){
			$i=$length - 1;
			echo "<option value=\"$i\">$i</option>";
		}else{
			for($i=0;$i<$length;$i++){
				$i = (strlen($i) < 2)?'0'.$i:$i;
				echo "<option value=\"$i\">$i</option>";
			}
		}
	?>                                                    
        </select>byte                                                     
      <select size="1" name="endBit"><option value="7">7</option>                                                    
          <option value="6">6</option><option value="5">5</option>
		  <option value="4">4</option><option value="3">3</option>
		  <option value="2">2</option><option value="1">1</option>
		  <option value="0">0</option>                                                    
        </select>bit 
		<?php
		if($valueType == '2'){
			echo "(Value Type : Integer)";
		}else{
			echo "(Value Type : Octet String)";
		}
		?>
		</td>    
    </tr>
	<?php //------单元编辑名----------- ?>     
    <tr>
      	<td>Unit Name</td>
		<td><input type="text" name="unitName" size="30">                                        
      		<!--
			Type:<select size="1" name="unitType">                                              
          		<option value="others">others</option>                                        
          		<option value="telNum">Telephone number</option>                                              
          		<option value="ipAddr">IP address</option>                                      
        		</select>(need if oct string)
			-->
		</td>
		</tr>                                                   
    <?php //------输入框----------- ?>
	<tr> 
	<td>Input Field</td>
	<td> mode:
       <select size="1" name="inputEditCode">                                             
		<?php
		if($valueType == '4'){
			echo "<option value=\"toDec-high-0\">Turn to Dec and high fill '0'</option>";
			echo "<option value=\"keepHex-high-0\">Keep Hex and high fill '0'</option>";
			echo "<option value=\"keepHex-low-E\">Keep Hex and low fill 'E'</option>";
			echo "<option value=\"keepHex-low-F\">Keep Hex and low fill 'F'</option>";
			echo "<option value=\"toAscii-low-0\">Turn to Ascii and low fill 0</option>";
		}elseif($valueType == '2'){
			echo "<option value=\"keepDec-high-0\">Keep Dec and high fill '0'</option>";                 
			//echo "<option value=\"toHex-high-0\">To Hex and high fill '0'</option>";
		}else{
		  echo "<option value=\"keepHex-high-0\">Error:Unknown value type!!</option>";
		}
		?>                                
        </select> 
        <input type="button" value="Add input field" name="B2" onclick="javascript:addInputField()"></td>
	</tr>                                             
    <?php //------下拉菜单----------- ?>
	<tr>                                                                                                                               
      <td>Put down Menu</td>
	  <td>Option number:   
      <input type="text" name="optNum" size="5">  
      <input type="button" value="ok" name="B7" onclick="javascript:addSelecArea()">  
      Please change "decValue" and "menuName"
      </td>
	 </tr>                                                                                     
    <tr bgcolor="#E6E6E6">                                                 
      <td colspan="2" width="100%"><font size=2><b>Created Code</b></font></td>
	</tr>                                                 
	 <?php //------编辑代码----------- ?>
	<tr>                                                                                                                              
      <td colspan="2" width="100%"><textarea rows="10" name="codeArea" cols="75"><?php echo "$objRows[$objSetField]"?></textarea></td>
	</tr>               
</table>             
<br>                                                                              
  <input type="submit" value="Save" name="save">&nbsp;&nbsp;                                           
  <input type="Reset" value="Reset" >&nbsp;&nbsp;
  <input type="button" value="Preview" onclick="javascript:preview(<?php echo "'$sysTypeNo','$keyId','$initValue'";?>)">                                                        
  </form>
<hr>
说明：
<br>1.如果一个参数值由多个值组成，则可分成多个编辑单元，以便用户编辑。
<br>2.每个编辑单元，可制定不同的编辑方式（输入框或下拉菜单）。
<br>3.当使用下拉菜单时，需要替换代码区中的"[v]DecValue[opt]MenuName"中的DecValue（十进制值）和MenuName（菜单名）。
<br>4.如果某段数值预留不用，请用"[unit]reserved[u]bgnOffset-endOffset[u]select"标示。
<br>5.请注意，不能删除语法标签[unit],[u],[v],[opt],[remark]，同时也不要在说明中使用这些标签。
<br>6.生成的编辑代码保存后，可点击"Preview"来查看是否正确。
<?php
adjust_content_tail();
?>
</body>             
</html>
