<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id:veryide.profile.php ,v1 10:39 2008-10-16 Leilei $
*/

require("../../include/header.php");
html_start("用户组编辑 - VeryIDE");
?>

<!--body start-->
	<?php
	//loader
	loader_setting("naver.admin");
	
	//菜单处理
	$action = GetQuery("action");
	$jump = GetQuery("jump");
	
	//下一步
	$next=true;
	
	if(!$jump) $jump="admins.php";
	
	$gid = GetNumber("id",0);	
	$name = GetQuery("name");
	$status = GetNumber("status",0);
	$medal = GetQuery("medal");
	$description = GetQuery("description");
	
	if( $action ){

		//权限配置
		$config = $_POST["config"];
		$config = format_json( fix_json( $config ) );
		
		//快捷方式
		$quick = $_POST["quick"];
		$quick = serialize( $quick );
		
		//小工具
		$widget = $_POST["widget"];
		$widget = serialize( $widget );
		
	}
		
	//mysql open
	reader_mysql();
	
	//动作处理
	switch ($action){
		case "add":
		
			//检查权限
			$func = "sys-group-add";
			checker_func($func);
				
			$sql="insert into ".VI_DBMANPRE."group(name,aid,account,status,description,dateline,config,quick,widget,medal) values('".$name."',".$_SESSION["Manager"]["id"].",'".$_SESSION["Manager"]["account"]."',".$status.",'".$description."',".time().",'".$config."','".$quick."','".$widget."','".$medal."')";
			
			mysql_query($sql,$mysql);
			
			$Project["queries"]++;
			
			$id = mysql_insert_id($mysql);
			
			//错误处理
			if (!$id) {
				trigger_error($sql, E_USER_ERROR);
				exit();
			}
					
			//写入日志
			event_insert($mysql,$func,time(),time(),"新增用户组：".$name);				
			
			//缓存系统用户组
			cache_table('system',$mysql,VI_DBMANPRE."group",'<option value=\'{table:id}\'>{table:name}</option>');
			
			reader_redirect("group.list.php","<b>消息:</b> 成功添加用户组!");
		
		break;
		
		case "update":
		
			//检查权限
			$func = "sys-group-mod";
			checker_func($func);				
		
			//更新数据
			$sql="update ".VI_DBMANPRE."group set name='".$name."',status='".$status."',description='".$description."',modify='".time()."',config='".$config."',quick='".$quick."',widget='".$widget."',medal='".$medal."' where id=".$gid;
			
			mysql_query($sql,$mysql);
			
			$Project["queries"]++;
			
			//缓存系统用户组
			cache_table('system',$mysql,VI_DBMANPRE."group",'<option value=\'{table:id}\'>{table:name}</option>');
					
			//写入日志
			event_insert($mysql,$func,time(),time(),"修改用户组：".$name);				
			
			//重载权限
			$_SESSION["GroupLife"] = 0;
			
			//$Project["message"]="<b>消息:</b> 成功修改用户!";
			reader_redirect("group.list.php","成功修改用户组!");
		
		break;
		
		case "edit";

			$sql="select * from ".VI_DBMANPRE."group where id=".$gid;
			$result=mysql_query($sql,$mysql);
			
			$Project["queries"]++;
			
			if(mysql_num_rows($result)==1){
				$row=mysql_fetch_assoc($result);

				//权限配置
				$config = fix_json( $row["config"] );
				
				//快捷方式
				$quick = unserialize( $row["quick"] );
				
				//小工具
				$widget = unserialize( $row["widget"] );
				
			}
			
			//$CFG = $VICACHE["sys.group"][]
			//var_dump($config);
		
		break;
		
	}
	
	//echo $row["quick"];
	
	/////////////////////////
	
	//mysql close
	reader_mysql(true);
	
	$c_icon='001';
	
	?>
    
    <?php
	
	//读取配置
	
	$mod = realpath(dirname(__FILE__)."/../../image/medal/");
	
	if ($handle = opendir($mod)) {
		while (false !== ($file = readdir($handle))) {
			if(stripos($file,".png")){				
				$medal .= '"'.$file.'",';	
				$icon = $file;
			}
		}

		closedir($handle);
	}
	
	?>
    
    <script type="text/javascript">
	
	var medal = [<?php echo str_replace(array('",','""'),array('"','","'),$medal);?>];	
	
	Mo.tmpNum=0;
	
	function showIcon(n){
		Mo.tmpNum+=n;
		
		if(Mo.tmpNum<1){
			Mo.tmpNum=1;
		}
		
		if( Mo.tmpNum > medal.length ){
			Mo.tmpNum = medal.length;
		}
		
		
		Mo("#c_icon").value(medal[Mo.tmpNum]);
		Mo("#icon").attr( {"src" : 'image/medal/'+medal[Mo.tmpNum]} );
	}
	
	
	function setIcon(val){
		Mo('#medal').value(val);
		
		Mo('#groups li').attr( { "className" : "" } );
		
		Mo(val).attr( {"className" : 'active'} );
	}
	
	</script>
	
	<div id="box">
		<form action="?" method="post" mode="edit" verify="true">
                        
            <table cellpadding="0" cellspacing="0" class="form">
            
                <tr><td colspan="2" class="section"><strong>基本信息</strong></td></tr>
                
                <tr>
                    <td width="60"><strong>分组名称：</strong></td>
                    <td>
                    <input name="name" type="text" class="text" id="name" value="<?php echo $row["name"];?>" size="60" fix_name="用户名称" fix_null="yes" />
                    </td>
                </tr>
                
                <tr>
                    <td width="60"><strong>分组标识：</strong></td>
                    <td>                    
                    <ul id='groups'>
					<?php
					
                    //读取配置
                    
                    $mod = realpath(dirname(__FILE__)."/../../image/medal/");
                    
                    if ($handle = opendir($mod)) {
                        while (false !== ($file = readdir($handle))) {
                            if(stripos($file,".png") && stripos($file,"mini")===false){				
								echo '<li id="'.$file.'" onclick="setIcon(\''.$file.'\');" ><img src="'.VI_BASE.'image/medal/'.$file.'" /></li>';
                            }
                        }
                
                        closedir($handle);
                    }
                    
                    ?>
                    </ul>
                    </td>
                </tr>
            
                <tr>
                    <td><strong>后台登录：</strong></td>
                    <td>		
                       <label><input type="radio" class="radio" name="status" value="1" checked> 启用 </label>
                       <label><input type="radio" class="radio" name="status" value="0"> 禁用</label>
                    </td>
                </tr>
                    
                <tr><td colspan="2" class="section"><strong>系统页面</strong></td></tr>
                
                <tr>
                    <td><strong></strong></td>
                    <td id="block-pages">
                    
                    	<ul class="func">
                        
                        <?php
                        
						$i = 0;
                        foreach($Project["pages"] as $key => $value){
                            echo '<li><label><input type="checkbox" class="checkbox" '.($config[$key]=='Y'?'checked="checked"':'').' name="config['.$key.']" value="Y" /> '.$value.'</label></li>';
							
							if( $i>=6 ){
								echo '<br />';
								$i=0;
							}else{							
								$i++;
							}
                        }
                        
                        ?>
                        
                        </ul>
                        
                    </td>
                </tr>
                
                <tr>
                    <td><strong></strong></td>
                    <td>
                        <a href="javascript:Mo('#block-pages input').checked(true);void(0);">全选</a> / <a href="javascript:Mo('#block-pages input').checked(false);void(0);">不选</a> / <a href="javascript:Mo('#block-pages input').checked();void(0);">反选</a>
                    </td>
                </tr>
                    
                <tr><td colspan="2" class="section"><strong>系统权限</strong></td></tr>
                
                <tr>
                    <td><strong></strong></td>
                    <td id="block-system">
                        
                        <?php
                        $i = 0;
                        foreach($Project["func"] as $group => $array){
							
							echo '<ul class="func">';
							
							echo '<li><strong>'.$group.'：</strong></li>';
							
							foreach($array as $key => $value){
							
                            	echo '<li><label><input type="checkbox" class="checkbox" '.($config['sys-'.$key]=='Y'?'checked="checked"':'').' name="config[sys-'.$key.']" value="Y" /> '.$value.'</label></li>';
							
							}
							
							echo '</ul>';
							
                        }
                        
                        ?>
                        
                    </td>
                </tr>
                
                <tr>
                    <td><strong></strong></td>
                    <td>
                        <a href="javascript:Mo('#block-system input').checked(true);void(0);">全选</a> / <a href="javascript:Mo('#block-system input').checked(false);void(0);">不选</a> / <a href="javascript:Mo('#block-system input').checked();void(0);">反选</a>
                    </td>
                </tr>
				
				<tr><td colspan="2" class="section"><strong>模块权限</strong></td></tr>

				<tr>
					<td><strong></strong></td>
					<td id="block-module">
                
					<?php
	
					//读取配置
	
					foreach($VICACHE["sys.module"] as $key=>$val){
	
						//appid
						$appid = $key;
	
						if ($val["config"]) {
	
							//config
							$file = VI_ROOT.$val["config"];
	
							if( file_exists($file) ) require_once($file);
	
							$func = $Modules[$appid]["func"];
	
							if( is_array($func) ){
							
								echo '<ul class="func">';

								echo '<li><strong>'.$Modules[$appid]["name"].'：</strong></li>';
	
								foreach($func as $item => $name){

									echo '<li><label><input type="checkbox" class="checkbox" '.($config['mod-'.$item]=='Y'?'checked="checked"':'').' name="config[mod-'.$item.']" value="Y" /> '.$name.'</label></li>';

								}
	
								echo '</ul>';
	
							}
	
						}
					}
	
					?>
	
					</td>
				</tr>

				<tr>
					<td><strong></strong></td>
					<td>
						<a href="javascript:Mo('#block-module input').checked(true);void(0);">全选</a> / <a href="javascript:Mo('#block-module input').checked(false);void(0);">不选</a> / <a href="javascript:Mo('#block-module input').checked();void(0);">反选</a>
					</td>
				</tr>
				
				<tr><td colspan="2" class="section"><strong>快捷方式</strong></td></tr>

				<tr>
					<td><strong></strong></td>
					<td id="block-quick">

					<?php

					//读取配置
					
					echo '<ul class="album">';
					
					$i = 0;

					foreach($VICACHE["sys.module"] as $key=>$val){

						//appid
						$appid = $key;

						if ($val["config"]) {

							//config
							$file = VI_ROOT.$val["config"];

							if( file_exists($file) ) require_once($file);

							$func = $Modules[$appid]["func"];

							if( is_array($func) ){

								echo '<li><img src="'.VI_BASE.'/module/'.$appid.'/icon.png" /><br /><strong><label><input type="checkbox" class="checkbox" '.( in_array( $appid, $quick ) ?'checked="checked"':'').' name="quick[]" value="'.$appid.'" /> '.$Modules[$appid]["name"].'</label></strong></li>';
								
								if( $i == 8 ){									
									echo '</ul><ul class="album">';
									
									$i = 0;
								}else{
									$i++;
								}

							}

						}
					}
					
					echo '</ul>';

					?>

					</td>
				</tr>

				<tr>
					<td><strong></strong></td>
					<td>
						<a href="javascript:Mo('#block-quick input').checked(true);void(0);">全选</a> / <a href="javascript:Mo('#block-quick input').checked(false);void(0);">不选</a> / <a href="javascript:Mo('#block-quick input').checked();void(0);">反选</a>
					</td>
				</tr>
				
				<tr><td colspan="2" class="section"><strong>小工具</strong></td></tr>

				<tr>
					<td><strong></strong></td>
					<td id="block-widget">

					<?php
					
					//XML 处理类
					require(VI_ROOT."/class/xmlparser.php");

					//读取配置
					foreach($VICACHE["sys.module"] as $key=>$val){

						//appid
						$appid = $key;

						if ($val["config"]) {

							//插件目录
							$root = VI_ROOT."/module/".$appid."/widget/";

							if( file_exists($root) ){
	
								/////////////////////
								
								$app = $VICACHE["sys.module"][$appid];

								echo '<strong>'.$app["name"].'</strong>';	
	
								echo '<ul class="album">';
	
								//遍历目录
								if ($handle = opendir($root)) {
									while (false !== ($file = readdir($handle))) {
										if (($file  !=   ".") && ($file   !=   "..")) {
	
											//皮肤名称
											$xml = new xmlParser();
												$xml -> xmlOpen( $root.$file."/config.xml" );							
												$config = $xml -> xmlToArray();
												$config = $config["WIDGET"];
	
											//UTF8 转 GBK
											if( $Project["charset"] == "gbk" ){								
												foreach ($config as $key => $val){
													$config[$key] = iconv('UTF-8', 'GBK//IGNORE', $val);
												}							
											}
	
											//
											echo '<li rel="'.$file.'">';
	
											echo '<img src="'.VI_BASE.'module/'.$appid.'/widget/'.$file.'/preview.png" /> <br /> '.$config["NAME"].' <br /> ';
											
											//echo $widget[$appid];
											
											echo '<label><input type="checkbox" class="checkbox" '.(in_array( $file, $widget[$appid] ) ?'checked="checked"':'').' name="widget['.$appid.'][]" value="'.$file.'" /></label>';
	
											echo '</li>';
	
											$i++;
										}
									}
									closedir($handle);
								}
	
								echo '</ul>';
	
								
								/////////////////////
							
							}

						}
					}

					?>

					</td>
				</tr>

				<tr>
					<td><strong></strong></td>
					<td>
						<a href="javascript:Mo('#block-widget input').checked(true);void(0);">全选</a> / <a href="javascript:Mo('#block-widget input').checked(false);void(0);">不选</a> / <a href="javascript:Mo('#block-widget input').checked();void(0);">反选</a>
					</td>
				</tr>
        
                <tr class="nobr">
                    <td><strong></strong></td>
                    <td>
                    <?php
                    
                    if($action=="edit" || $action=="update" ){
                        echo '
                        <input type="submit" name="Submit" value="修改分组" class="submit '.($disabled?'disabled="true"':'').'" />
                        <input type="hidden" name="action" id="action" value="update" />
                        <input type="hidden" name="id" id="id" value="'.$row["id"].'" />
                        ';
                    }else{
                        echo '
                        <input type="submit" name="Submit" value="添加分组" class="submit" '.($disabled?'disabled="true"':'').' />
                        <input type="hidden" name="action" id="action" value="add" />
                        ';
                    }
                    echo '<input type="hidden" name="jump" id="jump" value="'.$_GET["jump"].'" />';
                    
                    ?>
                    <input type="hidden" name="medal" id="medal" value="" />
                    </td>
                </tr>
            
            </table>
            
            <script type="text/javascript">
                Mo("input[name=status]").value("<?php echo $row["status"];?>");				
				setIcon('<?php echo $row["medal"]?$row["medal"]:$icon;?>');
            </script>

		</form>
	</div>
<!--body end-->

<?php html_close();?>