<?php

/*
'#################################################################
'# 程序名称：中环CMSV2.0
'# 授权方式：免费软件
'# 作者：善
'# 联系QQ:9118847
'# 作者网站:http://www.ps173.com
'# 发布时间：2015-6-10
'# 声明：欢迎广大的PHP爱好者一起共同探讨
'# 声明：各种用途均可免费使用、自由传播，但必须保留程序代码中的版权信息。
'#################################################################
 */
 
//留言Model类
class BookModel extends Model{

	public function __construct(){
		parent::__construct();
		$this->tabname = TAB_FIX."book";
		$this->tabkey = array("id", "aid", "name", "email", "cont", "state", "cmt", "ip", "ptime");
	}

	public function fpro($id, $post){
		return $this->where($id)->mod($post);
	}

	public function artdel($where){
		return $this->where($where)->del();
	}

	public function delete($id){
		return $this->del($id);
	}

	public function update($post){
		return $this->mod($post);
	}

	public function viewbook($id){
		return $this->field("id, aid, name, email, cont, state, ip, ptime")->where(array("id"=>$id))->one();
	}

	public function setstate($post){
		if($post['state'] == 0){
			$post['state'] = 1;
		}else if($post['state'] == 1){
			$post['state'] = 0;
		}else{
			return false;
		}

		$this->mod($post);
	}

	public function book_total($where =null){
		return $this->total($where);
	}

	public function showbook($where, $limit){
		return $this->field("id, aid, name, email, cont, state, ip, ptime")->where($where)->order("id desc")->limit($limit)->multi(array("article", 'title', 'id', 'aid'));
	}

	public function setcmt($id){
		return $this->where(array("id"=>$id))->mod("cmt=cmt+1");
	}

	public function cookie($id){
		setcookie('cmt', $id, time()+3600*24);
	}

	public function getbook($aid, $limit){
		$data = $this->field("id, name, cont, cmt, ptime")->where(array("aid"=>$aid, "state"=>0))->limit($limit)->order("id desc")->all();
		if(is_array($data) && !empty($data)){
			$set = D("set");
			foreach($data as $key => $val){
				$data[$key]['cont'] = $set->getword($val['cont']);
			}
		}

		return $data;
	}

	public function getcmt($aid){
		$data = $this->field("id, name, cont, cmt, ptime")->where(array("aid"=>$aid, "state"=>0))->limit(3)->order("cmt desc")->all();
		if(is_array($data) && !empty($data)){
			$set = D("set");
			foreach($data as $key => $val){
				$data[$key]['cont'] = $set->getword($val['cont']);
			}
		}

		return $data;
	}

	//添加或修改留言
	public function insert($post){
		 if(isset($post['id'])){
		 	return $this->mod($post);
		}else{
			$post['ptime'] = time();
			return $this->add($post);
		}
	}

}



?>