<?php

//生成表单数据标签
function parse_forms($array){
	//global $Modules;
	
	//表单根目录
	//$formRT=readpath(VI_BASE.$Modules["forms"]["path"]);

	$html = str_replace(chr(13),'<br />',$array["description"]);
	$html = str_replace("\$VIEW","<span id='form-".$array["id"]."-view'>0</span>",$html);
	$html = str_replace("\$COUNT","<span id='form-".$array["id"]."-count'>0</span>",$html);
	$html = str_replace("\$CLOCK","<span id='form-".$array["id"]."-clock' rel='".$array["expire"]."'></span>",$html);
	$html = str_replace("\$SEARCH",'<a href="?action=status&id='.$array["id"].'">用户列表</a>',$html);
	$html = ubb_basic($html);
		
	//Flash
	$html=preg_replace("/\[(header)=(.+?|),(.+?|),(.+?|)\]([^<>]*?)\[\/(header)\]/is",'<embed width="\\2" height="\\3" src="\\5" style="\\4" wmode="transparent"></header>',$html);

	return $html;
}


//生成HTML表单
function cache_forms($list,$appid,$mysql){
	global $Project;
	global $Modules;
	
	if(!$mysql){
		return false;
	}
	
	//读取表单信息
	if($list){
		$sql="select * from ".VI_DBMODPRE."forms where id in(".$list.") and appid='".$appid."' ";
	}else{
		$sql="select * from ".VI_DBMODPRE."forms where appid='".$appid."' ";
	}
		
	//创建子文件夹
	$folder = create_dir(VI_ROOT."/".$Project["cache"]."/".$appid);

	//echo $sql;
	
	$result=mysql_query($sql,$mysql);
	
	while($row=mysql_fetch_assoc($result)){
		
		$file = VI_ROOT."/".$Project["cache"]."/".$appid."/forms.".$row["id"].".php";
		
		$cache ='<?php'.chr(13);
		$cache.='/*'.date("Y-m-t H:i:s").'*/'.chr(13);
		
		/********表单主体********/
		
		$cache.='$VICACHE["mod.forms"] = array('.chr(13);
		
		$n = 1;
		foreach ($row as $key => $value) {
			$cache .= '"'.$key.'" => '.'"'.str_replace('$','\$',addslashes($value)).'"'.($n!=count($row)?',':'');
			$n++;
		}
		$cache.=");".chr(13).chr(13);
		
		/********选项组********/

		$sql="SELECT * FROM ".VI_DBMODPRE."groups where fid=".$row["id"]." and status>0 order BY sort asc,id asc";
		
		$groups=mysql_query($sql,$mysql);
		$rows = mysql_num_rows($groups);
		
		$cache.='$VICACHE["mod.forms"]["groups"] = array('.chr(13);
		$x = 1;
		while($group=mysql_fetch_assoc($groups)){
			
			$cache.='	"'.$group["id"].'" => array('.chr(13);
			
			$n = 1;
			foreach ($group as $key => $value) {
				$cache .= '"'.$key.'" => '.'"'.str_replace('$','\$',addslashes($value)).'"'.($n!=count($group)?',':'');
				$n++;
			}
			$cache.=')'.($x<$rows?',':'').chr(13).chr(13);
			$x++;
		}
		$cache.=");".chr(13).chr(13);
		
		/********子选项********/
	
		$sql="SELECT * FROM ".VI_DBMODPRE."options where fid=".$row["id"]." order BY sort asc,id asc";
		
		$options=mysql_query($sql,$mysql);
		$rows = mysql_num_rows($options);
		
		$cache.='$VICACHE["mod.forms"]["options"] = array('.chr(13);
		$x = 1;
		while($option=mysql_fetch_assoc($options)){
			
			$cache.='	"'.$option["id"].'" => array('.chr(13);
			
			$n = 1;
			foreach ($option as $key => $value) {
				$cache .= '"'.$key.'" => '.'"'.str_replace('$','\$',addslashes($value)).'"'.($n!=count($option)?',':'');
				$n++;
			}
			$cache.=')'.($x<$rows?',':'').chr(13).chr(13);
			$x++;
		}
	
		$cache.=");".chr(13).chr(13);
		$cache.="?>";
		
		$cache = str_replace($item,str_replace('),',')',$item),$cache);
		
		//写入缓存
		create_file($file,$cache);		
	}
}

?>