<?php

class Member extends Module{

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

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

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

	/*
		输出表单数据
		$appid		模块ID
		$fid		表单ID
		$base		扩展选项
	*/
	function getReport( $appid, $fid, $base ){
		global $_G;
		global $_CACHE;
		
		//设置内存占用大小
		if( $_G['setting']['global']['memory'] ){
			ini_set( 'memory_limit', $_G['setting']['global']['memory'].'M' );
		}
		
		$q = getgpc('q');
		$s = getgpc("s");
		
		$list = getgpc('list');
		$action = getgpc('action');
		
		//状态筛选
		$filter = getnum("filter",-1);
		
		//日期筛选
		$start = getgpc('start');
		$end = getgpc('end');

		//时间
		if ($start && $end){
			$ect=" and  dateline >=".strtotime($start)." and dateline <= ".strtotime($end.' 23:59:59');
		}
		
		//连接数据库
		System :: connect();
			  
		//加载缓存
		Cached :: loader( $appid, 'form.'.$fid );
		
		$groups = $_CACHE[$appid]['group'];
		
		$options = $_CACHE[$appid]['option'];
		
		$config = $_CACHE[$appid]["form"]["config"];
	    
		/************/
		
	    $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 = System :: $db -> getAll( $sql );
		
		//加载配置文件
		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.='	状态';
		$report.='	IP地址';
		$report.='	提交日期';    
		$report.='	提交时间'.chr(13);    
	    
		//输出数据
	    foreach( $result as $row ){
			
			//删除空白字符
			$row['config'] = preg_replace("/([\r\n\t\f])/"," ",$row['config']);
		
			$data = 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 = $data['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[$gid][$oid]["name"].($n!=count($array)?'、 ':'');
						$n++;
					}
					
					//新增，扩展信息
					if( $data['G-'.$gid.'-EXT'] ){
						$value .= ($value?'、 ':'').$data['G-'.$gid.'-EXT'];
					}
					
					$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 'dateline':						
						$val = date("Y/m/d H:i:s",$user[$fied]);
					break;				
					
					//没有从字段中取得信息就从内容中获取
					case 'account':						
					case 'name':						
					case 'email':						
					case 'phone':						
					case 'idcard':						
					case 'company':						
					case 'qq':						
						$val = $data[strtoupper($fied)] ? $data[strtoupper($fied)] : $user[$fied];
					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( call_user_func(array($appid, 'parse_value'), $fied, $row, $data, $config ) );
				}
			}
			
			$report.='	'.( $row['state'] ? "已确认" : "未确认" );
			$report.='	'.$row["ip"];
			$report.='	'.date("y/m/d",$row['dateline']);
			$report.='	\''.date("H:i:s",$row['dateline']).chr(13);

		}
	  
		//关闭数据库
		System :: connect();
		
		//加载下载函数
		require("../../function/client.php");
		
		/*
		//大文件导出方法
		
		create_file( 's.csv', $report );
		exit();
		*/
		
		report( $_CACHE[$appid]['form']['name'] , $report );
		
		unset( $report );
		
	}

}

?>