<?
class template_ex{

	var $db;
	var $tpl_complation_dir;
	var $now_tpl;
	var $tpl_file_dir;
	var $tpl_config;
	var $install_site_config_name	=	array(
			'site_logo','list_thumb','img_thumb','glo_proscenium_lang','default_icon',	
		 );
	function	__construct($db=null,$now_tpl='')
	{
		if($db){
			$this->db								=	$db;
		}else{
			global $db_obj;
			$this->db								=	$db_obj;
		}	
		$this->tpl_complation_dir			=	TEMPLATE_COMPILATION_DIR;
		if(!$now_tpl){
			$this->now_tpl						=	$this->get_this_template();
		}else{
			$this->now_tpl						=	$now_tpl;
		}
		$this->tpl_file_dir					=	$this->tpl_complation_dir.$this->now_tpl;
		$this->tpl_config						=	$this->get_tpl_config($this->tpl_file_dir);
		$this->is_tpl_preview();
	
	}	
	function get_block_type_arr()
	{
		if($this->block_type_arr){
			return $this->block_type_arr;
		}
		global $_ADMIN_LANG;
		if(!$_ADMIN_LANG){
			global $glo_lang;
			require_once(ROOT_PATH.'admin/lang/'.$glo_lang.'.php');
		}
		$this->block_type_arr				=	array(
			'soft_box'			=>	array('lib_file'=>'soft_box/soft_box.html','name'=>$_ADMIN_LANG['block_type_arr']['soft_box']),
			'soft_cat_box'		=>	array('lib_file'=>'soft_cat_box/soft_cat_box.html','name'=>$_ADMIN_LANG['block_type_arr']['soft_cat_box']),
			'art_box'			=>	array('lib_file'=>'art_box/art_box.html','name'=>$_ADMIN_LANG['block_type_arr']['art_box']),
			'art_cat_box'		=>	array('lib_file'=>'art_cat_box/art_cat_box.html','name'=>$_ADMIN_LANG['block_type_arr']['art_cat_box']),
			'soft_faq'			=>	array('lib_file'=>'soft_faq/soft_faq.html','name'=>$_ADMIN_LANG['block_type_arr']['soft_faq']),
			'soft_img_play'	=>	array('lib_file'=>'soft_img_play/soft_img_play.html','name'=>$_ADMIN_LANG['block_type_arr']['soft_img_play']),
			'soft_desc'			=>	array('lib_file'=>'soft_desc/soft_desc.html','name'=>$_ADMIN_LANG['block_type_arr']['soft_desc']),
			'soft_attr_info'	=>	array('lib_file'=>'soft_attr_info/soft_attr_info.html','name'=>$_ADMIN_LANG['block_type_arr']['soft_attr_info']),
			'related_soft'		=>	array('lib_file'=>'related_soft/related_soft.html','name'=>$_ADMIN_LANG['block_type_arr']['related_soft']),
			'released_history'	=>	array('lib_file'=>'released_history/released_history.html','name'=>$_ADMIN_LANG['block_type_arr']['released_history']),
			'soft_features'		=>	array('lib_file'=>'soft_features/soft_features.html','name'=>$_ADMIN_LANG['block_type_arr']['soft_features']),
			'custom'				=>	array('lib_file'=>'custom/.','name'=>$_ADMIN_LANG['block_type_arr']['custom']),
		);
		return $this->block_type_arr;
	}


	function get_this_template()	
	{
		if(!$this->now_tpl){
			global $glo_config_arr;
			if($glo_config_arr){
				$this->now_tpl	=	$glo_config_arr['now_template']['value'];	
				return $this->now_tpl;
			}
			$res	=	$this->db->getRecord("SELECT * FROM {$this->db->db_pre}site_config WHERE name='now_template' ");
			if(is_dir($this->tpl_complation_dir.$res['value'])){
				$this->now_tpl			=	$res['value'];	
				return $this->now_tpl;
			}else{
				return false;	
			}
		}else{
			return $this->now_tpl;
		}
	}
	
