<?php
/*********************************************************      
 程序说明:         
    功能说明:本程序实现对数据表中的菜单结构的编辑
    调用关系:调用:header.inc     
    变量说明:
    返回值:无

 作者:Cui Ticun    2006-05-15      
      
修改注释:      
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 > Menu Configuration";
		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 = 1;
$pubDb			= 'OMC_PUB';
$menuTable		= 'sysMenuConf';
$elementTable	= 'elementExistConf';
$maxLevelNum	= 5;

//选择语言版本
if($selectedLanguage == 'eng'){
	$nameField="name_2"; //语种2
}elseif ($selectedLanguage == 'chn'){
	$nameField="name_1"; //语种1(默认语种)
}

/**************************************删除一个菜单***********************************************/
if($_POST['delete']){
	$delSql = "DELETE FROM $menuTable WHERE keyId = '$keyId'";
	$delResult = mysqli_query($pubConn ,$delSql);
	echo mysqli_error($pubConn);

	if($delResult){
		echo $strDeleteSuccess;
	 }else{
		echo $strDeleteFailure;
	}
} //end if($delete) 

/*****************************添加,修改菜单*******************************************/
if($_POST['save']){
	if($keyId == 'new'){//add
		$addSql = "REPLACE INTO $menuTable 
					(menuTypeNo,level_1,level_2,level_3,level_4,level_5,
						name_1,name_2,httpLink,privilege,remark_1,remark_2)
					VALUES
					('0','$_POST[level_1]','$_POST[level_2]','$_POST[level_3]','$_POST[level_4]','$_POST[level_5]',
					'$_POST[name_1]','$_POST[name_2]','$_POST[httpLink]','$_POST[privilege]','$_POST[remark_1]','$_POST[remark_2]')
					";
		if($DEBUG) echo "<br>$addSql";
		$result = mysqli_query($pubConn ,$addSql);
		echo mysqli_error($pubConn);
		
		if($result > 0){
			$getKeyIdSql =" SELECT keyId FROM $menuTable 
			WHERE level_1='$_POST[level_1]' AND level_2='$_POST[level_2]'
			AND level_3='$_POST[level_3]' AND level_4='$_POST[level_4]' AND level_5='$_POST[level_5]' AND privilege='$_POST[privilege]'
						  ";
			if($DEBUG) echo "<br>$getKeyIdSql";
			$result = mysqli_query($pubConn,$getKeyIdSql);
			echo mysqli_error($pubConn);
			$rows = mysqli_fetch_array($result);
			$keyId = $rows[$keyId];
			echo "<br>ADD OK";
		}else{
			echo "<br>ADD Failed";
		}
	}else{//update
		$updateSql = "UPDATE $menuTable SET  menuTypeNo='0', level_1='$_POST[level_1]', 
		level_2='$_POST[level_2]', level_3='$_POST[level_3]', level_4='$_POST[level_4]',
		level_5='$_POST[level_5]', privilege='$_POST[privilege]', httpLink='$_POST[httpLink]',
							name_1='$_POST[name_1]', name_2 = '$_POST[name_2]',remark_1 = '$_POST[remark_1]', remark_2 = '$_POST[remark_2]'
							WHERE keyId = '$_POST[keyId]'";
		if($DEBUG) 
		echo "<br>updateSql = $updateSql";
		$updateResult = mysqli_query($pubConn,$updateSql);
		echo mysqli_error($pubConn);
		if($updateResult){
			echo "<br>UPDATE OK";
		}else{
			echo "<br>UPDATE Failed";
		}
	}
}
/*****************************显示一个菜单的定义*****************************/
?>


