<?php
defined('IN_YUNYECMSAdmin') or exit('No permission.');
core::load_admin_class('admin');
class template extends YUNYE_admin
{
	function __construct()
	{
		parent::__construct();
	}
	//加载首页
	public function init()
	{
		if (!getroot('tpl', 'tplstyle')) {
			messagebox(Lan('no_permission_sysconfig'), 'back', "warn");
		}
		$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', "template") . '\" target=\"maincontent\">系统</a></li><li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', 'template') . '\" target=\"maincontent\">模板管理</a></li><li class=\"breadcrumb-item active\">模板列表</li>';
		$cfg = getcfg();
		$available_templates = array();
		$theme_dir = YUNYECMS_ROOT . "theme/";
		$template_dir = @opendir($theme_dir);
		while ($file = readdir($template_dir)) {
			if ($file != '.' && $file != '..' && is_dir(YUNYECMS_ROOT . 'theme/' . $file) && $file != '.svn' && $file != 'index.htm') {
				$available_templates[] = get_template_info($file);
			}
		}
		foreach ($available_templates as $key => $var) {
			if (stripos($var['id'], '-') !== false) {
				$tplid = explode("-", $var['id']);
				$available_templates[$key]['tplid'] = intval(end($tplid));
			}
			if ($var['code'] == $cfg['theme']) {
				$available_templates[$key]['status'] = 1;
			} else {
				$available_templates[$key]['status'] = 0;
			}
		}
		$available_templates = sortArr($available_templates, 'tplid', SORT_DESC, SORT_NUMERIC);
		@closedir($template_dir);
		require tpl_adm('template_list');
	}


	public function apply()
	{
		if (!getroot('tpl', 'tplstyle')) {
			messagebox(Lan('no_permission_sysconfig'), 'back', "warn");
		}
		if (!empty($_REQUEST['theme'])) {
			$data["theme"] = usafestr(trim($_REQUEST["theme"]));
		} else {
			messagebox("模板参数错误！", url_admin('init'), "warn");
		}

		if ($_REQUEST["yyact"] == "switch_tpl") {
			$strsql = "select * from `#yunyecms_config` order by id desc limit 0,1";
			$curconfig = $this->db->find($strsql);
			if (empty($curconfig)) {
				messagebox("配置信息错误！", url_admin('init'), "warn");
			}
			$retres = $this->db->update($data, array('id' => $curconfig['id']), "config");
			if ($retres) {
				$doing = "更换模板";
				$yyact = "Switch_TPL";
				insert_admlogs($doing, $yyact);
				messagebox("更换模板成功！", url_admin('init', 'template'), "success");
			} else {
				messagebox("更换模板失败！", url_admin('init', 'template'), "error");
			}
		} else {
			messagebox("模板参数错误！", url_admin('init'), "warn");
		}
	}





	public function view()
	{
		if (!getroot('system', 'msgview')) {
			messagebox(Lan('no_permission_msgview'), 'back', "warn");
		}
		if (!empty($_GET["id"])) {
			$parnav = '<li class=\"breadcrumb-item\"><a href=\"' . url_admin('init', 'notice') . '\" target=\"maincontent\">系统消息管理</a></li><li class=\"breadcrumb-item active\">修改系统消息</li>';
			$id = usafestr(trim($_GET["id"]));
			if (!is_numeric($id)) {
				messagebox("系统消息参数错误", url_admin('init'));
			}
			$info = $this->db->find("select * from `#yunyecms_msg` where `id`= {$id}");
			if (empty($info)) {
				messagebox("系统消息不存在", $_SERVER['HTTP_REFERER']);
			} else {
				if ($info["content"]) $info["content"] = unserialize($info["content"]);
				$strsql = "update `#yunyecms_msg` set isview=1 where id=" . $id . "";
				$this->db->query($strsql);
			}
		}
		require tpl_adm('msg_view');
	}



	public function finaldelete()
	{
		if (!getroot('system', 'msgdel')) {
			messagebox(Lan('no_permission_msgdel'), 'back', "warn");
		}
		$id = $_REQUEST["id"];
		if (!is_array($id)) {
			$id = compact('id');
		}
		$idarray = $id;
		if (isset($idarray)) {
			foreach ($idarray as $key => $var) {
				if (!is_numeric($var)) {
					messagebox("错误的参数！", 'back', "warn");
				}
				$idarray[$key] = usafestr($var);
			}
			$idarray = implode(",", $idarray);
			$retres = $this->db->delete("id in ({$idarray})", "msg");
			if ($retres !== false) {
				$yyact = "Msg_Del";
				$logcontent['tablename'] = "msg";
				$logcontent['action'] = $yyact;
				$logcontent['ids'] = serialize($id);
				$logcontent = serialize($logcontent);
				$doing = "删除系统系统消息：{$idarray} ";
				insert_admlogs($doing, $yyact, $logcontent);
				messagebox(Lan('admin_delete_success'), $_SERVER['HTTP_REFERER'], "success");
			} else {
				messagebox(Lan('admin_delete_error'), $_SERVER['HTTP_REFERER'], "warn");
			}
		} else {
			messagebox(Lan('admin_delete_error'), $_SERVER['HTTP_REFERER'], "warn");
		}
	}

	private function check_exist($id)
	{
		$id = trim($id);
		if (empty($id)) {
			return false;
		} else {
			if (!is_numeric($id)) {
				return false;
			}
			if ($this->db->find("select count(*) as cnt from `#yunyecms_msg` where `id`= {$id}")) {
				return true;
			}
		}
	}
}
