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



require_once './include/common.inc.php';



/*--  对一些系统变量赋值  --*/
$template->assign("css_root",$css_root);
$template->assign("siteurl",$_CFG['siteurl']);
$template->assign("sitename",$_CFG['sitename']);



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

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

	/*--  创建分页  --*/
	$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']);
		if (!$row['repeattime']) {
			$row['repeattime'] = formatdate($_CFG['format_type'],$row['repeattime']);
		}
		$booklist[] = $row;
	}
	
	require_once 'header.php';

	$template->assign("booklist",$booklist);
	$template->assign("pages",$pages);
	$template->display("guestbook.html");
	
	require_once 'footer.php';
} 

  

/*------------------------------------------------------ */
//-- 提交添加留言
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'addok') {
	$sql = $query = '';
	$book = isset($_REQUEST['book']) ? $_REQUEST['book'] : '';

	/*--  添加留言信息  --*/
	$sql = "INSERT INTO {$dbprefix}messages (`mid` ,`author` ,`title` ,`email` ,`message` ,`ip` ,`postdate` ,`repeat` ,`repeattime`) VALUES ('','".$book['username']."','".$book['title']."','".$book['email']."','".$book['content']."','".$onlineip."','".$timestamp."','','')";
	
	$query = $DB->query($sql);
	if (!$query) {
		showmessage("留言添加失败，请重新填写！");
	} else {
		showmessage("留言添加成功","guestbook.php");
	}
}

/*------------------------------------------------------ */
//-- 添加留言
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add') {
	require_once 'header.php';
	$template->display("newbook.htm");
	require_once 'footer.php';
}




?>