<?php
	require("session_check.php");
	require("../foundation/ftpl_compile.php");
	
	//语言包引入
	$u_langpackage=new uilp;
	
	//获得方案名
	$loc=short_check(get_argg('pro'));
	$compile_type=short_check(get_argg('compile_type'));
	if(empty($loc)){
		echo "<script type='text/javascript'>alert('$u_langpackage->u_file_no');window.history.go(-1);</script>";
	}
	
	//数据表定义
	$t_config=$tablePreStr."config";
	
	//数据库连接
	$dbo=new dbex;
	dbtarget('w',$dbServs);
	if($compile_type!=''){
		$sql="update $t_config set v='$compile_type' where k='tpl_compile_type'";
		$dbo->exeUpdate($sql);
	}else{
		$sql="select v from $t_config where k='tpl_compile_type'";
		$config=$dbo->getRow($sql);
		$compile_type=$config['v'];
	}
	$sql="update $t_config set v='$loc' where k='templates'";
	$dbo->exeUpdate($sql);

	$def_skin='';
	if(file_exists("../skin/".$loc)){
		$skin_ref=opendir("../skin/".$loc);
		while($skin_plan=readdir($skin_ref)){
			if(!preg_match("/^\./",$skin_plan)&&$skin_plan!='js'&&$skin_plan!='home'){
				$def_skin=$loc."/".$skin_plan;break;
			}
		}
	}
	$sql="update $t_config set v = '$def_skin' where k = 'skin' ";
	if($dbo->exeUpdate($sql)){
		$conf_str=file_get_contents("../configuration.php");
		$change_conf=preg_replace('/\$skinUrl=\".*\"/', '\$skinUrl="'.$def_skin.'"' ,$conf_str);
		$conf_ref=fopen("../configuration.php","w+");
		fwrite($conf_ref,$change_conf);
	}

	if(get_argp('batch')){
		$c_tmp=get_argp('c_tmp');
		//批量生成模板
		function batch_comp($loc,$c_tmp){
			global $compile_type;
			if($c_tmp!=''){
				foreach($c_tmp as $value){
					tpl_engine($loc,$value,0,$compile_type);
				}
			}
		}
	}else{
		//直接应用模板
		function list_child_file($local){
			global $compile_type;
			$ref=opendir("../templates/".$local);
			while($tp_dir=readdir($ref)){
				if(!preg_match("/^\./",$tp_dir)){
					if(filetype("../templates/".$local."/".$tp_dir)=="dir"){
						list_child_file($local."/".$tp_dir);
					}
					if(filetype("../templates/".$local."/".$tp_dir)=="file"){
						global $loc;
						$show_local=$local.'/'.$tp_dir;
						$show_local=preg_replace("/$loc\//","",$show_local);
						tpl_engine($loc,$show_local,0,$compile_type);
					}
				}
			}
		}
	}
?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="css/right.css">
</head>
<body>
	
<div class="container">
	<div class="rs_head"><?php echo $u_langpackage->u_compile_state?></div>
</div>

<table class='main main_left'>
	<tr>
		<td><?php if(get_argp('batch')){batch_comp($loc,$c_tmp);}else{list_child_file($loc);}?></td>
	</tr>
	<tr>
		<td><input type='button' class='top_button' value='<?php echo $u_langpackage->u_list_back?>' onclick='window.history.go(-1);' /></td>
	</tr>
	
<table>
	
</body>
</html>		