<?php
if (!defined('MOD')) die("access die");
purviewCheck('mypage', 'plugin');
$action = $_GET['action'] ? $_GET['action'] : 'manage' ;
switch ($action) {
	case 'add':
		if ($_POST[insert] == 1) {
			if (!trim($_POST['title']) || !trim($_POST['content'])) {
				qbase::showmsg('您必须输入标题和内容，请返回修改。');
			} 
			$timestamp = $_SERVER['REQUEST_TIME'];
			$tpl = trim($_POST[tpl]);
			qdb::Query("INSERT INTO ".TABLEPRE."page (title,meta_title,meta_keywords,meta_description,content,tpl,updatetime) VALUES ('" . trim($_POST[title]) . "','" . trim($_POST[meta_title]) . "','" . trim($_POST[meta_keywords]) . "','" . trim($_POST[meta_description]) . "', '" . trim($_POST[content]) . "','$tpl','$timestamp')");
			qbase::showmsg('操作成功！', 'default', 'plugin.php?plugin=mypage');
		} 
		break;

	case 'edit':
		if ($_POST['insert'] == 1) {
			if (!trim($_POST['title']) || !trim($_POST['content'])) {
				qbase::showmsg('您必须输入标题和内容，请返回修改。');
			} else {
				$timestamp = $_SERVER['REQUEST_TIME'];
				$tpl = trim($_POST[tpl]);
				qdb::Query("UPDATE ".TABLEPRE."page SET  title='" . trim($_POST[title]) . "',meta_title='" . trim($_POST[meta_title]) . "',meta_keywords='" . trim($_POST[meta_keywords]) . "',meta_description='" . trim($_POST[meta_description]) . "',content='" . trim($_POST[content]) . "',tpl='$tpl',updatetime='$timestamp' WHERE id='" . intval($_POST[id]) . "'");
				$key = C_P . "/page/" . intval($_POST['id']) . ".html";
				if(is_file($key)) @unlink($key);
				qbase::showmsg('操作成功！', 'default', 'plugin.php?plugin=mypage');
			} 
		} else {
			$id = intval($_GET['id']);
			$info = qdb::QueryFirst("select * from ".TABLEPRE."page where id='$id'");
			if (is_array($info)) {
				@extract($info);
			} else {
				qbase::showmsg('没有找到相关内容', 'default', 'plugin.php?plugin=mypage');
			} 
		} 
		break;

	case 'manage':
		$pages = array();
		$query = qdb::fetchAll("SELECT * FROM ".TABLEPRE."page ORDER BY id DESC");
		foreach ($query as $page) {
			$page['updatetime'] = date('Y-m-d', $page['updatetime']);
			$page['url'] = REWRITE ? W_P . "index-ac-page-pageid-{$page['id']}.html" : W_P . "index.php?ac=page&pageid={$page['id']}";
			$pages[] = $page;
		} 
	break;

	case 'delete':
		checkAdminReferer();
		$id = intval($_GET['id']) ? intval($_GET['id']) : $_POST['id'];
		if (!$id) {
			qbase::showmsg('error request', 'default', 'plugin.php?plugin=mypage');
		} 
		$ids = is_array($id) ? implode(',', $id) : $id;
		qdb::Query("DELETE FROM ".TABLEPRE."page WHERE id IN ($ids)");
		$ids_array = explode(",", $ids);
		foreach($ids_array as $id){
			$key = C_P . "/page/" . intval($id) . ".html";
			if(is_file($key)) @unlink($key);
		}
		qbase::showmsg('操作成功！', 'default', 'plugin.php?plugin=mypage');
	break;
} 
include qbase::template($path . 'plugin/mypage');

?>