<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id: veryide_menu.php,v2 19:47 2008-2-21 Leilei $
*/

require("../../include/header.php");
html_start("菜单管理 - VeryIDE");
?>

<!--body start-->
	<?php require("include/naver.system.php");?>
    
	
	<?php
	
	$do = GetQuery("do");
	
	//菜单处理
	$action = GetQuery("action");
	
	//保存提示
	$save=true;
	
	//菜单处理
	$id = GetNumber("id",-1);
	
	$status = GetNumber("status",1);
	
	$sort = GetNumber("sort",0);
	
	$name = GetQuery("name");
	$link = GetQuery("link");
	
	$parent = GetNumber("parent",0);
	$parent = $parent ? GetNumber("category",0) : $parent;
	
	//菜单状态
	function getMenustatus($id,$status){
		
		$option='';
		
		$option.='<label><input type="radio" class="radio" name="status-'.$id.'" value="1" '.( $status==1 ?'checked="true"':'').' /> 启用</label>';
		$option.='<label><input type="radio" class="radio" name="status-'.$id.'" value="0" '.( $status==0 ?'checked="true"':'').' /> 禁用</label>';

		return $option;
	}	

	//mysql open
	reader_mysql();

	switch ($action){
	
		//新增菜单
		case "add":
		
			//检查权限
			$func = "sys-menu-add";
			checker_func($func);		
			
			$sql="insert into ".VI_DBMANPRE."menu(name,parent,link,sort,attach,status,dateline) values('".$name."','".$parent."','".$link."','".$sort."','DIY',1,".time().")";
				
			mysql_query($sql,$mysql);
			
			$Project["queries"]++;
			
			$id = mysql_insert_id($mysql);
			
			//错误处理
			if (!$id) {
				trigger_error($sql, E_USER_ERROR);
				exit();
			}
			
			$Project["message"]="菜单添加成功!";			
					
			//写入日志
			event_insert($mysql,$func,time(),time(),"菜单添加成功：".$name."（".$link."）");
			
		break;
		
		//修改菜单
		case "update":
		
			//检查权限
			$func = "sys-menu-mod";
			checker_func($func);				
		
			$arr	= explode(",",$_POST["all"]);
			
			for( $i=0; $i<count($arr); $i++ ){
			
				$id = intval($arr[$i],0);
				$name = $_POST["name-".$id];
				
				if( $id>0 && $name ){
				
					$link=$_POST["link-".$id];
					$sort=$_POST["sort-".$id];
					$status = GetNumber("status-".$id,1);
			
					$sql="update ".VI_DBMANPRE."menu set name='".$name."',sort='".$sort."',link='".$link."',status='".$status."' where id=".$id;
					mysql_query($sql,$mysql);
					
					$Project["queries"]++;
					
					//echo $sql.'<br />';
				}
			}
					
			//写入日志
			event_insert($mysql,$func,time(),time(),"菜单修改成功：".$_POST["all"]);
			
		break;
			
		//删除菜单
		case "delete":
		
			//检查权限
			$func = "sys-menu-mod";
			checker_func($func);				
		
			//删除父菜单
			$sql="delete from ".VI_DBMANPRE."menu where id=".$id;
			mysql_query($sql,$mysql);
			
			//echo $sql;
			
			//删除子菜单
			$sql="delete from ".VI_DBMANPRE."menu where parent=".$id;
			mysql_query($sql,$mysql);
			
			$Project["queries"]+=2;
					
			//写入日志
			event_insert($mysql,$func,time(),time(),"菜单删除成功：".$id);
			
		break;
		
		//菜单状态
		case "status":
			
			$sql="update ".VI_DBMANPRE."menu set status=".$status." where id=".$id;
			mysql_query($sql);
			
			$Project["queries"]++;

		break;
		
	}
		
	if ($action){
		
		echo '<div id="state">系统菜单已经更新！<input name="submit" onclick=\'parent.location.reload(true)\' type="button" class="submit" value="显示新菜单" /></div>';
		
		//缓存系统菜单
		cache_table('system',$mysql,VI_DBMANPRE."menu",'<option value=\'{table:id}\'>{table:name}</option>'," sort asc ");	
	}
	
	//cache_menus($mysql);
	
	?>
	    
    <div id="box">

	<?php
	if($do=="new"){
	?>
    <form action="?" method="post" mode="edit" verify="true">
    
    <table cellpadding="0" cellspacing="0" class="form">
        
        <tr>
            <td width="60"><strong>菜单名称：</strong></td>
            <td>
           		<input name="name" type="text" class="text" id="name" size="35" fix_name="菜单名称" fix_null="yes" />
            </td>
        </tr>
        
        <tr>
            <td width="60"><strong>所属分类：</strong></td>
            <td>
                <label>
                <input type="radio" class="radio" name="parent" id="parent" value="0" onclick="setDisabled('category',true);setDisabled('link',true);" checked>主菜单</label>            
                    
				<?php
				
				if( count($VICACHE["sys.menus"]) ){
				
					echo '	
					
					<label>
						<input name="parent" id="parent" type="radio" class="radio" onclick="setDisabled(\'category\',\'\');setDisabled(\'link\',\'\');" value="1" checked>
						<select name="category" id="category">';
						
						foreach($VICACHE["sys.menus"] as $key){
							if($key["parent"]=="0"){							
								echo '<option value="'.$key["id"].'">'.$key["name"].'</option>';
							}
						}
						
						echo '
						
						</select>
					</label>';
					
					echo '<script>Mo("select[name=category]").value("'.$category.'");</script>';
				
				}
					
				?>
            </td>
        </tr>
         
        <tr>
            <td width="60"><strong>链接地址：</strong></td>
            <td>
           		<input name="link" type="text" class="text" id="link" size="35" />
            </td>
        </tr>
        
        <tr>
            <td width="60"><strong>菜单排序：</strong></td>
            <td>
           		<input name="sort" type="text" class="text" id="sort" value="1" size="12" title="从小到大" onKeyUp="value=value.replace(/[^\d]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
            </td>
        </tr>
        
        <tr>
            <td width="60"><strong>菜单状态：</strong></td>
            <td>
           		<label><input type="radio" class="radio" name="status" value="1" checked="true" /> 启用</label><label><input type="radio" class="radio" name="status" value="0"  /> 禁用</label>
            </td>
        </tr>
        
        <tr class="nobr">
            <td><strong></strong></td>
            <td>
                <input type="submit" name="Submit" value="添加菜单" class="submit" />
                <input type="hidden" name="action" id="action" value="add" />
            </td>
        </tr>
    
    </table>
   
    </form>
    
    <?php
	
	}else{
	
	?>

	<?php
	
    $sql="SELECT * FROM ".VI_DBMANPRE."menu where parent=0";

    if ($q && $s){$sql.=" and  ".$s." like '%".$q."%'";}
    
    if ($f){$sql.=" and  name like '%".$f."%'";}
    
    if ($start && $end){
    
        //开始时间
        $e_array=explode("-",$start);
        $t1=mktime(0, 0, 0, $e_array[1], $e_array[2], $e_array[0]);
        
        //到期时间
        $e_array=explode("-",$end);
        $t2=mktime(23, 59, 59, $e_array[1], $e_array[2], $e_array[0]);
        
        $sql.=" and  dateline >=".$t1." and dateline <= ".$t2;
    }

    $sql.=" order BY sort asc,id desc";
	
	//echo $sql;

    //查询数据库_总记录数
    $result=mysql_query($sql,$mysql);
	
	$Project["queries"]++;
    
    //echo $sql;
    ?>		
    
    <form action="" name="formPost" id="formPost"  method="post">
        <table width="100%" border="0" cellpadding="1" cellspacing="1" class="table">
          <tr class="title">
            <td>序号</td>
            <td>菜单名称</td>
            <td>菜单链接</td>
            <td>菜单状态</td>
            <td>添加时间</td>
            <td width="40">操作</td>
          </tr>

        <?php			
        
		//批量ID
        $all = array();

        while($row=mysql_fetch_assoc($result)){
            
			array_push($all,$row["id"]);

            echo ('<tr class="line">');
            echo ('<td width="5"><input type="text" class="text" name="m_'.$row["id"].'_order" size="4" value="'.$row["sort"].'" /></td>');
            //echo ("<td>".$Project["object"][$row["attach"]]."</td>");
            echo ('<td> <input type="text" class="text" name="m_'.$row["id"].'_name" size="30" value="'.$row["name"].'" /></td>');
            echo ("<td></td>");
            echo ('<td>'.getMenustatus($row["id"],$row["status"]).'</td>');
            echo ('<td>'.date("y-m-d",$row["dateline"]).'</td>');
            echo ('<td align="center">'.($row["attach"]=="DIY" || $Global["safemode"]=="on"?'<script type="text/javascript">Mo.Tools("RsDelete","?action=delete&id='.$row["id"].'","");</script>':'').'</td></tr>');
            
            //子菜单
            $sql="select * from ".VI_DBMANPRE."menu where parent=".$row["id"]." order by sort asc,id desc";
            $result2=mysql_query($sql,$mysql);
			
			$Project["queries"]++;
            
            if(mysql_num_rows($result2)>0){
            
                while($row2=mysql_fetch_assoc($result2)){
                
					//记录批量ID
					array_push($all,$row2["id"]);
                
                    echo '<tr class="line">
                            <td><input type="text" class="text" name="sort-'.$row2["id"].'" size="4" value="'.$row2["sort"].'" /></td>
                            <td>'.loader_image("tree.gif").' <input type="text" class="text" name="name-'.$row2["id"].'" size="22" value="'.$row2["name"].'" /></td>
                            <td><input type="text" class="text" name="link-'.$row2["id"].'" size="30" value="'.$row2["link"].'" /></td>
                            <td>'.getMenustatus($row2["id"],$row2["status"]).'</td>
							<td>'.date("y-m-d",$row2["dateline"]).'</td>
                            <td align="center">'.($row2["attach"]=="DIY" || $Global["safemode"]=="on"?'<script type="text/javascript">Mo.Tools("RsDelete","?action=delete&id='.$row2["id"].'","");</script>':'').'</td>
                            </tr>';
                }

            }
        }
        ?>
	</table>
    	<?php
	
		//有子选项时显示提交按钮
		if( count($all) > 0 ){		
		?>
		
        <input type="hidden" name="all" value="<?php echo implode(",",$all);?>" />
        <input name="action" id="action" type="hidden" value="update" />
        <input type="submit" class="submit" value="保存更改" />
		
		<?php
		}
		?>
    
	</form>
	
	<?php
	}
	
	//mysql close
	reader_mysql(true);
	
	?>
    
    </div>
    
<!--body end-->

<?php html_close();?>