	function list_tpl()
  	{
		clearstatcache();
		$this->now_tpl		=		$this->get_this_template();
		if(!is_dir($this->tpl_complation_dir)){
			return false;
		}	
		$tpl_config_message	=	array();
		if($dh = opendir($this->tpl_complation_dir)){ 	
			$sort_arr			=		array();
			while(($file = readdir($dh)) !== false){ 
				$t_d	=	$this->tpl_complation_dir.$file;
				if(is_dir($t_d)&&$file!="." && $file!=".."&&substr($file,0,1)!='.'){
					if($this->now_tpl==$file){
						$tpl_config_message['now']						=		$this->tpl_config;
						$tpl_config_message['now']['dir_name']		=		$file;
						$tpl_config_message['now']['tpl_preview_image']	=	TEMPLATE_COMPILATION_URL.$this->now_tpl.'/'.$tpl_config_message['now']['tpl_preview_image'];	
					}else{
						$tmp_arr						=	$this->get_tpl_config($t_d);	
						if(!$tmp_arr){
							continue;
						}	
						$tmp_arr['dir_name']		=		$file;
						$tmp_arr['tpl_preview_image']	=	TEMPLATE_COMPILATION_URL.$file.'/'.$tmp_arr['tpl_preview_image'];
						$tmp_arr['tpl_dir']		=	$file;
						$tmp_arr['use']		=	false;
						$tpl_config_message['other'][]		=	$tmp_arr;
						$sort_arr[]				=		$tmp_arr['order_sort'];
					}
				}
			}
			if($sort_arr){
				array_multisort($sort_arr,SORT_DESC,$tpl_config_message['other']);
			}
			closedir($dh);
		}
		return $tpl_config_message;
	}		
	function get_tpl_config($t_d)
	{
		$tpl_config		=	$t_d.'/config';
		if(!is_file($tpl_config)){
			return false;
		}
		$TPL_CONFIG_MESSAGE	=	$this->check_config_data($tpl_config);
		if(!$TPL_CONFIG_MESSAGE){
			return false;
		}
		$c	=	$t_d.'/seat_lib/';
		if(is_dir($c)){
			if($dh = opendir($c)){ 	
				while(($file = readdir($dh)) !== false){ 
					$file_dir		=	$c.'/'.$file;
					if(is_file($file_dir)&&$file!="." && $file!=".."&&substr($file,0,1)!='.'){
							$TPL_CONFIG_MESSAGE['seat_lib'][]	=	$file;
						}
					}		
				closedir($dh);
			}	
		}
		
		
		return $TPL_CONFIG_MESSAGE;	
	}

	function  check_install_data($install_data_file)
	{
		$encode_str	=	file_get_contents($install_data_file);	
		$TPL_INSTALL_DATA		=		storage_dencode($encode_str);
		if(!$TPL_INSTALL_DATA){
				return false;
		}	
		global $glo_domain_directory;
		return $TPL_INSTALL_DATA;
	}

	function  check_config_data($tpl_config)
	{
		$encode_str		=	file_get_contents($tpl_config);
		$TPL_CONFIG_MESSAGE			=	storage_dencode($encode_str);
		return $TPL_CONFIG_MESSAGE;
	}


