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

class BookAction extends CommonAction{

	public function index(){
		if(!$this->isCached(null, $_SERVER["REQUEST_URI"])){
			if(isset($_GET['aid'])){
				$aid = intval($_GET['aid']);
				unset($_GET['aid']);
			}

			$data =$this->info();
			$this->assign("books", $this->article->art_list("id, title, posttime", 16, array("views"=>0), "nums desc"));
			$page= new Page($this->book->book_total(array("aid"=>$aid)), $data['size'], "aid/{$aid}");
			$this->assign("page", $page->pageinfo());
			$this->assign("book", $this->book->getbook($aid, $page->limit));
			$this->assign("title", $this->article->showtit($aid));
			$this->assign("list", $this->nav->menu());
			$this->assign("flink", $this->viewflink());
			$this->assign("info", $data);
		}
		
		$this->assign("adver", $this->viewadver());
		$this->assign("ip", val::clientip());
		$this->display(null, $_SERVER["REQUEST_URI"]);
	}

	public function checkbook($post){
		$rs = true;
		if(!val::check($post['name'])){
			$this->msg('姓名不能为空');
			$rs = false;
		}

		if(!val::checkemail($post['email'])){
			$this->msg('电子邮箱不正确');
			$rs = false;
		}

		if(!val::check($post['cont'])){
			$this->msg('评论内容不能为空');
			$rs = false;
		}

		if(val::length($post['cont'], 300, 'max')){
			$this->msg('评论内容不能超过300个字');
			$rs = false;
		}

		return $rs;
	}

	public function add($post){
		$this->caching=0;
		if(!empty($post)){
			$num = $this->book->insert($post);
			if(!empty($num)){
				$this->article->setnums($post['aid']);
			}
		}

		return $num;
	}

	public function addbook(){
		$this->caching=0;
		if(isset($_POST['sub'])){
			if(strtoupper($_POST['code']) == $_SESSION['code']){
				if($this->checkbook($_POST)){
					if($this->add($_POST)){
						if($_POST['state'] == '0'){
							$this->msg('恭喜您，评论成功');
						}else if($_POST['state'] == '1'){
							$this->msg('评论成功，请等待管理员审核！');
						}
					}	
				}else{
					$this->msg('很愦憾，评论失败！');
				}
			}else{
				$this->msg('验证码不正确');
			}
		}
	}

	//验证码输出
	public function code(){
		$this->caching=0;
	 	echo new Vcode();
	}

	public function setcmt(){
		$this->caching=0;
		if(isset($_GET['id'])){
			$this->book->cookie($_GET['id']);
			if(isset($_COOKIE['cmt']) && $_COOKIE['cmt'] == $_GET['id']){
				$this->msg('你已经赞过了，请不要重复操作');
			}else{
				if($this->book->setcmt($_GET['id'])){
					$this->msg('操作成功！');
				}else{
					$this->msg('操作失败!');
				}
			}
		}
	}

}

?>