<script LANGUAGE="JavaScript">  
function chgMenu()
{      
	var baseUrl,urlAdd,url;      
	baseUrl='<?php echo"$PHP_SELF?menuTypeNo=$menuTypeNo&keyId=";?>';
	urlAdd=document.confForm.keyId.options[document.confForm.keyId.selectedIndex].value;      
	//document.confForm.keyId.value=urlAdd;
	url = baseUrl+urlAdd;
	console.log(url)
	window.location=url;      
}
</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="#E6E6E6" > 
	<tr bgcolor="#E6E6E6">    
	<td width="100%" colspan="2" align="center">
		<font size=3><b><?php echo "OMC Menu Configuration"; ?></b></font>
	</td>
    </tr>
	<tr>
    <td width="30%">
		<?php echo "Select Menu:"; ?>
    </td>
    <td width="70%">
		
      <select size="1" name="keyId" onChange="javascript:chgMenu()">
		<?php
		$orderBy = '';
		for($i=1;$i<=$maxLevelNum;$i++){
			if($i < $maxLevelNum){
				$tempStr = ' level_'.$i.',';
			}else{
				$tempStr = ' level_'.$i;
			}
			$orderBy .= $tempStr;
		}
		
		$getMenuSql = "SELECT * FROM $menuTable ORDER BY $orderBy
                  ";
		$menuResult = mysqli_query($pubConn ,$getMenuSql);
		
		if($DEBUG) echo "<br>selected Id = $keyId"; 
		if($DEBUG) echo "<br>getMenuSql = $getMenuSql";
		if($DEBUG) echo mysqli_error($pubConn);
		$keyId=$_REQUEST['keyId'];
		//显示选择菜单类型下拉菜单
		echo"<option value=\"new\">------------------New------------------</option>";
		while($menuRows = mysqli_fetch_array($menuResult)){
			echo "<option value=\"$menuRows[keyId]\" ";
			if($menuRows['keyId'] == $keyId && $keyId > 0){
				echo "selected";
			}
			echo ">";
			$spaceStr = '';
			$oidStr='';
			for($i= $maxLevelNum; $i>=1; $i--){
				$levelEndId = 'level_'.$i;
				if($menuRows[$levelEndId] > 0){
					for($j=1;$j<=$i;$j++){
						$spaceStr .='&nbsp&nbsp';
						$levelBgnId = 'level_'.$j;
						$oidStr .=$menuRows[$levelBgnId].'.';
					}
					$spaceStr .='|--';
					break;
				}
			}
			echo "$spaceStr $oidStr $menuRows[$nameField]";
			echo"</option>";
		}
		?>
		</select>
		</td>
     </tr>
	 <?php
	 		$menuSql = "SELECT * FROM $menuTable WHERE keyId = '$keyId'";
			$menuResult = mysqli_query($pubConn ,$menuSql);
			$menuRows = @mysqli_fetch_array($menuResult);
	 ?>
	 <?php //------菜单oid----------- ?>
     <tr >
     <td width="30%"><?php echo "Menu 's Tree Id"?>:</td>
     <td width="70%">
     <?php
		for($i=1 ; $i <= $maxLevelNum; $i++){
			$levelId = 'level_'.$i;
			echo "<input type=\"text\" name=\"level_".$i."\" maxLength=2 size=2 value=\"$menuRows[$levelId]\">";
			if($i < $maxLevelNum){
				echo "&#8226;"; //dot
			}
		}
     ?>
	 </td>
     </tr>
	 <?php //------菜单名----------- ?>
	 <tr>    
     <td width="30%"><?php echo "Menu Name 1"; ?>:</td>
     <td width="70%"> 
		<input type="text" name="name_1" size="40" value="<?php echo"$menuRows[name_1]";?>"  >(Chinese)</td>
     </tr>
     <tr>    
     <td width="30%"><?php echo "Menu Name 2"; ?>:</td>
     <td width="70%">
		<input type="text" name="name_2" size="40" value="<?php echo"$menuRows[name_2]";?>" >(English)</td>
     </tr>
	 <?php //------链接----------- ?>
	 <tr>    
     <td width="30%"><?php echo "Http Link "; ?>:</td>
     <td width="70%"> 
		<input type="text" name="httpLink" size="50" value="<?php echo"$menuRows[httpLink]";?>" ></td>
     </tr>

	 <?php //------菜单描述----------- ?>
     <tr >
     <td width="30%"><?php echo "Menu Remark 1" ?>:</td>
     <td width="70%">
		<input type="text" name="remark_1" size="60" value="<?php echo"$menuRows[remark_1]";?>" >(Chinese)
     </td>  
     </tr>  
     <tr >
     <td width="30%"><?php echo "Menu Remark 2" ?>:</td>
     <td width="70%">
		<input type="text" name="remark_2" size="60" value="<?php echo"$menuRows[remark_2]";?>" >(English)
	 </td>  
     </tr>
	 
	 <tr>    
	 <td width="30%">
		<?php echo "Element type:" ?>
     </td>
	 <td width="70%">
		<select size="1" name="menuTypeNo">  
		<?php
		$tmpSql="SELECT * FROM $elementTable ORDER BY elementNo";
		if($DEBUG) echo "<br>menuSql = $menuSql";
		$tmpResult = @mysqli_query($pubConn,$tmpSql);
		echo mysqli_error($pubConn);
		while($tmpRow=mysqli_fetch_array($tmpResult)){
			echo "<option value=\"$tmpRow[elementNo]\" ";
			if($tmpRow['elementNo'] == $menuRows['menuTypeNo'] && isset($menuTypeNo)){
				echo"selected";
				$menuTypeName = $tmpRow['elementName'];
			}
			echo ">";
			echo "{$tmpRow['elementName']}";
			echo "</option>";
		}
		?>
		</select>
	 </td>
     </tr>
 
	 <?php //------权限类型----------- ?>	 
	 <tr>                   
     <td width="30%">Operation define:</td>                  
     <td width="70%"> 
		<?php 
		echo "<select name=\"privilege\" size=1>";
		
		for($i=0; $i < sizeof($privilegeDefArr); $i++){
			//if($privilegeDefArr[$i][show] <= 0) continue;
			echo "<option value =\"$i\" ";
			if($menuRows['privilege'] == $privilegeDefArr[$i][permit]){
				echo "  selected";
			}
			echo ">{$privilegeDefArr[$i]['remark']}</option>";
		}
		if($menuRows['privilege'] == -1){
			echo "<option value =\"-1\" selected>Disable</option>";
			$isDisableShowed=1;
		}else{
			echo "<option value =\"-1\">Disable</option>";
		}
		echo "</select>";
		?>

     </td>
     </tr>
	 </table>
<p>

<?php
	echo "<input type=\"submit\" value=\"$strSave\" name=\"save\"> ";
	echo "<input type=\"reset\" value=\"$strCancel\" name=\"reset\"> ";
	echo "<input type=\"submit\" value=\"$strDeleteMode\" name=\"delete\"> ";
?>
</p>
</form>
<?php
adjust_content_tail();
?>
</body>
</html>