<?php
if (!defined('IN_59MIAO'))
{
	exit('Access Defined');
}
//模板控制器
class TemplateAction extends Action{
	public function __construct(){
		parent::__construct();
	
	}	
	public function index(){
		$currentTpl=$this->getCurrentTpl();
		$this->_tpl->assign('Current_tpl',$currentTpl);
		$Alltpl=$this->getdir();		
		$this->_tpl->assign('All_tpl',$Alltpl);
		$this->_tpl->display(SMARTY_ADMIN.'public/template.tpl');
	}		
	private function getdir(){	
		$dir = scandir (ROOT_PATH.'/view' );
		foreach ( $dir as $k => $v ) {
				if (is_dir ( ROOT_PATH . '/view/' . $v ) && $v != '..' && $v != '.' && $v != 'admin' && $v != 'js') {
					if (is_file ( ROOT_PATH . '/view/' . $v . '/template.config.php' )) {
						include ROOT_PATH . '/view/' . $v . '/template.config.php';
						$tpldir [] = array ('tplname' => $v, 'picture' => 'view/' . $v . '/images/' . $picture, 'author' => $author, 'version' => $version, 'info' => $info );
					}
				}
			}
		return $tpldir;
	}
	private function getCurrentTpl(){
		if(file_exists(ROOT_PATH.'/view/'.SMARTY_FRONT.'/template.config.php')){
			require_once ROOT_PATH.'/view/'.SMARTY_FRONT.'/template.config.php';
		}	
		$currentTpl=array(
			'tplname'=>SMARTY_FRONT,
			'picture'=>$picture,
			'author'=>$author,
			'version'=>$version,
			'info'=>$info
		);
		return $currentTpl;
	}
	//设置模板
	public function settpl(){
		//如果存在模板名称
		$tpl=$_GET['tplname'];
		$picture='';
		$author='';
		$version='';
		$info='';
		if($_GET['tplname']){
			if(file_exists(ROOT_PATH.'/view/'.$tpl.'/template.config.php')){
				//存在模板的话保存配置信息
				if($this->saveTpl($tpl)){
					require_once ROOT_PATH.'/view/'.$tpl.'/template.config.php';
					$currentTpl=array(
						'tplname'=>$tpl,
						'picture'=>$picture,
						'author'=>$author,
						'version'=>$version,
						'info'=>$info
					);
					echo '
					<div class="tpl_img">
					<img src="view/'.$currentTpl['tplname'].'/images/'.$currentTpl['picture'].'" />
					</div>
					<div class="tpl_info">
						<dl>
							<dt><h3>模板名称：<span>'.$currentTpl['tplname'].'</span></h3></dt>
							<dd>版权信息：<span>'.$currentTpl['author'].'</span></dd>
							<dd>版本：<span>'.$currentTpl['version'].'</span></dd>
							<dd>描述：<span>'.$currentTpl['info'].'</span></dd>
						</dl>
					</div>
					';
				}
				else{					
					return false;
				}
				
			}				
		}
		else{			
			return false;
		}
	}
	
	//把模板信息写入
	private function saveTpl($tpl){		
		//写入文件
		$_br = "\r\n";
		$_tab = "\t";
		
		$_profile = '<?php'.$_br;
		
		
		$_profile .= $_tab."//模板配置文件".$_br;		
		$_profile .= $_tab."define('SMARTY_FRONT','{$tpl}');".$_br;		
		
		$_profile .= '?>'.$_br;
		
		if (!file_put_contents('./configs/fronttpl_config_inc.php', $_profile)) {			
			return false;
		}
		else{
			return true;
		}
	}
		
}
?>