<?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;
use \temmoku\lib\pages;
class special extends controller
{

    public function category()
    {
    	

    	$sid=$_GET['sid'];
    	!isset($_GET['page']) && $_GET['page']=1;
    	$cache_dir=RUNTIME_PATH."/cache/".MODULE."/special/category/{$sid}_{$_GET[page]}.txt";
    	if(C(MODULE.'|setting.special_category_cache_time') && is_file($cache_dir) && time()-@filemtime($cache_dir) < C(MODULE.'|setting.special_category_cache_time')*3600){
    		echo get_cache($cache_dir);
    		exit;
    	}
    	$data=C(MODULE.'|SPECIAL_CATEGORY_SETTING.'.$sid);
    	if(!$data){
    		$data=db::select('*')->from(M_jab.'special_category')->where("sid= '$sid' ")->row();
    		if(!$data){
    			$this->err('数据不存在');
    		}
    	}

    	$getalllist=$this->get_category_list($sid);
    	
    	
    	$getlist=$this->get_one_list($sid,'20',$_GET['page']);
    	
    	$setting=unserialize($data['setting']);
    	if($setting['tpl']['category']){
    		$tpl=$setting['tpl']['category'];
    	}else{
    		$tpl="Special/category";
    	}
    	
    	$TITLE=$data['name'].$data['seo_name']."_".C("WEBNAME");
    	$KEYWORDS=$data['name'].",".$data['seo_keywords'].",".C("WEBNAME");
    	$DESCRIPTION=$data['seo_description'];
    	
    	//面包屑
    	$Crumbs = preg_replace_callback('/sid=([0-9]+)/',function ($matches) {global $data;return U(['sid'=>$matches[1],'special'=>'category'],MODULE);}, C(MODULE.'|Special_Crumbs.'.$sid) );

    	$this->assign('WEBNAME',$TITLE);
    	$this->assign('KEYWORDS',$KEYWORDS);
    	$this->assign('DESCRIPTION',$DESCRIPTION);
    	$this->assign('data',$data);
    	$this->assign('getlist',$getlist);
    	$this->assign('Crumbs',$Crumbs);
    	$this->assign('Get_Page',$Get_Page);
    	$this->assign('getalllist',$getalllist);
		$this->display(tpl($tpl));

		if(C(MODULE.'|setting.special_category_cache_time')){
			write_file($cache_dir,ob_get_contents());
		}
    }
    
    public function content(){
    	$id=$_GET['id'];
    	$sid=$_GET['sid'];
		
		$cache_dir=APP_PATH."/runtime/cache/".MODULE."/special/list/{$sid}/{$id}.txt";
    	if(C(MODULE.'|setting.special_content_cache_time') && is_file($cache_dir) && time()-@filemtime($cache_dir) < C(MODULE.'|setting.special_content_cache_time')*3600){
    		echo get_cache($cache_dir);
    		exit;
    	}
    	$data=db::select('*')->from(M_jab.'special_list')->where("id= '$id'")->row();
    	if(!$data){
    		$this->err('数据不存在');
    	}
    	db::query("UPDATE `".M_jab."special_list` SET `clicks` = `clicks`+1 WHERE id=$data[id]");
    	$setting=unserialize($data['setting']);
    	
    	if($setting['tpl']['category']){
    		$tpl=$setting['tpl']['category'];
    	}else{
    		$tpl="Special/content";
    	}
    	
    	$setting['rows'] ? $data['rows']= $setting['rows'] : $data['rows']='20';
    	$setting['comment_rows'] ? $data['comment_rows']= $setting['comment_rows'] : $data['comment_rows']='20';
    	
    	$getlist=$this->get_article($id,$data['rows']);
    	
    	//面包屑
    	$Crumbs = preg_replace_callback('/sid=([0-9]+)/',function ($matches) {global $data;return U(['sid'=>$matches[1],'special'=>'category'],MODULE);}, C(MODULE.'|Special_Crumbs.'.$data['sid']) );
    	extract(db::select(' COUNT(id) AS article_num')->from(M_jab.'special_article')->where("lid = $id")->row());
    	
    	$data['article_num']=$article_num;
    	
    	$TITLE=$data['name'].$data['seo_name']."_".C("WEBNAME");
    	$KEYWORDS=$data['name'].",".$data['seo_keywords'].",".C("WEBNAME");
    	$DESCRIPTION=$data['seo_description'];

    	$this->assign('WEBNAME',$TITLE);
    	$this->assign('KEYWORDS',$KEYWORDS);
    	$this->assign('DESCRIPTION',$DESCRIPTION);
    	$this->assign('Crumbs',$Crumbs);
    	$this->assign('data',$data);
    	$this->assign('getlist',$getlist);
    	$this->display(tpl($tpl));
    	if(C(MODULE.'|setting.special_content_cache_time')){
    		$content=ob_get_contents();
    		$content .="<script type=\"text/javascript\" src=\"".M_WEBURL."/special/content_update?id=$id\" ></script>";
			write_file($cache_dir,$content);
		}
    }
    
