<?php
class TempletsAction extends Action{
    public function checkUser($level){
		if(empty($_SESSION['aid'])) $this->redirect('/Admin/Index/login');
		if(!empty($level) && !in_array($level,$_SESSION['levels'])) $this->error('您没有此权限!.');
	}
    public function index(){
		$this->checkUser('5_1_1');
		$theme = $this->dir(C('ROOT').'/template/Tpl/Home');
		$this->assign('theme',$theme);
		$this->display();
	}
	public function theme(){
		$this->checkUser('5_1_2');
		$config = file_get_contents('config/config.inc.php');
		$new = preg_replace('/\'DEFAULT_THEME\'=>\'(.*?)\',/','\'DEFAULT_THEME\'=>\''.$_GET['name'].'\',',$config);
		if(file_put_contents('config/config.inc.php',$new))	$this->success('更换成功!^_^');
		else $this->error('更换失败!= =重试吧!');
	}
	public function dir($path){
		$this->checkUser();
		if(!is_dir($path)) return;
		$files = array();
		$handle = opendir($path);
		$i=0;
		$config = file_get_contents('config/config.inc.php');
		preg_match('/\'DEFAULT_THEME\'=>\'(.*?)\'/',$config,$nowTheme);
		$nowTheme = $nowTheme[1];
		while(($file = readdir($handle)) != false){
			if($file != '.' && $file != '..'){
				$newPath = $path.'/'.$file;
				if(is_dir($newPath)) $dir[$i]['dir'] = $file;
				if(is_file($newPath.'/screen.jpg')) $dir[$i]['sreen'] = C('URL').'/template/Tpl/Home/'.$file.'/screen.jpg';
				else $dir[$i]['sreen'] = C('URL').'/Public/source/no_screen.jpg';
				if($file == $nowTheme) $dir[$i]['text'] = '<font color="red">正使用</font>';
				else $dir[$i]['text'] = '切换到此模板';
			}
			$i++;
		}
		return($dir);
	}
	public function edit(){
		$this->checkUser('5_2_1');
		$config = file_get_contents('config/config.inc.php');
		preg_match('/\'DEFAULT_THEME\'=>\'(.*?)\'/',$config,$nowTheme);
		$nowTheme = $nowTheme[1];
		$list = $this->file(C('ROOT').'/template/Tpl/Home/'.$nowTheme);
		$this->assign('list',$list);
		$this->display();
	}
	public function file($path){
		$this->checkUser();
		$desc['Index_index.html'] = '主页模板';
		$desc['Index_nlist.html'] = '新闻列表模板';
		$desc['Index_plist.html'] = '产品列表模板';
		$desc['Index_pro.html'] = '产品内容模板';
		$desc['Index_news.html'] = '新闻内容模板';
		$desc['Index_page.html'] = '自定义页面模板';
		$desc['Index_reply.html'] = '留言页模板';
		$desc['Index_buy.html'] = '订单页模板';
		$desc['Public_header.html'] = '公用头部模板';
		$desc['Public_footer.html'] = '公用底部模板';
		if(!is_dir($path)) return;
		$files = array();
		$handle = opendir($path);
		$i =0;
		while(($file = readdir($handle)) != false){
			if(substr($file,-4) == 'html'){
				$templets[$i]['name'] = $file;
				$templets[$i]['size'] = filesize($path.'/'.$file);
				$templets[$i]['time'] = filemtime($path.'/'.$file);
				$templets[$i]['edit'] = '<a href="__URL__/editTemplets/name/'.$file.'" style="color:red">[修改]</a>';
				if($desc[$file]){
					$templets[$i]['desc'] = $desc[$file];
				}else{ 
					$templets[$i]['desc'] = '未知模板';
					$templets[$i]['edit'] .= '&nbsp;&nbsp;|&nbsp;&nbsp;<a href="__URL__/delTemplets/name/'.$file.'" onclick="return confirm(\'提示：您确定要删除该模板吗？删了就真的没了!\')">[删除]</a>';
				}
			}
			$i++;
		}
		return($templets);
	}
	public function editTemplets(){
		$this->checkUser('5_2_2');
		$config = file_get_contents('config/config.inc.php');
		preg_match('/\'DEFAULT_THEME\'=>\'(.*?)\'/',$config,$nowTheme);
		$nowTheme = $nowTheme[1];
		$list['name'] = $_GET['name'];
		$list['file'] = C('ROOT').'/template/Tpl/Home/'.$nowTheme.'/'.$list['name'];
		$list['content'] = @file_get_contents(C('ROOT').'/template/Tpl/Home/'.$nowTheme.'/'.$list['name']);
		$list['content'] = str_replace('<textarea','##textarea',$list['content']);
		$list['content'] = str_replace('</textarea>','##/textarea>',$list['content']);
		$content = empty($list['content']) ? '<!--模板文件内暂无内容-->' : $list['content'];
		$this->assign('list',$list);
		$this->display();
	}
	public function checkTemplets(){
		$this->checkUser('5_2_2');
		$file = stripslashes($_POST['file']);
		$content = str_replace('##textarea','<textarea',stripslashes($_POST['content']));
		$content = str_replace('##/textarea>','</textarea>',$content);
		$content = empty($content) ? '<!--模板文件内暂无内容-->' : $content;
		if(file_put_contents($file,$content)) $this->success('修改成功!^_^');
		else $this->error('修改失败!= =重试吧!');
	}
	public function delTemplets(){
		$this->checkUser('5_2_3');
		$this->checkUser();
		$name = $_GET['name'];
		$this->checkUser();
		$config = file_get_contents('config/config.inc.php');
		preg_match('/\'DEFAULT_THEME\'=>\'(.*?)\'/',$config,$nowTheme);
		$nowTheme = $nowTheme[1];
		if(@unlink(C('ROOT').'/template/Tpl/Home/'.$nowTheme.'/'.$name)) $this->success('删除成功!^_^');
		else $this->error('删除失败?请检查你是否有该文件的权限!= =重试吧!');
	}
}
?>
