<?php

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

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

<!--body start-->
	<?php
	//loader
	require('include/naver.admin.php');
	
	//菜单处理
	$action = getgpc('action');
	$jump = getgpc('jump');
	
	//下一步
	$next=true;
	
	$gid = getnum('id',0);	
	$name = getgpc('name');
	$state = getnum('state',0);
	$medal = getgpc("medal");
	$description = getgpc("description");
	
	if( $action ){

		//权限配置
		$config = $_POST['config'];
		$config = format_json( fix_json( $config ) );
		
		//快捷方式
		$quick = $_POST['quick'];
		$quick = serialize( $quick );
		
		//小工具
		$widget = $_POST['widget'];
		$widget = serialize( $widget );
		
	}
		
	//连接数据库
	System :: connect();
	
	//动作处理
	switch ($action){
		case "add":
		
			//检查权限
			$func = "sys-group-add";
			System :: check_func( $func, false );
				
			$sql="INSERT INTO `sys:group`(name,aid,account,state,description,dateline,config,quick,widget,medal) values('".$name."',".$_SESSION['Manager']['id'].",'".$_SESSION['Manager']['account']."',".$state.",'".$description."',".time().",'".$config."','".$quick."','".$widget."','".$medal."')";
			
			System :: $db -> execute( $sql );
			
			$id = System :: $db -> getInsertId();
					
			//写入日志
			System :: insert_event($func,time(),time(),"新增用户组：".$name);				
			
			//缓存系统用户组
			Cached :: table( 'system', 'sys:group', array( 'jsonde' => array('config') ) );
			
			System :: redirect("group.list.php","<b>消息:</b> 成功添加用户组!");
		
		break;
		
		case "update":
		
			//检查权限
			$func = "sys-group-mod";
			System :: check_func( $func, false );				
		
			//更新数据
			$sql="UPDATE `sys:group` SET name='".$name."',`state`='".$state."',description='".$description."',`modify`='".time()."',config='".$config."',quick='".$quick."',widget='".$widget."',medal='".$medal."' WHERE id=".$gid;
			
			System :: $db -> execute( $sql );
			
			//缓存系统用户组
			Cached :: table( 'system', 'sys:group', array( 'jsonde' => array('config') ) );
					
			//写入日志
			System :: insert_event($func,time(),time(),"修改用户组：".$name);				
			
			//重载权限
			$_SESSION["GroupLife"] = 0;
			
			//$_G['project']['message']="<b>消息:</b> 成功修改用户!";
			System :: redirect("group.list.php","成功修改用户组!");
		
		break;
		
		case "edit";

			$sql="SELECT * FROM `sys:group` WHERE id=".$gid;
			$row = System :: $db -> getOne( $sql );

			if( $row ){

				//权限配置
				$config = fix_json( $row['config'] );
				
				//快捷方式
				$quick = unserialize( $row['quick'] );
				
				//小工具
				$widget = unserialize( $row['widget'] );
				
			}
			
			//$CFG = $_CACHE['system']['group'][]
			//var_dump($config);
		
		break;
		
	}
	
	/////////////////////////
	
	//关闭数据库
	System :: connect();
	
	?>
    
    <script type="text/javascript">
	
	function doSelect(val){
		Mo('#medal').value(val+".png");
		
		Mo('#group li').attr( { "class" : "" } );
		
		Mo('#'+val).attr( {"class" : '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='group'>
			<?php
					
                    //读取图标		    
		    $base = VI_ROOT. 'image/medal/';
		    
		    $list = loop_file( $base, array(), array('png') );
		    
		    foreach( $list as $file ){
                            if( stripos($file,"mini")===false){				
				echo '<li id="'.str_replace(".png","",$file).'" onclick="doSelect(\''.str_replace(".png","",$file).'\');" class="active"><img src="'.VI_BASE.'image/medal/'.$file.'" /></li>';
				
				$icon = $row["medal"] ? $row["medal"] : $file;
                            }
                    }
                    
                    ?>
                    </ul>
                    </td>
                </tr>
            
                <tr>
                    <td><strong>后台登录：</strong></td>
                    <td>		
                       <label><input type="radio" class="radio" name="state" value="1" checked> 启用 </label>
                       <label><input type="radio" class="radio" name="state" 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($_G['project']['page'] 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($_G['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($_CACHE['system']['module'] as $key=>$val){
	
						//appid
						$appid = $key;
	
						if ($val["config"]) {
	
							//config
							$file = VI_ROOT.$val["config"];
	
							if( file_exists($file) ) require_once($file);
	
							$func = $_G['module'][$key]['func'];
	
							if( is_array($func) ){
							
								echo '<ul class="func">';

								echo '<li><strong>'.$_G['module'][$key]['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( $_CACHE['system']['module'] as $appid => $val ){

						if ($val["config"]) {

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

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

							$func = $_G['module'][$key]['func'];

							if( is_array($func) ){

								echo '<li><img src="'.VI_BASE.'module/'.$appid.'/icon.png" /><br /><strong><label> '.$_G['module'][$appid]['name'].'<br /><input type="checkbox" class="checkbox" '.( in_array( $appid, $quick ) ?'checked="checked"':'').' name="quick[]" value="'.$appid.'" /></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( $_CACHE['system']['module'] as $appid => $val ){

						if ($val["config"]) {

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

							if( file_exists($root) ){
	
								/////////////////////
								
								$app = $_CACHE['system']['module'][$appid];

								echo '<strong>'.$app["name"].'</strong>';	
	
								echo '<ul class="album">';
	
								//遍历目录
								if ($handle = opendir($root)) {
									while (false !== ($file = readdir($handle))) {
										if (($file  !=   ".") && ($file   !=   "..")) {
											
											$doc = $root.$file."/config.xml";
											
											//如果配置文件存在
											if( file_exists($doc) ){
												
												/*
	
												//皮肤名称
												$xml = new xmlParser();
													$xml -> xmlOpen( $root.$file."/config.xml" );							
													$config = $xml -> xmlToArray();
													$config = $config["WIDGET"];
		
												//UTF8 转 GBK
												if( $_G['product']['charset'] == "gbk" ){								
													foreach ($config as $key => $val){
														$config[$key] = iconv('UTF-8', 'GBK//IGNORE', $val);
													}							
												}
												*/
									
												/************* OR **************/
												
												$config = xml_array( sreadfile($doc) );
												
												//UTF8 转 GBK
												if( $_G['product']['charset'] == "gbk" ){								
													foreach ($config['widget'] as $key => $val){
														$config['widget'][$key] = $val ? iconv('UTF-8', 'GBK//IGNORE', $val) : $val;
													}							
												}
												
												////////////////////////////////
									
	
											//
											echo '<li rel="'.$file.'">';
	
											echo '<img src="'.VI_BASE.'module/'.$appid.'/widget/'.$file.'/preview.png" /> <br /> <label>'.$config['widget']["name"].'</label> <br /> ';
											
											echo '<input type="checkbox" class="checkbox" '.(in_array( $file, $widget[$appid] ) ?'checked="checked"':'').' name="widget['.$appid.'][]" value="'.$file.'" />';
	
											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=state]").value("<?php echo isset($row['state']) ? $row['state'] : 1;?>");
				doSelect('<?php echo str_replace(".png","",$icon);?>');
            </script>

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

<?php html_close();?>