    public function content_update(){
    	$data=db::select('*')->from(M_jab.'special_list')->where("id= '$_GET[id]'")->row();
    	if($data){
    		db::query("UPDATE `".M_jab."special_list` SET `clicks` = `clicks`+1 WHERE id=$data[id]");
    		$clicks=$data['clicks']+1;
    		echo "$('#clicks').html($clicks);$('#comment_num').html($data[comment_num]);allnum='$data[comment_num]'";
    	}
    }
    
    public function ajax_article(){
    	$id=$_POST['id'];
    	$sid=$_POST['sid'];
    	if(!$id){
    		$array=['code'=>'1','text'=>'缺少必要参数'];
    	}else{
    		$data=C(MODULE.'|Special_list_setting.'.$id);
    		if(!$data){
    			$array=['code'=>'501','text'=>'数据不存在'];
    		}else{
    			!isset($_POST['page']) && $_POST['page']=1;
    			$setting=unserialize($data['setting']);
    			$setting['rows'] ? $data['rows']= $setting['rows'] : $data['rows']='20';
    			$getlist=$this->get_article($id,$data['rows'],$_POST['page']);
    			$array=['code'=>'0','text'=>'ok','list'=>$getlist];
    		}
    	}
    	echo json_encode($array);
    }
    
    public function comment_list(){
		!isset($_POST['page']) && $_POST['page']=1;
		$id=$_POST['id'];
		
		$data=C(MODULE.'|Special_list_setting.'.$id);
		if(!$data){
    		$array=['code'=>'0','text'=>'数据不存在'];
    	}else{
    		!isset($_POST['page']) && $_POST['page']=1;
    		$setting=unserialize($data['setting']);
    		$setting['comment_rows'] ? $rows= $setting['comment_rows'] : $rows='10';
    		
			$get_comment_list=db::select('*')->from(M_jab."special_comment")->where("lid=$id AND reply_id='0'")->orderByDESC(array('id'))->setPaging($rows)->page($_POST['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_son($row['id']);
				if($l){
					$row['list']=$l;
				}
				
				$row['posttime']=formatTime($row['posttime']);
				$get_comment_listDB[]=$row;
			}
			
			$array=['code'=>'1','text'=>'操作成功','list'=>$get_comment_listDB];
   		}
		echo json_encode($array);
    }
    
    public function comment_add(){
    	$id=$_POST['id'];
    	$data=C(MODULE.'|Special_list_setting.'.$id);
		if(!$data){
			$a=array('code'=>'501','text'=>'要评论的内容不存在');
			echo json_encode($a);
			exit;
		}
		
		if($data['is_comment']=='0'){
			$a=array('code'=>'502','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_recommend']){
				$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((new CommentModel)->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['Result']==='0'){
				$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(
	    'lid'=>$id,
	    'uid'=>$uid,
	    'username'=>$username,
	    'state'=>$state,
	    'reply_id'=>$reply_id,
	    'content'=>$_POST['content'],
	    'posttime'=>NOWTIME);
		$lid = db::insert(M_jab."special_comment")->cols($array)->query();
		
		//更新主表comment的值,只有不是回复的时候才去更新
		if($reply_id=='0'){
			db::query("UPDATE `".M_jab."special_list` SET `comment_num` = `comment_num`+1 WHERE id='$data[id]'");
		}
		$array['posttime']=formatTime(NOWTIME);
		$a=array('code'=>'1','id'=>$lid,'reply_id'=>$reply_id,'text'=>$array);
		echo json_encode($a);
		exit;
    }
    
    public function comment_del(){
    	
		if(!C(MODULE.'|SETTING.comment_del')){
			echo json_encode(['code'=>'501','text'=>'系统设置不允许删除自己的评论']);
			exit;
		}
		
		//查询要删除的数据是不是自己的
		$data=db::select('*')->from(M_jab."special_comment")->where('id='.$_POST['id'])->row();
		
		if($data['uid'] != C('mydb.uid') && C('mydb.groupid') != '3'){
			echo json_encode(['code'=>'505','text'=>'没有权限删除']);
			exit;
		}
		
		//先删除主评论
		db::delete(M_jab."special_comment")->where("id='$data[id]'")->query();
		
		$this_son=db::select('*')->from(M_jab."special_comment")->where('reply_id='.$_POST['id'])->row();
		
		if($this_son){
			db::update(M_jab."special_comment")->cols(['reply_id'=>$data['reply_id']])->where('reply_id='.$data['id'])->query();
		}
		
		echo json_encode(['code'=>'1','text'=>'删除成功']);
	}
    
    private function comment_list_son($id){
		$get_comment_list=db::select('*')->from(M_jab."special_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_son($row['id']);
				if($l){
					$row['list']=$l;
				}
				$get_comment_listDB[]=$row;
			}
			
			return $get_comment_listDB;
		}
		return ;
	}
    
    
    
    
    
    
    private  function get_category_list($sid){
    	$_data=db::select('*')->from(M_jab.'special_category')->where("upsid=$sid")->query();
    	if(!$_data){
    		return;
    	}
    	$all_sid='';
    	foreach($_data AS $row){
    		$list=$this->get_one_list($row['sid']);
    		if(!$list){
    			$all_sid=$this->get_son($row['sid']);
    			if($all_sid){
    				$list=$this->get_one_list(implode(',',$all_sid));
    			}
    			
    		}
    		$row['list']=$list;
    		$data[]=$row;
    	}
    	return $data;
    }
    
    private function get_article($id,$rows=20,$page=1){
    	$_list=db::select('aid')->from(M_jab.'special_article')->where("lid = $id")->orderByDESC(array('aid'))->setPaging($rows)->page($page)->query();
    	foreach($_list AS $row){
    		$_data=db::select('aid,cid,title,posttime')->from(M_jab.'article')->where("aid= '$row[aid]' ")->row();
    		$_data['url']=U(['aid'=>$_data['aid'],'cid'=>$_data['cid']],MODULE);
    		unset($_data['aid'],$_data['cid']);
    		$_data['posttime']=date('Y-m-d H:i',$_data['posttime']);
    		$getlist[]=$_data;
    	}
    	
    	return $getlist;
    }
    
    
    private function get_son($sid){
    	static $data;
    	$_data=db::select('*')->from(M_jab.'special_category')->where("upsid=$sid")->query();
    	$data=[];
    	foreach($_data AS $row){
    		$this->get_son($row['sid']);
    		$data[]=$row['sid'];
    	}
    	return $data;
    }
    
    private function get_one_list($sid,$rows='20',$page='1'){
    	$data=db::select('*')->from(M_jab.'special_list')->where("sid IN($sid)")->orderByDESC(array('reorder'))->setPaging($rows)->page($page)->query();
    	return $data;
    }
}
?>