<?php
class followControl extends skymvc{
	public $userid;
	public function __construct(){
		parent::__construct();
		$this->loadmodel(array("login","user","follow","followed"));
		$this->login->checkLogin();
		$this->userid=$this->login->userid;
	}
	
	public function onDefault(){
		$_GET['type']=$type=max(1,get('type'));
		if($type==2){
			$where =" status=2  ";
		}else{
			$where="";
		}
		
		$option=array(
			"where"=>$where
		);
		$rscount=true;
		if($type==3){
			$data=$this->followed->setTable("followed",$this->userid)->select($option,$rscount);
		}else{
			$data=$this->follow->setTable("follow",$this->userid)->select($option,$rscount);
		}
		
		if($data){
			foreach($data as $k=>$v){
				$uids[]=$v['t_userid'];
			}
			$user=$this->user->getUserByIds($uids);
			foreach($data as $k=>$v){
				$v['user_head']=$user[$v['t_userid']]['user_head'];
				$v['t_nickname']=$user[$v['t_userid']]['nickname'];
				
				$data[$k]=$v;
			}
		}
		
		
		$this->smarty->assign(array(
			"data"=>$data
		));
		if($type==3){
			$this->smarty->display("follow/followed.html");
		}else{
			$this->smarty->display("follow/index.html");
		}
		
	}
	
	
	
	public function onFollow(){
		$t_userid=get_post('t_userid','i');
		if($this->userid == $t_userid){
			exit(json_encode(array("error"=>1,"message"=>"不能关注自己哦")));
		}
		//判断是否关注了
		$row=$this->follow->setTable("follow",$this->userid)->selectRow(array("where"=>"t_userid=".$t_userid." AND userid=".$this->userid."   "));
		if(isset($row['status'])  ){
			exit(json_encode(array("error"=>1,"message"=>"你已经关注他了")));
		}else{
			//判断是否被关注 主角我
			$row=$this->followed->setTable("followed",$this->userid)->selectRow(array("where"=>" t_userid=".$t_userid." AND userid=".$this->userid."  "));
			if(isset($row['status'])){//被关注
				//插入关注表 双向关注
				$data=array(
					"userid"=>$this->userid,
					"t_userid"=>$t_userid,
					"status"=>2,
					"dateline"=>time(),
					"siteid"=>SITEID
				);
				$data['id']=$this->follow->setTable("follow_10")->insert($data);
				$this->follow->setTable("follow",$this->userid)->insert($data);
				
				/******************被关注表************/
				//更新被关注人
				$this->follow->setTable("follow",$t_userid)->update(array("status"=>2),"  userid=".$t_userid." AND t_userid=".$this->userid."  ");
				// 更新自己
				$this->followed->setTable("followed",$this->userid)->update(array("status"=>2)," t_userid=".$t_userid." AND userid=".$this->userid."  ");
				
				//插入 被关注的人
				$data=array(
					"t_userid"=>$this->userid,
					"userid"=>$t_userid,
					"status"=>2,
					"dateline"=>time(),
					"siteid"=>SITEID
				);
				$data['id']=$this->followed->setTable("followed_10")->insert($data);
				$this->followed->setTable("followed",$t_userid)->insert($data);	
			}else{
				//插入关注表
				$data=array(
					"userid"=>$this->userid,
					"t_userid"=>$t_userid,
					"status"=>1,
					"dateline"=>time(),
					"siteid"=>SITEID
				);
				$data['id']=$this->follow->setTable("follow_10")->insert($data);
				$this->follow->setTable("follow",$this->userid)->insert($data);
				//插入被关注表
				$data=array(
					"t_userid"=>$this->userid,
					"userid"=>$t_userid,
					"status"=>1,
					"dateline"=>time(),
					"siteid"=>SITEID
				);
				$data['id']=$this->followed->setTable("followed_10")->insert($data);
				$this->followed->setTable("followed",$t_userid)->insert($data);	
			}
			$this->user->changenum("follow_num",1,"userid=".$this->userid);
			$this->user->changenum("followed_num",1,"userid=".$t_userid);					
			exit(json_encode(array("error"=>0,"message"=>"关注成功")));
		}
	}
	
	public function onUnfollow(){
		$t_userid=get_post('t_userid','i');
		$row=$this->follow->setTable("follow",$this->userid)->selectRow(array("where"=>"t_userid=".$t_userid." AND userid=".$this->userid."  "));
		if(empty($row)){
			exit(json_encode(array("error"=>1,"message"=>"你还未关注他哦")));
		}else{
			//删除关注
			$this->follow->setTable("follow",$this->userid)->delete("id=".$row['id']);
			if($row['status']==2){//双向关注 更新为关注
				$this->followed->setTable("followed",$this->userid)->update(array("status"=>1)," t_userid=".$t_userid." AND  userid=".$this->userid."  ");
				$this->followed->setTable("followed",$t_userid)->delete(" userid=".$t_userid." AND t_userid=".$this->userid."  ");
				
				$this->follow->setTable("follow",$t_userid)->update(array("status"=>1)," userid=".$t_userid." AND t_userid=".$this->userid."  ");
				
			}else{				
				//删除被关注 单向关注 直接删除
				$this->followed->setTable("followed",$t_userid)->delete(" userid=".$t_userid." AND t_userid=".$this->userid."  ");
			}
			$this->user->changenum("follow_num",-1,"userid=".$this->userid);
			$this->user->changenum("followed_num",-1,"userid=".$t_userid);				
			exit(json_encode(array("error"=>0,"message"=>"取消关注成功")));
		}
	}
	
}

?>