	function install_tpl()
	{
		$t_d			=	$this->tpl_file_dir;
		if($TPL_INSTALL_DATA	=	$this->check_install_data($t_d.'/install_data')){
			if($TPL_INSTALL_DATA['tpl_block']){
				$this->db->deleteRecord("{$this->db->db_pre}tpls "," tpl= '".$this->db->escape($this->now_tpl)."'");
				foreach($TPL_INSTALL_DATA['tpl_block'] as $install_val){
					$install_val['blocks']['update_time']	=	time();
					$install_val['tpls']['update_time']		=	time();
					$install_val['tpls']['tpl']				=	$this->now_tpl;
					$block_id	=	$this->db->addRecord("{$this->db->db_pre}blocks",$install_val['blocks']);
					
					if(!$block_id){
						$this->db->updateRecord("{$this->db->db_pre}blocks"," block_name='".$this->db->escape($install_val['blocks']['block_name'])."'",$install_val['blocks']);
						$block_id	=	$this->db->getNumber("SELECT block_id FROM {$this->db->db_pre}blocks WHERE block_name='".$this->db->escape($install_val['blocks']['block_name'])."'");
					}
					$install_val['tpls']['block_id']		=	$block_id;
					$this->db->addRecord("{$this->db->db_pre}tpls",$install_val['tpls']);
				}
			}
			
			if($TPL_INSTALL_DATA['site_config']){	
					foreach($TPL_INSTALL_DATA['site_config'] as $site_config_val){
						$site_config_val['name']		=	trim($site_config_val['name']);
						if(in_array($site_config_val['name'],$this->install_site_config_name)){
							$cond			=		" name = '{$site_config_val['name']}' ";
							$this->db->updateRecord("{$this->db->db_pre}site_config",$cond, $site_config_val['data']);
						}
					}	
			}		
			if($TPL_INSTALL_DATA['channel_data']){	
					foreach($TPL_INSTALL_DATA['channel_data'] as $channel_data_val){
						$this->db->updateRecord(
									"{$this->db->db_pre}channel",$channel_data_val['cond'], $channel_data_val['data']
						);
					}	
			}
		}
		$this->db->updateRecord(
					"{$this->db->db_pre}site_config"," name='now_template' ",array('value'=>$this->now_tpl,'update_time'=>time())	
		);	
		//unlink tpl_c
		$this->clear_tpl_cache();
		return true;
	}
	function clear_tpl_cache()
	{
		$c			=		defined('TEMPLATE_COMPILATION_C_DIR')?TEMPLATE_COMPILATION_C_DIR:TEMPLATE_C_DIR;
		if($dh = opendir($c)){ 	
			while(($file = readdir($dh)) !== false){ 
				$file_dir		=	$c.'/'.$file;
				if(is_file($file_dir)&&$file!="." && $file!=".."&&substr($file,0,1)!='.'){
					@unlink($file_dir);
				}
			}		
			closedir($dh);
		}	
	}
	function get_tpl_seat_block($file)
	{
		$res		=	$this->db->getRecords("SELECT * FROM  {$this->db->db_pre}blocks a JOIN {$this->db->db_pre}tpls b ON a.block_id=b.block_id WHERE b.file='".$this->db->escape($file)."' AND b.tpl='".$this->db->escape($this->now_tpl)."'  ORDER BY b.sort_order Desc");
		$seat_arr		=		$this->tpl_config['tpl_file_name'][$file]['seat'];
		if($res){
			$block_type_arr		=	$this->get_block_type_arr();
			foreach($res as $key=>$val){
				$val['block_setting']							=	storage_dencode($val['block_setting']);
				$val['block_type_name']							=	$block_type_arr[$val['block_type']]['name'];
				$seat_arr[$val['seat']]['block'][]			=	$val;
			}
		}
		return $seat_arr;
	}	

	function uninstall_tpl()
	{
		return true;
	}

	function is_tpl_preview()
	{
		if(!($this->preview_seat_block_id	=	(int)$_GET['preview_seat_block_id'])){
			return false;
		}
		if(strstr($_SERVER['SCRIPT_NAME'],'soft.php')){
			$_GET['software_id']				=		$this->db->getNumber("SELECT software_id FROM {$this->db->db_pre}software LIMIT 1");
		}elseif(strstr($_SERVER['SCRIPT_NAME'],'/article.php')){
			$_GET['article_id']				=		$this->db->getNumber("SELECT article_id FROM {$this->db->db_pre}article LIMIT 1");
		}
		return $this->preview_seat_block_id;
	}

