<?php
defined('PP_PATH') or exit;
class guestbook_control extends user_control{
	public $_navgroup = 'home';	
	//我的留言
	public function index(){
		$this->assign_value('navkey', 'guestbook-index');
		$model = &$this->guestbook;

		// 初始分页
		$pagenum = 10;
		$where['uid'] = $this->_user['uid'];
		$total = $model->find_count($where);
		
		$maxpage = max(1, ceil($total/$pagenum));
		$page = min($maxpage, max(1, intval(R('page'))));
		$pages = pages($page, $maxpage, 'index.php?u=guestbook-index-page-{page}');
		$this->assign('total', $total);
		$this->assign('pages', $pages);
		
		$list_arr = $model->find_fetch($where, array('id'=>'-1'), ($page-1)*$pagenum, $pagenum, $total);
		$this->assign('lists', $list_arr);
		
		$this->display('guestbook.htm');
	}
	
	//发布
	public function add(){
		$this->assign_value('navkey', 'guestbook-index');
		if( empty($_POST) ){
			$this->display();
		}else{
			$data = R('data_info','P');
			if( empty($data) ){
				$this->message(0, '留言数据为空！');
			}else{
				$code = R('code','P');
				if( empty($code) ){
					$this->message(0, '验证码不能为空！');
				}elseif ( md5($code) != session::get('guestcode') ){
					$this->message(0, '验证码不正确！');
				}
				
				$model = &$this->guestbook;
				$data['uid'] = $this->_uid;
				$data['dateline'] = time();
				$data['ip'] = ip(1);
				
				$id = $model->create($data);
				if( $id ){
					$this->message(1, '留言成功！','index.php?u=guestbook-index');
				}else{
					$this->message(0, '留言失败！');
				}
			}
		}
	}
	//删除
	public function del(){
		$model = &$this->guestbook;
		$id = intval( R('id','P') );
	
		$where['id'] = $id;
		$where['uid'] = $this->_uid;

		$status = $model->find_delete($where);
		if($status){
			E(0, '成功');
		}else{
			E(1, '删除留言失败！');
		}
	}
}
