<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace news\home\controller;
use \temmoku\db;
use \temmoku\controller;
class comment extends controller{
	
	public function alllist(){
		$rows=10;
		!isset($_GET['page']) && $_GET['page']=1;
		$get_comment_list=db::select('*')->from(M_jab."comment")->where("aid=$_GET[aid] AND reply_id=0")->orderByDESC(array('id'))->setPaging($rows)->page($_GET['page'])->query();
		foreach($get_comment_list AS $row){
			if($row['state']==='0'){
				if($row['uid']==C('MYDB.uid') ||	C('MYDB.groupid')	==	'3'){
					$row['content']="$row[content]<span class='red'><p>等待审核中，只有本人或者管理员才可以查看</p></span>";
				}else{
					$row['content']="<span class='red'>等待审核中，不允许查看</span>";
				}
			}
			if($row['uid']==C('MYDB.uid') && $row['uid'] !='0'){
				$row['quanxian']=1;
			}elseif(C('MYDB.groupid')=='3'){
				$row['quanxian']=2;
			}else{
				$row['quanxian']=0;
			}
			$l=$this->comment_list($row['id']);
			if($l){
				$row['list']=$l;
			}
			
			$row['posttime']=formatTime($row['posttime']);
			$get_comment_listDB[]=$row;
		}
		echo json_encode($get_comment_listDB);
	}
	public function post(){
		$row=db::select('*')->from(M_jab."article")->where("aid= '$_POST[aid]'")->row();
		if(!$row){
			$a=array('code'=>'501','text'=>'要评论的内容不存在');
			echo json_encode($a);
			exit;
		}
		//如果不是登录用户
		if(!C('MYDB') && !C(MODULE.'|SETTING.is_login_add_comment')){
			$a=array('code'=>'19','text'=>'必须登录才可以发布评论');
			echo json_encode($a);
			exit;
		}else{
			
			if($row['is_comment']){
				$a=array('code'=>'502','text'=>'本篇文章已经禁止了评论');
				echo json_encode($a);
				exit;
			}

			//存在则说明限制每天用户发布的评论数量，不存在或者为0则不限制
			if(C(MODULE.'|SETTING.today_comment_num') && C(MODULE.'|SETTING.is_login_add_comment')){
				//计算时间。今天的0点；
				$start_time=strtotime(date('Y-m-d 00:00:00',NOWTIME));
				//今天的23点
				$end_time=strtotime(date('Y-m-d 23:59:59',NOWTIME));
				extract(db::select('count(*) AS NUM')->from(M_jab."comment")->where("uid= '".C('MYDB.uid')."' AND $start_time < posttime AND $end_time > posttime")->row());
				if($NUM>=C(MODULE.'|SETTING.today_comment_num')){
					$a=array('code'=>'503','text'=>'每天最多可以发布评论“'.C(MODULE.'|SETTING.today_comment_num').'”条，你已经超过限制');
					echo json_encode($a);
					exit;
				}
			}
		}
		//如果权限需要使用验证码或者不存在，都需要进行判断验证码是否正确
		if(!C(MODULE.'|SETTING.add_comment_yzm')){
			$yzm=\temmoku\lib\yzm::get_yzm_code($_POST['yz_code']);
			if($yzm['code'] =='9'){
				$a=array('code'=>'9','text'=>'验证码错误');
				echo json_encode($a);
				exit;
			}
		}
		
		if(C('MYDB.groupid')=='3'){
			$state=1;
		}else{
			if(C(MODULE.'|SETTING.add_comment_state')){
				$state=1;
			}else{
				$state=0;
			}
		}
		$reply_id = intval($_POST['reply_id']);
		if(!C('mydb.uid')){
			$uid='0';
			$username='游客';
		}else{
			$uid=C('MYDB.uid');
			$username=C('MYDB.username');
		}
		$array=array(
	    'aid'=>$_POST['aid'],
	    'uid'=>$uid,
	    'username'=>$username,
	    'state'=>$state,
	    'reply_id'=>$reply_id,
	    'content'=>$_POST['content'],
	    'posttime'=>NOWTIME);
		$id = db::insert(M_jab."comment")->cols($array)->query();
		
		//更新主表comment的值,只有不是回复的时候才去更新
		if($reply_id=='0'){
			db::query("UPDATE `".M_jab."article` SET `comment_num` = `comment_num`+1 WHERE aid='$row[aid]'");
		}
		$array['posttime']=formatTime(NOWTIME);
		$a=array('code'=>'0','id'=>$id,'reply_id'=>$reply_id,'text'=>$array);
		echo json_encode($a);
		exit;
	}
	public function del(){
		if(!C(MODULE.'|SETTING.comment_del')){
			echo json_encode(['code'=>'504','text'=>'系统设置不允许删除自己的评论']);
			exit;
		}
		//查询要删除的数据是不是自己的
		$data=db::select('*')->from(M_jab."comment")->where('id='.$_POST['id'])->row();
		if($data['uid'] != C('mydb.uid') && C('mydb.groupid') != '3'){
			echo json_encode(['code'=>'505','text'=>'没有权限删除']);
			exit;
		}
		(new \news\Public_class\Comment)->comment_del($data['id']);
		echo json_encode(['code'=>'0','text'=>'删除成功']);
	}
	private function comment_list($id){
		$get_comment_list=db::select('*')->from(M_jab."comment")->where("reply_id=$id")->orderByDESC(array('id'))->query();
		if($get_comment_list){
			foreach($get_comment_list AS $row){
				if($row['state']==0){
					if($row['uid']===C('MYDB.uid') ||	C('MYDB.groupid')	===	'3'){
						$row['content']="$row[content]<span class='red'><p>等待审核中，只有本人或者管理员才可以查看</p></span>";
					}else{
						$row['content']="<span class='red'>等待审核中，不允许查看</span>";
					}
					
				}
				if($row['uid']==C('MYDB.uid') && $row['uid'] !='0'){
					$row['quanxian']=1;
				}elseif(C('MYDB.groupid')=='3'){
					$row['quanxian']=2;
				}else{
						$row['quanxian']=0;
				}
				
				$row['posttime']=formatTime($row['posttime']);
				$l=self::comment_list($row['id']);
				if($l){
					$row['list']=$l;
				}
				$get_comment_listDB[]=$row;
			}
			
			return $get_comment_listDB;
		}
		return ;
	}
}
?>
