<?php
/**
* FILE_NAME : guestbook.php
*
* @copyright Copyright (c) www.shuangyulin.com
* @author 双鱼林设计
* @package 用户留言管理
* @subpackage
* @version QQ:287307421或254540457
*/

define('IN_WEB', TRUE);
	
require_once( str_replace('\\', '/', dirname(__FILE__)). '/include/global.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act'])){
    $_REQUEST['act'] = 'show';
} else {
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

$sql = $query = $allnum = $perpage = $curpage = $startpage = $book = '';

/*------------------------------------------------------ */
//-- 显示留言列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'show')
{
	/*--  查询数据总量，实例分页  --*/
	$sql = "SELECT * FROM {$dbprefix}messages";
	$query = $DB->query($sql);
	$allnum = $DB->num_rows($query);
	$perpage = 10;
	$curpage = isset($_REQUEST['page']) ? (int)$_GET['page'] : 1;
	$php_self = "guestbook.php?act=show";

	/*--  创建分页  --*/
	$pages = multi($allnum, $perpage, $curpage, $php_self);
	
	/*--  根据分页查询数据  --*/
	if (($curpage-1) >= 0) {
		$step = $curpage-1;
	} else {
		$step = 0;
	}
	$startpage = $perpage * $step;
	
	$sql = "SELECT * FROM {$dbprefix}messages LIMIT $startpage,$perpage";
	$query = $DB->query($sql);
	while ($row = $DB->fetch_array($query)) {
		/*--  格式化时间  --*/
		$row['postdate'] = formatdate($_CFG['format_type'],$row['postdate']);
		$row['isrepeat'] = $row['repeat'] ? "已回复" : "未回复";
		$booklist[] = $row;
	}

	$smarty->assign("css_root",$css_root);
	$smarty->assign("booklist",$booklist);
	$smarty->assign("pages",$pages);
	$smarty->display("guestbook.html");
	
}

/*------------------------------------------------------ */
//-- 回复留言
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'repeat')
{
	$mid = $_REQUEST['mid'];
	
	$sql = "SELECT * FROM {$dbprefix}messages WHERE mid = $mid";
	$book = $DB->fetch_one_array($sql);
	
	$smarty->assign("css_root",$css_root);
	$smarty->assign("web_url",$web_url);
	
	$smarty->assign("mid",$book['mid']);
	$smarty->assign("message",$book['message']);
	$smarty->assign("repeat",$book['repeat']);
	$smarty->display("guestbook_repeat.html");
}

/*------------------------------------------------------ */
//-- 提交回复留言
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'repeatok')
{
	$book = $_REQUEST['book'];
	$sql = $query = '';
	
	$sql = "UPDATE {$dbprefix}messages SET `repeat` = '".$book['repeat']."',`repeattime` = '".$timestamp."' WHERE `mid` = '".$book['mid']."'";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("留言回复成功","guestbook.php?act=show");
	} else {
		showmsg("留言回复失败");
	}
	
}

/*------------------------------------------------------ */
//-- 删除留言
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'remove')
{
	$mid = $_REQUEST['mid'];
	
	$sql = "DELETE FROM {$dbprefix}messages WHERE mid = $mid";
	$query = $DB->query($sql);
	
	if ($query) {
		showmsg("留言删除成功","guestbook.php?act=show");
	} else {
		showmsg("留言删除失败");
	}
	
}

/*------------------------------------------------------ */
//-- 查看留言
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'view')
{
	$mid = $_REQUEST['mid'];
	$sql = '';
	
	$sql = "SELECT * FROM {$dbprefix}messages WHERE mid = $mid";
	$book = $DB->fetch_one_array($sql);
	
	$smarty->assign("css_root",$css_root);
	$smarty->assign("web_url",$web_url);
	
	$smarty->assign("title",$book['title']);
	$smarty->assign("message",$book['message']);
	$smarty->assign("repeat",$book['repeat']);
	$smarty->display("guestbook_view.html");
	
}
?>