<?php
if(!defined('MOD'))  die("access die");

$id = intval( $_GET['id']);
if ( $id == 0){
	$result = qdb::Query( "select id from ".TABLEPRE."js where channelid='$channelid'");
	while( $is = qdb::fetchArray( $result)){
		$iis[] = $is['id'];
	}
	if( isset( $iis) && is_array( $iis)){
		$id = implode(",", $iis);
	}else{
		qbase::showmsg("没有相关信息","default","system.php?sys=js&channelid={$channelid}&channelselect={$channelselect}&action=manage");
	}
}
$result = qdb::Query( "select * from ".TABLEPRE."js where id IN (".$id.")");
while( $js = qdb::fetchArray( $result)){
	qbase::writetofile(A_P . "/templates/cache/" . substr($js['file_name'], 0, strpos($js['file_name'], '.')) . '.php', $js['tpl']);
	@extract( @unserialize( $js['args']));
	$order_type = $order_type ? " views desc " : " id desc ";
	if ( intval( $_GET['preview']) == 1){
		outputHeader();
		$temp = cmsQuery("channelid={$js['channelid']}&cid={$cid}&is_comend={$is_comend}&order={$order_type}&filter={$filter}&thumb={$thumb}&pagesize={$num}");
		$_obj = $temp['data'];
		include qbase::template('cache/' . $js['file_name'] );
		die();
	}else{
		$temp = cmsQuery("channelid={$js['channelid']}&cid={$cid}&is_comend={$is_comend}&order={$order_type}&filter={$filter}&thumb={$thumb}&pagesize={$num}");
		$_obj = $temp['data'];
		include qbase::template('cache/' . $js['file_name'] );
		$content =  ob_get_contents();
		ob_clean();
		$content = str_replace( "\"","\\\"", $content);
		if ( strpos( $content, "\n") !== false){
			$contents = explode("\n", $content);
		}else{
			$contents[] = $content;
		}
		$content = '';
		foreach($contents as $val){
			$content.= "document.write(\"".$val."\");\n"; 	
		}
		$file   = C_P."/js/{$channelid}/{$js['file_name']}";
		qbase::writetofile($file , $content);
	}
	if ( !intval( $_GET['preview'])) qbase::get_object('event', 'js', array('action'=>'cache', 'data'=>explode(',', $id)));
}
qbase::showmsg("相关文件生成成功","default","system.php?sys=js&channelid={$channelid}&channelselect={$channelselect}&action=manage");

function outputHeader()
{
	global $_SET;
	echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=Content-Type content=\"text/html; charset={$_SET['def_charset']}\"><link href=\"".W_P."templates/default/default.css\" type=text/css rel=stylesheet /><base target='_blank' /></head><body>";
}

?>