<?php
/*
	生成基于 forms 表的 keys
*/
function form_hash( $appid , $fid , $name ){

	//这里很奇怪，只支持长度为4的数组，所以要注意
	$hash = array("appid"=>$appid,"fid"=>$fid,"name"=>$name ,"expire"=>time());
	
	$keys = do_mencrypt(serialize($hash),VI_START);
	
	return rawurlencode($keys);
	
}

/********** Func **********/

//生成HTML表单
function getReport( $appid, $fid ){
	global $Project;
	global $Modules;
	global $VICACHE;
	global $mysql;
	global $db;
	
	$q = GetQuery("q");
	$s = GetQuery("s");
	$base = $_GET["b"];
	
	$list = GetQuery("list");
	$action = GetQuery("action");
	
	//状态筛选
	$filter = GetNumber("filter",-1);
	
	//日期筛选
	$start = GetQuery("start");
	$end = GetQuery("end");

	//时间
	if ($start && $end){
	
		//开始时间
		$e_array=explode("-",$start);
		$time1=mktime(0, 0, 0, $e_array[1], $e_array[2], $e_array[0]);
		
		//到期时间
		$e_array=explode("-",$end);
		$time2=mktime(23, 59, 59, $e_array[1], $e_array[2], $e_array[0]);
		
		$ect=" and  dateline >=".$time1." and dateline <= ".$time2;
	}
	
	//mysql open
	reader_mysql();
		  
	//加载缓存
	loader_cache($appid,"forms.".$fid);
	
	$groups = $VICACHE["mod.forms"]["groups"];
	
	$options = $VICACHE["mod.forms"]["options"];
    
	/************/
	
    $sql="SELECT * FROM ".VI_DBMODPRE."values where fid=".$fid." ";
    if ($q && $s){
        $sql.=" and  ".$s." like '%".$q."%'";
    }
	
    if ( $filter >- 1){
        $sql.=" and status = ".$filter;
    }
	
	if ($ect) $sql.= $ect;
    
    //排序
    $sql.=" order BY id desc";    
	
	//echo $sql;
    
    $result=mysql_query($sql,$mysql);
	
	//加载配置文件
	require("../$appid/config.php");
	require("../$appid/function.php");
	
	
	//构造表头
	$report='	用户名';
	
    //表单数据
    foreach( $VICACHE["mod.forms"]["groups"] as $id => $array ){
		$report.='	'.$array["name"];
    }
    
    //用户数据
    if( is_array($base) ){
        foreach ($Modules["member"]["fieds"] as $fied => $name){
            if( $base[$fied] ) $report.='	'.$name;
        }
    }
    
    //数据扩展
	if( is_array($Modules[$appid]["value"]) ){
		foreach( $Modules[$appid]["value"] as $fied => $name ){
			$report.='	'.$name;
		}
	}
	
	$report.='	状态';
	$report.='	提交时间'.chr(13);    
    
	//输出数据
    while($row=mysql_fetch_assoc($result)){
		
		//删除空白字符
		$row["config"] = preg_replace("/([\r\n\t\f])/"," ",$row["config"]);
        
        $config = fix_json( $row["config"] );	
		
		$user = array();
		
		if( is_array($base) && $row["uid"] ){
			
			$sql="select * from ".VI_DBMODPRE."members where id='".$row["uid"]."'";
			
			$db -> query($sql);
			$user = $db -> fetchRow();
		}
        
		$report.='	'.$row["account"];
			
		//表单数据
		foreach( $groups as $gid => $array ){
			
			$value = $config['G-'.$gid];
			
			if( in_array($groups[$gid]["type"],array("text","textarea","file")) ){
				
				if( $groups[$gid]["type"] == "file" ){
					$report.='	'.$value;
				}else{
					//数字前加单引号
					if( is_numeric($value) ){
						$report.="	'".$value;
					}else{
						$report.='	'.$value;
					}
				}
			
			}else{
					
				$array = explode(",", $value);
				
				$value = "";
				
				$n = 1;
				foreach( $array as $oid ){					
					$value .= $options[$oid]["name"].($n!=count($array)?'、 ':'');
					$n++;
				}
				
				$report.='	'.$value;			
			}
			
		}
		
		//用户数据
		foreach ($Modules["member"]["fieds"] as $fied => $name){
			
			//转换数据					
			switch($fied){
			
				case 'sex':						
					$val = $Project["sex"][$user[$fied]];
				break;
			
				case 'marriage':						
					$val = $Project["marriage"][$user[$fied]];
				break;
            
                case 'dateline':						
                    $val = date("Y/m/d H:i:s",$user[$fied]);
                break;				
				
				case 'account':						
					$val = $user[$fied] ? $user[$fied] : $config['ACCOUNT'] ;
				break;
				
				case 'name':						
					$val = $user[$fied] ? $user[$fied] : $config['NAME'] ;
				break;
			
				case 'email':						
					$val = $user[$fied] ? $user[$fied] : $config['EMAIL'] ;
				break;
			
				case 'phone':						
					$val = "'".( $user[$fied] ? $user[$fied] : $config['PHONE'] );
				break;

				case 'idcard':						
					//$val = "'".$user[$fied];
					$val = "'".( $user[$fied] ? $user[$fied] : $config['IDCARD'] );
				break;
							
				default:						
					$val = $user[$fied];
				break;
			
			}
			
			if( $base[$fied] )	$report.='	'.$val;
		}
	
		
		//数据扩展
		if( is_array($Modules[$appid]["value"]) ){
			foreach( $Modules[$appid]["value"] as $fied => $name ){
				$report.='	'.strip_tags(parseValue($fied,$row,$config));
			}
		}
		
		$report.='	'.( $row["status"] ? "已确认" : "未确认" );
		$report.='	'.date("y/m/d",$row["dateline"]).chr(13);

	}
  
    //mysql close
    reader_mysql(true);
	
	//加载下载函数
	require("../../function/client.php");
	
	report( $VICACHE["mod.forms"]["name"] , $report );
	
	/*
	//输出CSV
	header("Cache-Control: public");
	header("Content-type:application/msexcel; charset=GBK");
	
	//IE就对文件名进行编码
	if( eregi("MSIE" , $_SERVER["HTTP_USER_AGENT"]) ){
		
		$reg = "/^.+MSIE (\d+\.\d+).+$/";
		
		$ver = preg_replace ($reg, "$1",$_SERVER["HTTP_USER_AGENT"]);
		
		//$name = str_replace(array(chr(63),"?","/","\\","<",">","'",'"',"|"),"_",$VICACHE["mod.forms"]["name"]);
		
		$name = preg_replace('/[^\x7f-\xff\d]/',"",$VICACHE["mod.forms"]["name"]);
		
		//$name=urlencode($VICACHE["mod.forms"]["name"]);//将关键字编码
		//$name=preg_replace("/(%7E|%60|%21|%40|%23|%24|%25|%5E|%26|%27|%2A|%28|%29|%2B|%7C|%5C|%3D|\-|_|%5B|%5D|%7D|%7B|%3B|%22|%3A|%3F|%3E|%3C|%2C|\.|%2F|%A3%BF|%A1%B7|%A1%B6|%A1%A2|%A1%A3|%A3%AC|%7D|%A1%B0|%A3%BA|%A3%BB|%A1%AE|%A1%AF|%A1%B1|%A3%FC|%A3%BD|%A1%AA|%A3%A9|%A3%A8|%A1%AD|%A3%A4|%A1%A4|%A3%A1)+/",'',$name);
		
		
		if( $ver == "6.0" || $ver == "6" ){		
			header("Content-Disposition:attachment; filename=\"".urlencode($name).".csv\"");		
		}else{
			header("Content-Disposition:attachment; filename=\"".$name.".csv\"");
		}
		
	}else{
		header("Content-Disposition:attachment; filename=\"".(addslashes($VICACHE["mod.forms"]["name"])).".csv\"");
	}
	
	header("Accept-Ranges: bytes");
	
	echo(chr(255).chr(254));
	
	//$report = iconv($Project["charset"],"UTF-8",$report);
	
	//echo $report;
	
	echo(iconv($Project["charset"],"UTF-16LE".'//IGNORE',$report));
	*/
}

?>