<?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\user\controller;
use \news\user\controller;
use news\user\model\comment AS model;
use	\temmoku\lib\pages;
use \news\init;
class comment extends controller{
	public function index(){
		!isset($_GET['page']) && $_GET['page']=1;
		!isset($_GET['state']) && $_GET['state']='';
		$rows='1';
		if($_GET['state']==='0' || $_GET['state']==='1'){
			$WHERE="state = '$_GET[state]'";
			$url='state/'.$_GET['state'].'/';
		}else{
			$WHERE="1";
		}
		$WHERE.=' AND uid='.C('MYDB.uid');
		$data=(new model)->select('*')->from(init::$table."comment")->where($WHERE)->orderByDESC(array('id'))->setPaging($rows)->page($_GET['page'])->query();
		$Get_Page=(new pages)->Pages(init::$table."comment",$WHERE,"",$rows,$_GET['page']);
		$Get_Page = preg_replace_callback('/page_([0-9]+)/',function ($matches) {return user_module_url."comment/index/page/".$matches[1];}, $Get_Page );
		$this->assign('data', $data);
    	$this->assign('Get_Page', $Get_Page);
		if(DEVICE != 'pc'){
			$this->assign('webname','我的评论');
		}
    	$this->display(tpl('comment/index'));
	}
	
	public function del(){
		
		if($_POST['id']){
			if(!C(init::$dir.'|SETTING.comment_del')){
				echo json_encode(['code'=>'505','text'=>'系统设置不允许删除自己的评论']);
				exit;
			}
			hook_listen('news_comment_del_begin',['id'=>$_POST['id']]);
			//查询要删除的数据是不是自己的
			$data=(new model)->select('*')->from(init::$table."comment")->where('id='.$_POST['id'] ." AND uid=".C('MYDB.uid'))->row();
			if($data){
				if($data['reply_id']=='0'){
					//更新主表comment的值
					(new model)->query("UPDATE `".init::$table."article` SET `comment_num` = comment_num-1 WHERE aid='$data[aid]'");
				}else{
					$this->update_down($rows['reply_id']);
				}
				(new model)->delete(init::$table."comment")->where("id='$_POST[id]'")->query();
				hook_listen('news_comment_del_end',['id'=>$_POST['id']]);
				echo json_encode(['code'=>'0','text'=>'删除成功']);
			}else{
				echo json_encode(['code'=>'505','text'=>'不存在的数据或者没有权限']);
			}
		}elseif(is_array($_POST['iddb'])){
			if(!C(init::$dir.'|SETTING.comment_del')){
				$this->err('系统设置不允许删除自己的评论');
			}
			$id=implode(',',$_POST['iddb']);
			//查询一下自己的评论，不能胡乱删除
			$data=(new model)->select('*')->from(init::$table."comment")->where("id IN ($id) AND uid=".C('MYDB.uid'))->query();
			foreach($data AS $row){
				$rows=(new model)->select('*')->from(init::$table."comment")->where('id='.$row['id'])->row();
				hook_listen('news_comment_del_begin',['id'=>$row['id']]);
				//主评论
				if($rows['reply_id']=='0'){
					//更新主表comment的值
					(new model)->query("UPDATE `".init::$table."article` SET `comment_num` = comment_num-1 WHERE aid='$rows[aid]'");
				}else{
					$this->update_down($rows['reply_id']);
				}
				(new model)->delete(init::$table."comment")->where("id='$row[id]'")->query();
				hook_listen('news_comment_del_begin',['id'=>$row['id']]);
			}
			
			$this->success('删除成功');
		}
	}
	private function update_down($id){
		//查询下自己下面的有没有回复。有就上提一级
		$reply=(new model)->delete(init::$table."comment")->where("reply_id='$id'")->query();
		if($reply){
			foreach($reply as $row){
				//更新下面的评论继承自己的
				(new model)->update(init::$table."comment")->cols(['reply_id'=>$id])->where('id='.$row['uid'])->query();
			}
		}
	}
}
?>