<?php

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

require("../../include/header.php");
html_start("表单数据 - VeryIDE");
?>

<!--body start-->
	<?php

	//module
	//require("naver.php");
	require("config.php");

	$q = GetQuery("q");
	$s = GetQuery("s");
	
	$id = GetNumber("id",0);
	$list = GetQuery("list");
	$status = GetNumber("status",1);
	$order = GetQuery("order");
	$action = GetQuery("action");
	
	//mysql open
	reader_mysql();

	if ($action){
	
		switch($action){
			
			case "update":
			
				//检查权限
				checker_func("mod-form-mod");
				
	
				//用户信息
				$sql="SELECT * FROM ".VI_DBMODPRE."values where id=".$id;
				$result=mysql_query($sql,$mysql);				
				
				if( mysql_num_rows($result)==1 ){
					$row = mysql_fetch_assoc($result);
					
					//删除空白字符
					$row["config"] = preg_replace("/([\r\n\t\f])/"," ",$row["config"]);
					
					//兼容GBK中文
					if( $Project["charset"] == "gbk" ){
						$array = json_decode(iconv('GBK', 'UTF-8', $row["config"]),true);
						
						$config = array();
						
						if( is_array($array) ){
							foreach ($array as $key => $val){
								$config[$key] = iconv('UTF-8', 'GBK', $val);
							}
						}
					}else{
						//配置
        $config = fix_json( $row["config"] );	
					}
					
					//var_dump($config);
					
					//写入新值
					foreach( $config as $temp => $value ){
						if( isset($_POST[$temp]) ){
				
							//兼容GBK中文
							if( $Project["charset"] == "gbk" ){
								$config[$temp] = iconv('GBK','UTF-8', $_POST[$temp]);
							}else{
								$config[$temp] = $_POST[$temp];
							}							
							
						}
					}
				
					//记录用户提交数据	
					$config = format_json(json_encode($config));
					//var_dump($config);
					
					$sql="update ".VI_DBMODPRE."values set config='".$config."' where id = ".$id;
					mysql_query($sql);
					
				}
				
				reader_redirect("?action=message&id=".$id,"用户数据修改成功!");

			break;
			
			//显示消息，关闭窗口
			case "message":
				
				exit('<script>parent.Mo.Message("message","","用户数据修改成功!",3,"");parent.window.frames["window-iframe"].location.reload();remove(parent.$("dialog"));</script>');
			
			break;
			
		}
	}
	
	//用户信息
    $sql="SELECT * FROM ".VI_DBMODPRE."values where id=".$id;
    $result=mysql_query($sql,$mysql);
	
	//echo $sql;
	
    $Project["queries"]++;
    
    if( mysql_num_rows($result)==1 ){
        $row = mysql_fetch_assoc($result);
		
		//删除空白字符
		$row["config"] = preg_replace("/([\r\n\t\f])/"," ",$row["config"]);
		
		//兼容GBK中文
		if( $Project["charset"] == "gbk" ){
			$array = json_decode(iconv('GBK', 'UTF-8', $row["config"]),true);
			
			$config = array();
			
			if( is_array($array) ){
				foreach ($array as $key => $val){
					$config[$key] = iconv('UTF-8', 'GBK', $val);
				}
			}
		}else{
			//配置
        $config = fix_json( $row["config"] );	
		}
		
    }
	
	//加载缓存
	if( !loader_cache($appid,"forms.".$row["fid"]) ){
		exit('Cache Error!');
	}
	
	//var_dump($row);
    ?>
    
    <div id="nav">
        <strong><?php echo $VICACHE["mod.forms"]["name"];?></strong> 修改用户数据 	
    </div>
    
    <div id="box">
    
    <form action="" name="formPost" id="formPost"  method="post">
    
    <table width="100%" border="0" cellpadding="1" cellspacing="1" id="table" class="table">
                    
        <?php
        
        //用户数据
		$i = 0;
        foreach ($VICACHE["mod.forms"]["groups"] as $gid => $group){
			
			if ( in_array($group["type"],array("text","textarea","file")) ){
				
				if( $group["type"] == "" ){
					$val = '<textarea name="G-'.$gid.'">'.$config["G-".$gid].'</textarea>';	
				}else{
					$val = '<input name="G-'.$gid.'" value="'.$config["G-".$gid].'" size="60" />';	
				}
			
           		echo '<tr class="'.($i%2?'title':'line').'"> <td align="right">'.$group["name"].'</td> <td>'.$val.'</td></tr>';
				
				$i++;
			}
			
			
        }
        
        
        ?>
    </table>
    
    	<div id="saving">	
        <input type="hidden" name="id" value="<?php echo $id;?>" />
        <input name="action" id="action" type="hidden" value="update" />
        <input type="submit" class="submit" value="保存更改" />
        </div>
    
    </form>
    
    </div>
    
    <?php
  
    //mysql close
    reader_mysql(true);
    
    ?>
    
<!--body end-->

<?php html_close();?>