<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	2011/6/5 Lay
*/

	require("../../config/config.php");
	require("../../config/version.php");
	require("../../function/library.php");
	require("../../function/compat.php");
	require("../../function/system.php");
	
	System :: check_login();

	//module
	require 'config.php';

	$q = getgpc('q');
	$s = getgpc("s");
	$base = $_GET["b"];
	
	$fid = getnum('fid',0);
	$list = getgpc('list');
	$appid = getgpc('appid');
	$action = getgpc('action');
	
	//状态筛选
	$filter = getnum("filter",-1);
	
	//日期筛选
	$start = getgpc('start');
	$end = getgpc('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;
	}
	
	//连接数据库
	System :: connect();
		  
	//加载缓存
	Cached :: loader( $appid, 'form.'.$fid );
	
	$groups = $_CACHE[$appid]['group'];
	
	$options = $_CACHE[$appid]['option'];
    
	/************/
	
    $sql="SELECT * FROM `mod:value` WHERE fid=".$fid." ";
    if ($q && $s){
        $sql.=" and `".$s."` like '%".$q."%'";
    }
	
    if ( $filter >- 1){
        $sql.=" and state = ".$filter;
    }
	
	if ($ect) $sql.= $ect;
    
    //排序
    $sql.=" order BY id desc";    
    
    $result=mysql_query($sql,$mysql);
	
	require("../$appid/config.php");
	require("../$appid/function.php");
	
	
	//构造表头
	$report='	用户名';
	
    //表单数据
    foreach( $_CACHE[$appid]['group'] as $id => $array ){
		$report.='	'.$array["name"];
    }
    
    //用户数据
    if( is_array($base) ){
        foreach ($_G['module']['member']["fieds"] as $fied => $name){
            if( $base[$fied] ) $report.='	'.$name;
        }
    }
    
    //数据扩展
	if( is_array($_G['module']['member']["value"]) ){
		foreach( $_G['module']['member']["value"] as $fied => $name ){
			$report.='	'.$name;
		}
	}
	
	$report.='	提交时间'.chr(13);
    
    
    while($row=mysql_fetch_assoc($result)){
		
		//删除空白字符
		$row['config'] = preg_replace("/([\r\n\t\f])/"," ",$row['config']);
        
		//兼容GBK中文
		if( $_G['product']['charset'] == "gbk" ){
			$config = json_decode(iconv('GBK', 'UTF-8', $row['config']),true);
			
			if( is_array($config) ){
				foreach ($config as $key => $val){
					$config[$key] = iconv('UTF-8', 'GBK', $val);
				}
			}
		}else{
			//配置
        $config = fix_json( $row['config'] );	
		}
		
		
		$user = array();
		
		if( is_array($base) && $row['uid'] ){
			
			$sql="SELECT * FROM `mod:member` WHERE id='".$row['uid']."'";
			
			System :: $db -> query($sql);
			$user = System :: $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 ($_G['module']['member']["fieds"] as $fied => $name){
			
			//转换数据					
			switch($fied){
			
				case 'sex':						
					$val = $_G['project']['sex'][$user[$fied]];
				break;
			
				case 'marriage':						
					$val = $_G['project']['marriage'][$user[$fied]];
				break;
			
				case 'idcard':						
					$val = "'".$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 'email':						
					$val = $user[$fied] ? $user[$fied] : $config['EMAIL'] ;
				break;
			
				case 'phone':						
					$val = "'".( $user[$fied] ? $user[$fied] : $config['PHONE'] );
				break;
			
				default:						
					$val = $user[$fied];
				break;
			
			}
			
			if( $base[$fied] )	$report.='	'.$val;
		}
	
		
		//数据扩展
		if( is_array($_G['module']['member']["value"]) ){
			foreach( $_G['module']['member']["value"] as $fied => $name ){
				$report.='	'.strip_tags(parse_value($fied,$config[$fied]));
			}
		}
		
		$report.='	'.date("y/m/d",$row['dateline']).chr(13);

	}
  
	//关闭数据库
	System :: connect();	
	
	//输出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),"?","/","\\","<",">","'",'"',"|"),"_",$_CACHE[$appid]['form']["name"]);
		
		$name = preg_replace('/[^\x7f-\xff\d]/',"",$_CACHE[$appid]['form']["name"]);
		
		//$name=urlencode($_CACHE[$appid]['form']["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($_CACHE[$appid]['form']["name"])).".csv\"");
	}
	
	header("Accept-Ranges: bytes");
	
	echo(chr(255).chr(254));
	
	//$report = iconv($_G['product']['charset'],"UTF-8",$report);
	
	//echo $report;
	
	echo(iconv($_G['product']['charset'],"UTF-16LE".'//IGNORE',$report));

?>