	function tpl_to_url($file)
	{
		global $glo_domain_directory;
		if(strstr($file,'index.html')){
			return $glo_domain_directory.'index.php';
		}elseif(strstr($file,'software.html')){
			return $glo_domain_directory.'soft.php';
		}elseif(strstr($file,'article.html')){
			return $glo_domain_directory.'article.php';
		}elseif(strstr($file,'article_list.html')){
			return $glo_domain_directory.'article_list.php';
		}elseif(strstr($file,'product.html')){
			return $glo_domain_directory.'product.php';
		}elseif(strstr($file,'download.html')){
			return $glo_domain_directory.'download.php';
		}elseif(strstr($file,'purchase.html')){
			return $glo_domain_directory.'purchase.php';
		}elseif(strstr($file,'support.html')){
			return $glo_domain_directory.'support.php';
		}elseif(strstr($file,'about.html')){
			return $glo_domain_directory.'about.php';
		}elseif(strstr($file,'channel.html')){
			return $glo_domain_directory.'channel.php';
		}
	}  

	function preview_block($block_string){
		global $_LANG;
		global $glo_lang;
		global $_ADMIN_LANG;
		if(!$_ADMIN_LANG){
			require_once(ROOT_PATH.'/admin/lang/'.$glo_lang.'.php');
		}

		if(!$block_string){
				$block_string	=	'<center>'.htmlspecialchars($_ADMIN_LANG['no_available_data_by_now'],ENT_QUOTES).'</center>';
		}	
		$block_string	=<<<html
<div id='preview_block_id' >{$block_string}</div><div id='preview_background' style='width:100%;height:100%;background:#A5D11F;filter:alpha(opacity=10);opacity:0.1;position:absolute;top:0;left:0;display:none;z-index:997;'/></div>
<div id='preview_background_2' style='border:3px solid black;background:#66FF00;filter:alpha(opacity=50);opacity:0.3;position:absolute;display:none; z-index:998;'></div><div style=' width:90px;height:22px;position:fixed;z-index:999;top:5%;right:3%'><a href='javascript:history.go(-1);' style="background-color:red;color:#fff;line-height:22px;display:block;width:90px;height:22px;text-align:center;" >{$_ADMIN_LANG['return']}</a></div>
<script>
$(document).ready(function(){
		document.getElementById("preview_block_id").scrollIntoView();	
		preview_background_obj		=	$("#preview_background");
		preview_background_obj.css({height:$(document).height(),width:$(document).width()});
		preview_background_obj.show();
		preview_block_obj				=	$("#preview_block_id");
		$("body")[0].scrollTop		=	preview_block_obj[0].offsetTop;

		preview_background_obj_2	=	$("#preview_background_2");
		var offset    					=  preview_block_obj.offset();

		var top_s						=	offset.top-3;
		var left_s						=	offset.left-3;
		var width_s						=	preview_block_obj.width();
		var height_s					=	preview_block_obj.height();
			

		preview_background_obj_2.css({top:top_s+"px",left:left_s+"px",height:height_s,width:width_s});
		preview_background_obj_2.show();
		
});
</script>
html;
		return $block_string;
	}

	function get_lib_file($block_type)
	{
		$block_type_arr	=	$this->get_block_type_arr();
		
		$lib_dir	=	$this->tpl_file_dir.'/lib/'.dirname($block_type_arr[$block_type]['lib_file']);
		
		$lib_arr	=	array();
		if(!is_dir($lib_dir)){
			return false;
		}
		if($dh = opendir($lib_dir)){ 	
			while(($file = readdir($dh)) !== false){ 
				$file_dir		=	$lib_dir.'/'.$file;
				if(is_file($file_dir)&&$file!="." && $file!=".."&&substr($file,0,1)!='.'){
					preg_match('|^<!\-\-\{.*?\*(.*?)\*.*?\}\-\->|si',file_get_contents($file_dir),$name);
					if(!$name[1]){
						continue;
					}
					$lib_arr[]	=	array(
						'name'		=>	$name[1],
						'file'		=>	dirname($block_type_arr[$block_type]['lib_file']).'/'.$file,
					);
				}
			}		
			closedir($dh);
		}
		return $lib_arr;	
	}

}
